PowerShell / PSScriptAnalyzer

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

Auto-update linted/formatted files #1878

Closed nvuillam closed 1 year ago

nvuillam commented 1 year ago

We are trying to provide new capabilities on MegaLinter ( thread )

Despite our researchs, it seems that it is not possible to :

Do you confirm that it is not possible, or did we miss a magic argument that would allow that ?

Note: we saw the ways to have fixes in stdout but it would require to apply ourselves the fixes, and that does not follow the patterns of 99% of linters embedded within MegaLinter

bergmeister commented 1 year ago

Invoke-ScriptAnalyzer has a -Fix switch, which achieves what you want. We have seen that sometimes the file encoding doesn't get preserved though. If you search in issues here, you will find one asking for similar functionality for Invoke-Formatter but you can do it yourself with Get-Content and Set-Content

Also just so you know, you can technically run the formatting rules with Invoke-ScriptAnalyzer as well, I am not sure how well this works in combination with -Fix because what makes Invoke-Formatter special is that it runs the rules sequentially and applies fixes sequentially (and re-parse script again every time because changes are often in an overlapping area) and on top of that loops over this until the rules return no more suggester corrections.

For your use case, it might or might not be easier to use PowerShellEditorServices APIs, which wrap PSSCRIPTANALYZER: https://github.com/PowerShell/PowerShellEditorServices