PowerShell / PSScriptAnalyzer

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

`invoke-scriptanalyzer -fix -Confirm` should be more specific #1854

Open iRon7 opened 2 years ago

iRon7 commented 2 years ago

Currently the -Fix -Confirm (and -WhatIf) common parameters aren't very specific on what and how PSScriptAnalyzer is about to correct the found violations:

invoke-scriptanalyzer .\Test.ps1 -fix -Confirm

Confirm
Are you sure you want to perform this action?
Performing the operation "Analyzing and fixing file C:\Users\Gebruiker\Scripts\Test\Rule\Test.ps1" on target "C:\Users\Gebruiker\Scripts\Test\Rule\Test.ps1".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): n

I would expect a more specific message of what and how a violation is changed, e.g.:

invoke-scriptanalyzer .\Test.ps1 -fix -Confirm

Confirm
Are you sure you want to perform this action?
Changing "$pascalCase = 'Test'" to "$PascalCase = 'Test'" at line 10 character 5 of file  "Analyzing and fixing file C:\Users\Gebruiker\Scripts\Test\Rule\Test.ps1" on target "C:\Users\Gebruiker\Scripts\Test\Rule\Test.ps1".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): n
bergmeister commented 2 years ago

Asking on a per file basis was the easiest to implement because otherwise there are many different low level code paths for invoking rules as they are treated differently internally based on subtypes (custom rule vs DSC rule vs built in, etc.). But the real challenge to ask the user more specifically is that the rules are executed in parallel and result are later collected and merged. Therefore we'd need to have a different way of running rules individually, collecting the result, asking the user, applying the fix conditionally and then continuing just to be able to achieve what you want. I am not saying it's impossible but it is a LOT of plumbing work that potentially requires further refactoring to make it possible.

Maybe a better way to achieve the same result that you ask for is to collect diagnostic results first from Invoke-ScriptAnalyzer and then the user can pass them to a (new?) cmdlet to apply the correction or have an extension method on a diagnostic record to apply the suggested correction. What do you think about this?