Skip to content

Commit ba7463b

Browse files
author
Kapil Borle
committed
Add violation correction section to about_PSScriptScriptAnalyzer
1 parent 804ead7 commit ba7463b

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

Engine/about_PSScriptAnalyzer.help.txt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,47 @@ RULE SUPPRESSSION
183183
Target="*")]
184184
param()
185185
}
186+
187+
VIOLATION CORRECTION
188+
189+
Most violations can be fixed by replacing the violation causing content with the correct alternative. In an attempt to provide the user with the ability to correct the violation we provide a property - `SuggestedCorrections`, in each DiagnosticRecord instance. This property contains the information needed to rectify the violation. For example, consider a script `C:\tmp\test.ps1` with the following content.
190+
191+
PS> Get-Content C:\tmp\test.ps1
192+
gci C:\
193+
194+
Invoking PSScriptAnalyzer on the file gives the following output.
195+
196+
PS>$diagnosticRecord = Invoke-ScriptAnalyzer -Path C:\tmp\test.p1
197+
PS>$diagnosticRecord | select SuggestedCorrections | Format-Custom
198+
199+
class DiagnosticRecord
200+
{
201+
SuggestedCorrections =
202+
[
203+
class CorrectionExtent
204+
{
205+
EndColumnNumber = 4
206+
EndLineNumber = 1
207+
File = C:\Users\kabawany\tmp\test3.ps1
208+
StartColumnNumber = 1
209+
StartLineNumber = 1
210+
Text = Get-ChildItem
211+
Description = Replace gci with Get-ChildItem
212+
}
213+
]
214+
215+
}
216+
217+
The *LineNumber and *ColumnNumber properties give the region of the script that can be replaced by the contents of Text property, i.e., replace gci with Get-ChildItem.
218+
219+
The main motivation behind having SuggestedCorrections is to enable quick-fix like scenarios in editors like VSCode, Sublime, etc. At present, we provide valid SuggestedCorrection only for the following rules, while gradually adding this feature to more rules.
220+
221+
* AvoidAlias.cs
222+
* AvoidUsingPlainTextForPassword.cs
223+
* MisleadingBacktick.cs
224+
* MissingModuleManifestField.cs
225+
* UseToExportFieldsInManifest.cs
226+
186227

187228
EXTENSIBILITY
188229

0 commit comments

Comments
 (0)