Skip to content

Commit b0aae75

Browse files
author
Kapil Borle
authored
Merge pull request #749 from PowerShell/kapilmb/fix-indentation-backtick
Fix indentation on line after backtick (line continuation character)
2 parents 141e109 + 813da65 commit b0aae75

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed

Rules/UseConsistentIndentation.cs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
9191
break;
9292

9393
case TokenKind.NewLine:
94+
case TokenKind.LineContinuation:
9495
onNewLine = true;
9596
break;
9697

@@ -101,18 +102,26 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
101102
{
102103
var tempIndentationLevel = indentationLevel;
103104

104-
// Ignore comments
105-
// Since the previous token is a newline token we start
106-
// looking for comments at the token before the newline token.
107-
int j = k - 2;
108-
while (j > 0 && tokens[j].Kind == TokenKind.Comment)
105+
// Check if the preceding character is an escape character
106+
if (k > 0 && tokens[k - 1].Kind == TokenKind.LineContinuation)
109107
{
110-
--j;
108+
++tempIndentationLevel;
111109
}
112-
113-
if (j >= 0 && tokens[j].Kind == TokenKind.Pipe)
110+
else
114111
{
115-
++tempIndentationLevel;
112+
// Ignore comments
113+
// Since the previous token is a newline token we start
114+
// looking for comments at the token before the newline token.
115+
int j = k - 2;
116+
while (j > 0 && tokens[j].Kind == TokenKind.Comment)
117+
{
118+
--j;
119+
}
120+
121+
if (j >= 0 && tokens[j].Kind == TokenKind.Pipe)
122+
{
123+
++tempIndentationLevel;
124+
}
116125
}
117126

118127
AddViolation(token, tempIndentationLevel, diagnosticRecords, ref onNewLine);

Tests/Rules/UseConsistentIndentation.tests.ps1

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
Import-Module PSScriptAnalyzer
1+
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
2+
$testRootDirectory = Split-Path -Parent $directory
3+
4+
Import-Module PSScriptAnalyzer
5+
Import-Module (Join-Path $testRootDirectory "PSScriptAnalyzerTestHelper.psm1")
6+
7+
$indentationUnit = ' '
8+
$indentationSize = 4
29
$settings = @{
310
IncludeRules = @("PSUseConsistentIndentation")
411
Rules = @{
@@ -143,5 +150,22 @@ select Name,Id |
143150
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
144151
$violations.Count | Should Be 3
145152
}
153+
154+
It "Should indent properly after line continuation (backtick) character" {
155+
$def = @'
156+
$x = "this " + `
157+
"Should be indented properly"
158+
'@
159+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
160+
$violations.Count | Should Be 1
161+
$params = @{
162+
RawContent = $def
163+
DiagnosticRecord = $violations[0]
164+
CorrectionsCount = 1
165+
ViolationText = "`"Should be indented properly`""
166+
CorrectionText = (New-Object -TypeName String -ArgumentList $indentationUnit,$indentationSize) + "`"Should be indented properly`""
167+
}
168+
Test-CorrectionExtentFromContent @params
169+
}
146170
}
147171
}

0 commit comments

Comments
 (0)