Skip to content

Commit 7de186a

Browse files
author
Kapil Borle
committed
Add violation correction to readme
1 parent 8072873 commit 7de186a

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,50 @@ public System.Collections.Generic.IEnumerable<IRule> GetRule(string[] moduleName
194194
string[] ruleNames)
195195
```
196196

197+
Violation Correction
198+
====================
199+
200+
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.
201+
202+
```powershell
203+
PS> Get-Content C:\tmp\test.ps1
204+
gci C:\
205+
```
206+
207+
Invoking PSScriptAnalyzer on the file gives the following output.
208+
209+
```powershell
210+
PS>$diagnosticRecord = Invoke-ScriptAnalyzer -Path C:\tmp\test.p1
211+
PS>$diagnosticRecord | select SuggestedCorrections | Format-Custom
212+
213+
class DiagnosticRecord
214+
{
215+
SuggestedCorrections =
216+
[
217+
class CorrectionExtent
218+
{
219+
EndColumnNumber = 4
220+
EndLineNumber = 1
221+
File = C:\Users\kabawany\tmp\test3.ps1
222+
StartColumnNumber = 1
223+
StartLineNumber = 1
224+
Text = Get-ChildItem
225+
Description = Replace gci with Get-ChildItem
226+
}
227+
]
228+
229+
}
230+
```
231+
232+
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.
233+
234+
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.
235+
236+
* AvoidAlias.cs
237+
* AvoidUsingPlainTextForPassword.cs
238+
* MisleadingBacktick.cs
239+
* MissingModuleManifestField.cs
240+
* UseToExportFieldsInManifest.cs
197241

198242
Building the Code
199243
=================

0 commit comments

Comments
 (0)