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
36 changes: 36 additions & 0 deletions Rules/UseConsistentIndentation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,30 @@ private static int GetCommentRunIndentationOffset(
out bool commentRunStartsAfterLineContinuation,
out bool commentRunStartsAfterPipeline)
{
commentRunStartsAfterLineContinuation = false;
commentRunStartsAfterPipeline = false;
if (HasCodeAfterCommentOnSameLine(tokens, tokenIndex))
{
commentRunStartsAfterLineContinuation =
tokenIndex > 0 &&
tokens[tokenIndex - 1].Kind == TokenKind.LineContinuation;
if (tokenIndex > 2 &&
(tokens[tokenIndex - 1].Kind == TokenKind.NewLine ||
tokens[tokenIndex - 1].Kind == TokenKind.LineContinuation) &&
tokens[tokenIndex - 2].Kind == TokenKind.Comment &&
(tokens[tokenIndex - 3].Kind == TokenKind.NewLine ||
tokens[tokenIndex - 3].Kind == TokenKind.LineContinuation))
{
GetCommentRunIndentationOffset(
tokens,
tokenIndex - 2,
out _,
out commentRunStartsAfterPipeline);
}

return 0;
}
Comment thread
bergmeister marked this conversation as resolved.

int firstCommentIndex = tokenIndex;
int currentLine = tokens[tokenIndex].Extent.StartLineNumber;
int precedingIndex = tokenIndex - 1;
Expand All @@ -369,6 +393,7 @@ private static int GetCommentRunIndentationOffset(
precedingIndex--;
if (precedingIndex < 0 ||
tokens[precedingIndex].Kind != TokenKind.Comment ||
HasCodeAfterCommentOnSameLine(tokens, precedingIndex) ||
tokens[precedingIndex].Extent.EndLineNumber != currentLine - 1 ||
(precedingIndex > 0 &&
tokens[precedingIndex - 1].Kind != TokenKind.NewLine &&
Expand All @@ -393,6 +418,7 @@ private static int GetCommentRunIndentationOffset(
(tokens[nextCommentIndex - 1].Kind == TokenKind.NewLine ||
tokens[nextCommentIndex - 1].Kind == TokenKind.LineContinuation) &&
tokens[nextCommentIndex].Kind == TokenKind.Comment &&
!HasCodeAfterCommentOnSameLine(tokens, nextCommentIndex) &&
tokens[nextCommentIndex].Extent.StartLineNumber == tokens[nextCommentIndex - 2].Extent.EndLineNumber + 1;
nextCommentIndex += 2)
{
Expand All @@ -405,6 +431,16 @@ private static int GetCommentRunIndentationOffset(
leastIndentedColumn;
}

private static bool HasCodeAfterCommentOnSameLine(Token[] tokens, int commentIndex)
{
Token nextToken = NextTokenIgnoringComments(tokens, commentIndex);
return nextToken != null &&
nextToken.Kind != TokenKind.NewLine &&
nextToken.Kind != TokenKind.LineContinuation &&
nextToken.Kind != TokenKind.EndOfInput &&
nextToken.Extent.StartLineNumber <= tokens[commentIndex].Extent.EndLineNumber;
}

/// <summary>
/// Scans forward from the current opener to the end of the line.
/// Returns true if there is at least one unclosed opener when
Expand Down
49 changes: 49 additions & 0 deletions Tests/Rules/UseConsistentIndentation.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,25 @@ Write-Output `
Should -Be $scriptDefinition
}

It 'Should not include a block comment followed by code in a comment run' {
$scriptDefinition = @'
if ($a) {
# comment
<# note #> Write-Output 'value'
}
'@
$expectedScriptDefinition = @'
if ($a) {
# comment
<# note #> Write-Output 'value'
}
'@
$settings.Rules.PSUseConsistentIndentation.PipelineIndentation = 'NoIndentation'

Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings $settings |
Should -Be $expectedScriptDefinition
}

It 'Should preserve a comment run containing an internal line continuation with <Kind> indentation' -TestCases @(
@{ Kind = 'space'; Indentation = ' ' }
@{ Kind = 'tab'; Indentation = "`t" }
Expand Down Expand Up @@ -674,6 +693,36 @@ bar
Invoke-Formatter -ScriptDefinition $def -Settings $settings | Should -Be $def
}

It "Should preserve pipeline indentation across a comment line continuation with PipelineIndentation None" {
$def = @'
foo |
<# comment #> `
<# note #> bar
'@
$settings.Rules.PSUseConsistentIndentation.PipelineIndentation = 'None'
Invoke-Formatter -ScriptDefinition $def -Settings $settings | Should -Be $def
}

It "Should not inherit pipeline indentation from a trailing comment with PipelineIndentation None" {
$def = @'
function test {
foo | bar # trailing
<# note #> baz
}
'@
$expected = @'
function test {
foo | bar # trailing
<# note #> baz
}
'@
$settings.Rules.PSUseConsistentIndentation.PipelineIndentation = 'None'
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
$violations.Count | Should -Be 1
Invoke-Formatter -ScriptDefinition $def -Settings $settings | Should -Be $expected
Invoke-Formatter -ScriptDefinition $expected -Settings $settings | Should -Be $expected
}

It "Should indent properly after line continuation (backtick) character with pipeline" {
$def = @'
foo |
Expand Down
Loading