diff --git a/Rules/UseConsistentIndentation.cs b/Rules/UseConsistentIndentation.cs index 099002274..48b5beb8d 100644 --- a/Rules/UseConsistentIndentation.cs +++ b/Rules/UseConsistentIndentation.cs @@ -99,7 +99,23 @@ public override IEnumerable AnalyzeScript(Ast ast, string file // we add this redundant check if (onNewLine) { - AddViolation(token, indentationLevel, diagnosticRecords, ref onNewLine); + var tempIndentationLevel = indentationLevel; + + // Ignore comments + // Since the previous token is a newline token we start + // looking for comments at the token before the newline token. + int j = k - 2; + while (j > 0 && tokens[j].Kind == TokenKind.Comment) + { + --j; + } + + if (j >= 0 && tokens[j].Kind == TokenKind.Pipe) + { + ++tempIndentationLevel; + } + + AddViolation(token, tempIndentationLevel, diagnosticRecords, ref onNewLine); } break; } diff --git a/Tests/Rules/UseConsistentIndentation.tests.ps1 b/Tests/Rules/UseConsistentIndentation.tests.ps1 index 51b411a70..633435845 100644 --- a/Tests/Rules/UseConsistentIndentation.tests.ps1 +++ b/Tests/Rules/UseConsistentIndentation.tests.ps1 @@ -113,4 +113,35 @@ function foo { $violations.Count | Should Be 0 } } + + Context "When a multi-line command is given" { + It "Should find a violation if a pipleline element is not indented correctly" { + $def = @' +get-process | +where-object {$_.Name -match 'powershell'} +'@ + $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings + $violations.Count | Should Be 1 + } + + It "Should not find a violation if a pipleline element is indented correctly" { + $def = @' +get-process | + where-object {$_.Name -match 'powershell'} +'@ + $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings + $violations.Count | Should Be 0 + } + + It "Should ignore comment in the pipleline" { + $def = @' + get-process | + where-object Name -match 'powershell' | # only this is indented correctly +select Name,Id | + format-list +'@ + $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings + $violations.Count | Should Be 3 + } + } }