Skip to content

Commit cfea14f

Browse files
bergmeisterJamesWTruher
authored andcommitted
Add documentation for -Fix switch to README.md (#852)
1 parent 26f623a commit cfea14f

File tree

1 file changed

+8
-41
lines changed

1 file changed

+8
-41
lines changed

README.md

Lines changed: 8 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Usage
5151
``` PowerShell
5252
Get-ScriptAnalyzerRule [-CustomizedRulePath <string[]>] [-Name <string[]>] [<CommonParameters>] [-Severity <string[]>]
5353
54-
Invoke-ScriptAnalyzer [-Path] <string> [-CustomizedRulePath <string[]>] [-ExcludeRule <string[]>] [-IncludeRule <string[]>] [-Severity <string[]>] [-Recurse] [<CommonParameters>]
54+
Invoke-ScriptAnalyzer [-Path] <string> [-CustomizedRulePath <string[]>] [-ExcludeRule <string[]>] [-IncludeRule <string[]>] [-Severity <string[]>] [-Recurse] [-Fix] [<CommonParameters>]
5555
```
5656

5757
[Back to ToC](#table-of-contents)
@@ -318,49 +318,16 @@ public System.Collections.Generic.IEnumerable<IRule> GetRule(string[] moduleName
318318

319319
Violation Correction
320320
====================
321-
Most violations can be fixed by replacing the violation causing content with the correct alternative.
322321

323-
In an attempt to provide the user with the ability to correct the violation we provide a property, `SuggestedCorrections`, in each DiagnosticRecord instance,
324-
that contains information needed to rectify the violation.
322+
Some violations can be fixed by replacing the violation causing content with a suggested alternative. You can use the `-Fix` switch to automatically apply the suggestions. Since `Invoke-ScriptAnalyzer` implements `SupportsShouldProcess`, you can additionally use `-WhatIf` or `-Confirm` to find out which corrections would be applied. It goes without saying that you should use source control when applying those corrections since some some of them such as the one for `AvoidUsingPlainTextForPassword` might require additional script modifications that cannot be made automatically. Should your scripts be sensitive to encoding you should also check that because the initial encoding can not be preserved in all cases.
325323

326-
For example, consider a script `C:\tmp\test.ps1` with the following content:
327-
``` PowerShell
328-
PS> Get-Content C:\tmp\test.ps1
329-
gci C:\
330-
```
331-
332-
Invoking PSScriptAnalyzer on the file gives the following output.
333-
``` PowerShell
334-
PS>$diagnosticRecord = Invoke-ScriptAnalyzer -Path C:\tmp\test.p1
335-
PS>$diagnosticRecord | select SuggestedCorrections | Format-Custom
336-
337-
class DiagnosticRecord
338-
{
339-
SuggestedCorrections =
340-
[
341-
class CorrectionExtent
342-
{
343-
EndColumnNumber = 4
344-
EndLineNumber = 1
345-
File = C:\Users\kabawany\tmp\test3.ps1
346-
StartColumnNumber = 1
347-
StartLineNumber = 1
348-
Text = Get-ChildItem
349-
Description = Replace gci with Get-ChildItem
350-
}
351-
]
352-
}
353-
```
354-
355-
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.
356-
357-
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.
324+
The initial motivation behind having the `SuggestedCorrections` property on the `ErrorRecord` (which is how the `-Fix` switch works under the hood) was 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.
358325

359-
* AvoidAlias.cs
360-
* AvoidUsingPlainTextForPassword.cs
361-
* MisleadingBacktick.cs
362-
* MissingModuleManifestField.cs
363-
* UseToExportFieldsInManifest.cs
326+
- AvoidAlias.cs
327+
- AvoidUsingPlainTextForPassword.cs
328+
- MisleadingBacktick.cs
329+
- MissingModuleManifestField.cs
330+
- UseToExportFieldsInManifest.cs
364331

365332
[Back to ToC](#table-of-contents)
366333

0 commit comments

Comments
 (0)