Skip to content

Commit 8072873

Browse files
author
Kapil Borle
committed
Add CorrectionExtent description to some rules
1 parent 10bcbdc commit 8072873

7 files changed

Lines changed: 21 additions & 7 deletions

Rules/AvoidAlias.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,15 @@ private List<CorrectionExtent> GetCorrectionExtent(CommandAst cmdAst, string cmd
7878
var alias = cmdAst.GetCommandName();
7979
var startColumnNumber = ext.StartColumnNumber + ext.Text.IndexOf(alias);
8080
var endColumnNumber = startColumnNumber + alias.Length;
81+
string description = string.Format("Replace {0} with {1}", alias, cmdletName);
8182
corrections.Add(new CorrectionExtent(
8283
ext.StartLineNumber,
8384
ext.EndLineNumber,
8485
startColumnNumber,
8586
endColumnNumber,
8687
cmdletName,
87-
ext.File));
88+
ext.File,
89+
description));
8890
return corrections;
8991
}
9092

Rules/AvoidUsingPlainTextForPassword.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,16 @@ private List<CorrectionExtent> GetCorrectionExtent(ParameterAst paramAst)
7373
{
7474
IScriptExtent ext = paramAst.Extent;
7575
var corrections = new List<CorrectionExtent>();
76-
string correctionText = string.Format("{0} {1}", "[SecureString]", paramAst.Name.Extent.Text);
76+
string correctionText = string.Format("[SecureString] {0}", paramAst.Name.Extent.Text);
77+
string description = string.Format("Set {0} type to SecureString", paramAst.Name.Extent.Text);
7778
corrections.Add(new CorrectionExtent(
7879
ext.StartLineNumber,
7980
ext.EndLineNumber,
8081
ext.StartColumnNumber,
8182
ext.EndColumnNumber,
8283
correctionText,
83-
ext.File));
84+
ext.File,
85+
description));
8486
return corrections;
8587
}
8688

Rules/MisleadingBacktick.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,16 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
7676
/// <returns>Returns a list of suggested corrections</returns>
7777
private List<CorrectionExtent> GetCorrectionExtent(IScriptExtent violationExtent)
7878
{
79-
var corrections = new List<CorrectionExtent>();
79+
var corrections = new List<CorrectionExtent>();
80+
string description = "Remove trailing whilespace";
8081
corrections.Add(new CorrectionExtent(
8182
violationExtent.StartLineNumber ,
8283
violationExtent.EndLineNumber,
8384
violationExtent.StartColumnNumber + 1,
8485
violationExtent.EndColumnNumber,
8586
String.Empty,
86-
violationExtent.File));
87+
violationExtent.File,
88+
description));
8789
return corrections;
8890
}
8991

Rules/UseToExportFieldsInManifest.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,16 @@ private List<CorrectionExtent> GetCorrectionExtent(string field, IScriptExtent e
126126
break;
127127
default:
128128
throw new NotImplementedException(string.Format("{0} not implemented", field));
129-
}
129+
}
130+
string description = string.Format("Replace {0} with {1}", extent.Text, correctionText);
130131
corrections.Add(new CorrectionExtent(
131132
extent.StartLineNumber,
132133
extent.EndLineNumber,
133134
extent.StartColumnNumber,
134135
extent.EndColumnNumber,
135136
correctionText,
136-
extent.File));
137+
extent.File,
138+
description));
137139
return corrections;
138140
}
139141

Tests/Rules/AvoidUsingAlias.tests.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ Describe "AvoidUsingAlias" {
1919
It "suggests correction" {
2020
Import-Module .\PSScriptAnalyzerTestHelper.psm1
2121
Test-CorrectionExtent $violationFilepath $violations[0] 1 'iex' 'Invoke-Expression'
22+
$violations[0].SuggestedCorrections[0].Description | Should Be 'Replace iex with Invoke-Expression'
23+
2224
Test-CorrectionExtent $violationFilepath $violations[1] 1 'cls' 'Clear-Host'
25+
$violations[1].SuggestedCorrections[0].Description | Should Be 'Replace cls with Clear-Host'
2326
}
2427
}
2528

Tests/Rules/AvoidUsingPlainTextForPassword.tests.ps1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Describe "AvoidUsingPlainTextForPassword" {
1616
It "suggests corrections" {
1717
Import-Module .\PSScriptAnalyzerTestHelper.psm1
1818
Test-CorrectionExtent $violationFilepath $violations[0] 1 '$passphrases' '[SecureString] $passphrases'
19+
$violations[0].SuggestedCorrections[0].Description | Should Be 'Set $passphrases type to SecureString'
20+
1921
Test-CorrectionExtent $violationFilepath $violations[1] 1 '$passwordparam' '[SecureString] $passwordparam'
2022
Test-CorrectionExtent $violationFilepath $violations[2] 1 '$credential' '[SecureString] $credential'
2123
Test-CorrectionExtent $violationFilepath $violations[3] 1 '$password' '[SecureString] $password'

Tests/Rules/UseToExportFieldsInManifest.tests.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Describe "UseManifestExportFields" {
4444
$violations = Run-PSScriptAnalyzerRule $testManifestBadFunctionsWildcardPath
4545
$violationFilepath = Join-path $testManifestPath $testManifestBadFunctionsWildcardPath
4646
Test-CorrectionExtent $violationFilepath $violations[0] 1 "'*'" "@('Get-Foo', 'Get-Bar')"
47+
$violations[0].SuggestedCorrections[0].Description | Should Be "Replace '*' with @('Get-Foo', 'Get-Bar')"
4748
}
4849

4950
It "detects FunctionsToExport with null" {

0 commit comments

Comments
 (0)