Skip to content

Suggested corrections for violations #502

Closed
@kapilmb

Description

@kapilmb

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:

  • Replace the violation text with correction text.
  • For Example, replace gci with Get-ChildItem
  • Add text to the script that corrects the violation.
  • For Example, should process related changes.
  • Ability to propogate the change within the entire script
  • For Example, consider the PSUseSingularNoun rule. It triggers whenever a function definition is found wherein the noun part is plural. To rectify the violation we can suggest the singular version of the noun and then apply the change to all the instances of the function in the script.

The primary motivation behind this feature is to allow editors to take advantage of this property to enable quick fix scenarios for PS scripts.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions