diff --git a/Rules/UseConsistentIndentation.cs b/Rules/UseConsistentIndentation.cs index 5fd573cc5..0e6680e63 100644 --- a/Rules/UseConsistentIndentation.cs +++ b/Rules/UseConsistentIndentation.cs @@ -282,7 +282,8 @@ public override IEnumerable AnalyzeScript(Ast ast, string file GetCommentRunIndentationOffset( tokens, tokenIndex - 2, - out bool commentRunStartsAfterLineContinuation); + out bool commentRunStartsAfterLineContinuation, + out _); if (commentRunStartsAfterLineContinuation) { tempIndentationLevel++; @@ -302,7 +303,14 @@ public override IEnumerable AnalyzeScript(Ast ast, string file commentIndentationOffset = GetCommentRunIndentationOffset( tokens, tokenIndex, - out bool commentRunStartsAfterLineContinuation); + out bool commentRunStartsAfterLineContinuation, + out bool commentRunStartsAfterPipeline); + if (pipelineIndentationStyle == PipelineIndentationStyle.None && + commentRunStartsAfterPipeline) + { + onNewLine = false; + continue; + } tempIndentationLevel = indentationLevel + (commentRunStartsAfterLineContinuation ? 1 : 0); } @@ -348,7 +356,8 @@ public override IEnumerable AnalyzeScript(Ast ast, string file private static int GetCommentRunIndentationOffset( Token[] tokens, int tokenIndex, - out bool commentRunStartsAfterLineContinuation) + out bool commentRunStartsAfterLineContinuation, + out bool commentRunStartsAfterPipeline) { int firstCommentIndex = tokenIndex; int currentLine = tokens[tokenIndex].Extent.StartLineNumber; @@ -376,6 +385,8 @@ private static int GetCommentRunIndentationOffset( commentRunStartsAfterLineContinuation = firstCommentIndex > 0 && tokens[firstCommentIndex - 1].Kind == TokenKind.LineContinuation; + commentRunStartsAfterPipeline = + PreviousLineEndedWithPipe(tokens, firstCommentIndex, tokens[firstCommentIndex]); int leastIndentedColumn = tokens[firstCommentIndex].Extent.StartColumnNumber; for (int nextCommentIndex = firstCommentIndex + 2; nextCommentIndex < tokens.Length && diff --git a/Tests/Rules/UseConsistentIndentation.tests.ps1 b/Tests/Rules/UseConsistentIndentation.tests.ps1 index 6febc02c7..d07a7b0a5 100644 --- a/Tests/Rules/UseConsistentIndentation.tests.ps1 +++ b/Tests/Rules/UseConsistentIndentation.tests.ps1 @@ -661,6 +661,19 @@ foo | Test-CorrectionExtentFromContent @params } + It "Should preserve indentation in a comment run after a pipeline with PipelineIndentation None" { + $def = @' +foo | + # first comment + # second comment +bar +'@ + $settings.Rules.PSUseConsistentIndentation.PipelineIndentation = 'None' + $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings + $violations | Should -BeNullOrEmpty + Invoke-Formatter -ScriptDefinition $def -Settings $settings | Should -Be $def + } + It "Should indent properly after line continuation (backtick) character with pipeline" { $def = @' foo |