Skip to content

Fix bug with PipelineIndentationStyle.None where break instead of continue statement was used #1497

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 11, 2020
40 changes: 37 additions & 3 deletions Rules/UseConsistentIndentation.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
Expand Down Expand Up @@ -220,14 +220,19 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
}
}

bool lineHasPipelineBeforeToken = LineHasPipelineBeforeToken(tokens, tokenIndex, token);
if (pipelineIndentationStyle == PipelineIndentationStyle.None && PreviousLineEndedWithPipe(tokens, tokenIndex, token))
{
continue;
}

bool lineHasPipelineBeforeToken = LineHasPipelineBeforeToken(tokens, tokenIndex, token);
AddViolation(token, tempIndentationLevel, diagnosticRecords, ref onNewLine, lineHasPipelineBeforeToken);
}
break;
}

if (pipelineIndentationStyle == PipelineIndentationStyle.None) { break; }
if (pipelineIndentationStyle == PipelineIndentationStyle.None) { continue; }

// Check if the current token matches the end of a PipelineAst
PipelineAst matchingPipeLineAstEnd = MatchingPipelineAstEnd(pipelineAsts, ref minimumPipelineAstIndex, token);
if (matchingPipeLineAstEnd == null)
Expand Down Expand Up @@ -284,6 +289,35 @@ private static bool PipelineIsFollowedByNewlineOrLineContinuation(Token[] tokens
return false;
}

private static bool PreviousLineEndedWithPipe(Token[] tokens, int tokenIndex, Token token)
{
if (tokenIndex < 2 || token.Extent.StartLineNumber == 1)
{
return false;
}

int searchIndex = tokenIndex - 2;
int searchLine;
do
{
searchLine = tokens[searchIndex].Extent.StartLineNumber;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh! My switch suggestion got lost! Ugh, too much context switching... Anyway, not an issue

Copy link
Collaborator Author

@bergmeister bergmeister Jun 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought I addressed your comment here by not initializing it: #1497 (comment)
Or did you mean a switch around tokens[searchIndex].Kind? I don't see too much value in it tbh. Switch statements don't look that much cleaner for simple cases like that IMHO because of their ceremony that requires additional indentation layer, break statements, etc..
Otherwise we can always create a follow up PR

if (tokens[searchIndex].Kind == TokenKind.Comment)
{
searchIndex--;
}
else if (tokens[searchIndex].Kind == TokenKind.Pipe)
{
return true;
}
else
{
break;
}
} while (searchLine == token.Extent.StartLineNumber - 1 && searchIndex >= 0);

return false;
}

private static bool LineHasPipelineBeforeToken(Token[] tokens, int tokenIndex, Token token)
{
int searchIndex = tokenIndex;
Expand Down
34 changes: 34 additions & 0 deletions Tests/Rules/UseConsistentIndentation.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,40 @@ baz
Test-CorrectionExtentFromContent @params
}

It "Should indent hashtable correctly using <PipelineIndentation> option" -TestCases @(
@{
PipelineIndentation = 'IncreaseIndentationForFirstPipeline'
},
@{
PipelineIndentation = 'IncreaseIndentationAfterEveryPipeline'
},
@{
PipelineIndentation = 'NoIndentation'
}
@{
PipelineIndentation = 'None'
}
) {
Param([string] $PipelineIndentation)
$scriptDefinition = @'
@{
foo = "value1"
bar = "value2"
}
'@
$settings = @{
IncludeRules = @('PSUseConsistentIndentation')
Rules = @{ PSUseConsistentIndentation = @{ Enable = $true; PipelineIndentation = $PipelineIndentation } }
}
Invoke-Formatter -Settings $settings -ScriptDefinition $scriptDefinition | Should -Be @'
@{
foo = "value1"
bar = "value2"
}
'@

}

It "Should indent pipelines correctly using <PipelineIndentation> option" -TestCases @(
@{
PipelineIndentation = 'IncreaseIndentationForFirstPipeline'
Expand Down