diff --git a/Rules/UseConsistentIndentation.cs b/Rules/UseConsistentIndentation.cs index 74f3602d6..7fc98509d 100644 --- a/Rules/UseConsistentIndentation.cs +++ b/Rules/UseConsistentIndentation.cs @@ -404,6 +404,11 @@ private static int GetCommentRunIndentationOffset( return 0; } + if (HasCodeBeforeCommentOnSameLine(tokens, tokenIndex)) + { + return 0; + } + int firstCommentIndex = tokenIndex; int currentLine = tokens[tokenIndex].Extent.StartLineNumber; int precedingIndex = tokenIndex - 1; diff --git a/Tests/Rules/UseConsistentIndentation.tests.ps1 b/Tests/Rules/UseConsistentIndentation.tests.ps1 index e178a146d..81c90d324 100644 --- a/Tests/Rules/UseConsistentIndentation.tests.ps1 +++ b/Tests/Rules/UseConsistentIndentation.tests.ps1 @@ -932,6 +932,31 @@ function test { Invoke-Formatter -ScriptDefinition $expected -Settings $settings | Should -Be $expected } + It "Should exclude a trailing comment after a line-leading pipe from comment runs" { + $def = @' +function test { + foo ` + | # trailing +# standalone + bar +} +'@ + $expected = @' +function test { + foo ` + | # trailing + # standalone + bar +} +'@ + $settings.Rules.PSUseConsistentIndentation.PipelineIndentation = 'NoIndentation' + $formatted = Invoke-Formatter -ScriptDefinition $def -Settings $settings + + $formatted | Should -Be $expected + Invoke-Formatter -ScriptDefinition $formatted -Settings $settings | + Should -Be $expected + } + It "Should indent properly after line continuation (backtick) character with pipeline" { $def = @' foo |