Skip to content

Commit eb05a8e

Browse files
author
Kapil Borle
committed
Modify correction extent of misleading backticks rule
1 parent d811566 commit eb05a8e

2 files changed

Lines changed: 33 additions & 11 deletions

File tree

Rules/MisleadingBacktick.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
5555
{
5656
int lineNumber = ast.Extent.StartLineNumber + i;
5757

58-
var start = new ScriptPosition(fileName, lineNumber, match.Index, line);
59-
var end = new ScriptPosition(fileName, lineNumber, match.Index + match.Length, line);
58+
var start = new ScriptPosition(fileName, lineNumber, match.Index + 1, line);
59+
var end = new ScriptPosition(fileName, lineNumber, match.Index + match.Length + 1, line);
6060
var extent = new ScriptExtent(start, end);
6161
yield return new DiagnosticRecord(
6262
string.Format(CultureInfo.CurrentCulture, Strings.MisleadingBacktickError),
@@ -75,12 +75,12 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
7575
/// <param name="cmdAst"></param>
7676
/// <returns>Returns a list of suggested corrections</returns>
7777
private List<CorrectionExtent> GetCorrectionExtent(IScriptExtent violationExtent)
78-
{
78+
{
7979
var corrections = new List<CorrectionExtent>();
8080
corrections.Add(new CorrectionExtent(
81-
violationExtent.StartLineNumber,
81+
violationExtent.StartLineNumber ,
8282
violationExtent.EndLineNumber,
83-
violationExtent.StartColumnNumber,
83+
violationExtent.StartColumnNumber + 1,
8484
violationExtent.EndColumnNumber,
8585
String.Empty,
8686
violationExtent.File));

Tests/Rules/MisleadingBacktick.tests.ps1

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,41 @@
11
Import-Module PSScriptAnalyzer
22
$writeHostName = "PSMisleadingBacktick"
33
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
4-
$violations = Invoke-ScriptAnalyzer $directory\MisleadingBacktick.ps1 | Where-Object {$_.RuleName -eq $writeHostName}
4+
$violationsFilepath = Join-Path $directory 'MisleadingBacktick.ps1'
5+
$violations = Invoke-ScriptAnalyzer $violationsFilepath | Where-Object {$_.RuleName -eq $writeHostName}
56
$noViolations = Invoke-ScriptAnalyzer $directory\NoMisleadingBacktick.ps1 | Where-Object {$_.RuleName -eq $clearHostName}
67

78
Describe "Avoid Misleading Backticks" {
89
Context "When there are violations" {
910
It "has 5 misleading backtick violations" {
11+
Import-Module .\PSScriptAnalyzerTestHelper.psm1
1012
$violations.Count | Should Be 5
1113

12-
foreach ($violation in $violations)
13-
{
14-
$violation.SuggestedCorrections.Count | Should Be 1
15-
$violation.SuggestedCorrections[0].Text | Should Be ''
16-
}
14+
$idx = 0
15+
$violations[$idx].SuggestedCorrections.Count | Should Be 1
16+
$violations[$idx].SuggestedCorrections[0].Text | Should Be ''
17+
Get-ExtentText $violations[$idx].SuggestedCorrections[0] $violationsFilepath | Should BeExactly ' '
18+
19+
$idx = 1
20+
$violations[$idx].SuggestedCorrections.Count | Should Be 1
21+
$violations[$idx].SuggestedCorrections[0].Text | Should Be ''
22+
Get-ExtentText $violations[$idx].SuggestedCorrections[0] $violationsFilepath | Should BeExactly ' '
23+
24+
$idx = 2
25+
$violations[$idx].SuggestedCorrections.Count | Should Be 1
26+
$violations[$idx].SuggestedCorrections[0].Text | Should Be ''
27+
Get-ExtentText $violations[$idx].SuggestedCorrections[0] $violationsFilepath | Should BeExactly ' '
28+
29+
$idx = 3
30+
$violations[$idx].SuggestedCorrections.Count | Should Be 1
31+
$violations[$idx].SuggestedCorrections[0].Text | Should Be ''
32+
Get-ExtentText $violations[$idx].SuggestedCorrections[0] $violationsFilepath | Should BeExactly ' '
33+
34+
$idx = 4
35+
$violations[$idx].SuggestedCorrections.Count | Should Be 1
36+
$violations[$idx].SuggestedCorrections[0].Text | Should Be ''
37+
Get-ExtentText $violations[$idx].SuggestedCorrections[0] $violationsFilepath | Should BeExactly ' '
38+
1739
}
1840
}
1941

0 commit comments

Comments
 (0)