Skip to content
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

Fix edge case of PSUseConsistentIndentation for non-default value (IncreaseIndentationForFirstPipeline/IncreaseIndentationAfterEveryPipeline) #1423

Merged
merged 5 commits into from
Apr 4, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
19 changes: 18 additions & 1 deletion Rules/UseConsistentIndentation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
}
if (pipelineIndentationStyle == PipelineIndentationStyle.IncreaseIndentationForFirstPipeline)
{
bool isFirstPipeInPipeline = pipelineAsts.Any(pipelineAst => PositionIsEqual(((PipelineAst)pipelineAst).PipelineElements[0].Extent.EndScriptPosition, tokens[tokenIndex - 1].Extent.EndScriptPosition));
bool isFirstPipeInPipeline = pipelineAsts.Any(pipelineAst =>
PositionIsEqual(LastPipeOnFirstLineWithPipeUsage((PipelineAst)pipelineAst).Extent.EndScriptPosition,
tokens[tokenIndex - 1].Extent.EndScriptPosition));
if (isFirstPipeInPipeline)
{
AddViolation(token, indentationLevel++, diagnosticRecords, ref onNewLine);
Expand Down Expand Up @@ -255,6 +257,21 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
return diagnosticRecords;
}

private static CommandBaseAst LastPipeOnFirstLineWithPipeUsage(PipelineAst pipelineAst)
{
CommandBaseAst lastPipeOnFirstLineWithPipeUsage = pipelineAst.PipelineElements[0];
foreach (CommandBaseAst pipelineElement in pipelineAst.PipelineElements.Skip(1))
bergmeister marked this conversation as resolved.
Show resolved Hide resolved
{
if (pipelineElement.Extent.StartLineNumber == pipelineAst.PipelineElements[0].Extent.StartLineNumber ||
pipelineElement.Extent.StartLineNumber == pipelineAst.PipelineElements[0].Extent.EndLineNumber ||
pipelineElement.Extent.EndLineNumber == pipelineAst.PipelineElements[0].Extent.EndLineNumber)
{
lastPipeOnFirstLineWithPipeUsage = pipelineElement;
}
}
return lastPipeOnFirstLineWithPipeUsage;
}

private static bool PositionIsEqual(IScriptPosition position1, IScriptPosition position2)
{
return position1.ColumnNumber == position2.ColumnNumber &&
Expand Down
42 changes: 42 additions & 0 deletions Tests/Rules/UseConsistentIndentation.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,48 @@ baz
Test-CorrectionExtentFromContent @params
}

It "Should indent pipelines correctly using <PipelineIndentation> option" -TestCases @(
@{
PipelineIndentation = 'IncreaseIndentationForFirstPipeline'
ExpectCorrection = $true
},
@{
PipelineIndentation = 'IncreaseIndentationAfterEveryPipeline'
ExpectCorrection = $true
},
@{
PipelineIndentation = 'NoIndentation'
ExpectCorrection = $false
}
@{
PipelineIndentation = 'None'
ExpectCorrection = $false
}
) {
Param([string] $PipelineIndentation, [bool] $ExpectCorrection)
$def = @'
foo | bar |
baz
'@
$settings.Rules.PSUseConsistentIndentation.PipelineIndentation = $PipelineIndentation
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
if ($ExpectCorrection) {
$violations.Count | Should -Be 1
$params = @{
RawContent = $def
DiagnosticRecord = $violations[0]
CorrectionsCount = 1
ViolationText = "baz"
CorrectionText = (New-Object -TypeName String -ArgumentList $indentationUnit, ($indentationSize * 1)) + 'baz'
Copy link
Contributor

Choose a reason for hiding this comment

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

this could also be written without using New-Object:

CorrectionText = ($indentationUnit * $indentationSize) + 'baz'

it's sort of a toss up as to which one is more readable

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good idea, thanks, I applied it, I could even remove the parenthesis because multiplication takes precedence over addition

}
Test-CorrectionExtentFromContent @params
}
else
{
$violations | Should -BeNullOrEmpty
}
}

It 'Should preserve script when using PipelineIndentation None' -TestCases @(
@{ IdempotentScriptDefinition = @'
foo |
Expand Down