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
18 changes: 18 additions & 0 deletions Rules/UseConsistentIndentation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ private static int GetCommentRunIndentationOffset(
if (precedingIndex < 0 ||
tokens[precedingIndex].Kind != TokenKind.Comment ||
HasCodeAfterCommentOnSameLine(tokens, precedingIndex) ||
HasCodeBeforeCommentOnSameLine(tokens, precedingIndex) ||
tokens[precedingIndex].Extent.EndLineNumber != currentLine - 1)
{
break;
Expand Down Expand Up @@ -520,6 +521,23 @@ private static bool HasCodeAfterCommentOnSameLine(Token[] tokens, int commentInd
nextToken.Extent.StartLineNumber <= commentGroupEndLine;
}

private static bool HasCodeBeforeCommentOnSameLine(Token[] tokens, int commentIndex)
{
int commentStartLine = tokens[commentIndex].Extent.StartLineNumber;
int precedingIndex = commentIndex - 1;
while (precedingIndex >= 0 &&
tokens[precedingIndex].Kind == TokenKind.Comment &&
tokens[precedingIndex].Extent.EndLineNumber >= commentStartLine)
{
precedingIndex--;
}

return precedingIndex >= 0 &&
tokens[precedingIndex].Kind != TokenKind.NewLine &&
tokens[precedingIndex].Kind != TokenKind.LineContinuation &&
tokens[precedingIndex].Extent.EndLineNumber == commentStartLine;
}

/// <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
19 changes: 19 additions & 0 deletions Tests/Rules/UseConsistentIndentation.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,25 @@ if ($a) {
Should -Be $expectedScriptDefinition
}

It 'Should not include a trailing inline comment in the following comment run' {
$scriptDefinition = @'
function test {
$value = 1 # trailing
# standalone
}
'@
$expectedScriptDefinition = @'
function test {
$value = 1 # trailing
# standalone
}
'@
$settings.Rules.PSUseConsistentIndentation.PipelineIndentation = 'NoIndentation'

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

It 'Should not include a multiline block-comment group followed by code in a comment run' {
$scriptDefinition = @'
if ($a) {
Expand Down
Loading