Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions Rules/UseConsistentIndentation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,8 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
GetCommentRunIndentationOffset(
tokens,
tokenIndex - 2,
out bool commentRunStartsAfterLineContinuation);
out bool commentRunStartsAfterLineContinuation,
out _);
if (commentRunStartsAfterLineContinuation)
{
tempIndentationLevel++;
Expand All @@ -302,7 +303,14 @@ public override IEnumerable<DiagnosticRecord> 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);
}
Expand Down Expand Up @@ -348,7 +356,8 @@ public override IEnumerable<DiagnosticRecord> 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;
Expand Down Expand Up @@ -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 &&
Expand Down
13 changes: 13 additions & 0 deletions Tests/Rules/UseConsistentIndentation.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment thread
Copilot marked this conversation as resolved.
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 |
Expand Down
Loading