-
Notifications
You must be signed in to change notification settings - Fork 395
Suggested corrections for violations #502
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Love this. A few thoughts:
You might not hit these targets on the first go, but I wanted to add them while they were top of mind because I think this is what this feature/functionality should strive towards. |
The model we've designed will enable multi-file corrections in the first release. A single rule will be able to make insertions, modifications, or deletions in an arbitrary set of files. The example you give for renaming a function and finding all of the occurrences and changing them is one that we're actively investigating. The idea of defining an alias to map the old name to the new one is great! I think we'll be at the mercy of editors when it comes to giving the user optional, additional corrections while performing a primary one (like renaming a cmdlet and then getting the option to add an alias). Right now VS Code doesn't give you the ability to do a multi-step correct, you just select a single correction item and it applies that. I'm looking forward to this one! |
@KirkMunro Those are great suggestions. Thanks! As @daviwil mentioned, the current model that we have can allow multi-file edits. But for the first release of this feature we are planning to roll out support for only script-wide edits. This is due to the way PSSA works as of now. Having the ability to propagate a change within a module and across modules is something that will need a significant redesign of the PSSA engine. Nevertheless, these are great suggestions to have on our TODO list. |
I agree with everything said so far in this thread with one caveat: we might want to hold off on implementing any auto-correct for changes which may require edits throughout multiple files. As @KirkMunro suggests, that design is probably going to a bit trickier, and I don't want to paint ourselves into a corner here. |
At present we have added this capability (see PR #508) to the following rules
Which other rules should be equipped with this functionality so that it can improve authoring/editing PS scripts experience? |
I would be more suggestive of moving this functionality to its own cmdlet, Invoke-ScriptAnalyzerCorrection Reason being for me is that it allows me as an author to either use it or not use it especially for the reasons listed by Kirk above. I like the idea to be able to do this though and think it's great it's being put on the table but I'm a little concerned that doing so as an additional aspect to Invoke-ScriptAnalyzer will likely pollute it's real purpose - To inform the author about what they could be looking to improve in their own writing practices. |
I don't think we need another cmdlet. But the idea of applying auto-correct is interesting and makes me think back to tools like chkdsk (am I aging myself?). That would allow users to get the details about what could be improved by default (like it does today, but with an identifier/flag in the default format applied to the output or a "[AutoCorrectible]" prefix/qualifier in the issue description that clearly indicates whether or not a rule is autocorrectable), and then PSScriptAnalyzer could be run again with an optional -AutoCorrect switch to apply the changes. I think that would be a better approach than a separate cmdlet. |
@kilasuit This feature only emits a property which can be consumed by external utilities (e.g. editors) to provide auto-correct capability. Invoke-ScriptAnalyzer by itself will not modify the scripts that it analyzes. |
@kapilmb Thanks for that clarificatin, that makes more sense, so that editors can apply correction to one or multiple issues identified by this feature, leveraging their UI properly to highlight auto-correctible items and to show what the corrections look like before applying them. |
Whenever PSScriptAnalyzer (PSSA) is invoked on a PowerShell (PS) script, it runs a set of rules against the contents of the script. Each rule checks for some sort of violation. As of now, the violations need to be fixed manually by the script author. Many of the violations, however, are simple fixes that can be automated. For example, gci triggers the PSAvoidUsingAliases rule and can be easily rectified by replacing gci with Get-ChildItem.
Whenever there are violations in a script, PSSA invocation emits a list of DiagnosticRecord (DR) instances. Each DR instance corresponds to one violation of a rule. The DR also contains the extent of the violation. We want to provide the correction extent that can be used to remove the violation. Whenever, a rule processes a script syntax tree, it has, in most cases, all the information needed to correct the violation. Hence, it would be efficient for the rule itself to emit the correction extent such that we can tag on a corrections property to each emitted DR. There are three cases that such a correction extent needs to address:
The primary motivation behind this feature is to allow editors to take advantage of this property to enable quick fix scenarios for PS scripts.
The text was updated successfully, but these errors were encountered: