PowerShell / PSScriptAnalyzer

Download ScriptAnalyzer from PowerShellGallery
https://www.powershellgallery.com/packages/PSScriptAnalyzer/
MIT License
1.85k stars 373 forks source link

Rule request: `Get-Content x | ConvertFrom-Json` should suggest adding `-raw` for `Get-content` #1951

Open jessehouwing opened 10 months ago

jessehouwing commented 10 months ago

Summary of the new feature

When you call get-content file.json | convertfrom-json, funny things happen when file.json contains a comment at the top of the file.

Multiple places suggest that it's better to use get-content -raw for performance and predictability.

See:

Proposed technical implementation details (optional)

When get-content is piped to convertfrom-*, PSScriptAnalyzer should suggest adding -raw to get-content.

The same could apply to [xml] get-content file.xml.

What is the latest version of PSScriptAnalyzer at the point of writing

1.21

SydneyhSmith commented 9 months ago

Thanks for the suggestion @jessehouwing this would make a good custom rule, or this seems like the kind of thing that could be good for a feedback provider. Typically we don't add things into script analyzer that are code optimizers or work arounds. I will mark this as up for grabs if you want to try opening a PR for it.

jessehouwing commented 9 months ago

@SydneyhSmith which existing rule would be a good basis to use to build this from?

bergmeister commented 8 months ago

@jessehouwing See here for docs around custom rules: https://learn.microsoft.com/en-us/powershell/utility-modules/psscriptanalyzer/create-custom-rule?view=ps-modules I kind of agree that we shouldn't add too many cmdlet specific rules but if this one is common, we could consider it, Here is a good, similar example: https://github.com/PowerShell/PSScriptAnalyzer/pull/1857