Skip to content

Fix edge case of UseConsistentIndentation where child pipeline is on the same line as hashtable #1838

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 3 commits into from
Sep 22, 2022
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
8 changes: 3 additions & 5 deletions Rules/UseConsistentIndentation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
var currentIndenationLevelIncreaseDueToPipelines = 0;
var onNewLine = true;
var pipelineAsts = ast.FindAll(testAst => testAst is PipelineAst && (testAst as PipelineAst).PipelineElements.Count > 1, true).ToList();
int minimumPipelineAstIndex = 0;
/*
When an LParen and LBrace are on the same line, it can lead to too much de-indentation.
In order to prevent the RParen code from de-indenting too much, we keep a stack of when we skipped the indentation
Expand Down Expand Up @@ -273,7 +272,7 @@ caused by tokens that require a closing RParen (which are LParen, AtParen and Do
if (pipelineIndentationStyle == PipelineIndentationStyle.None) { continue; }

// Check if the current token matches the end of a PipelineAst
PipelineAst matchingPipeLineAstEnd = MatchingPipelineAstEnd(pipelineAsts, ref minimumPipelineAstIndex, token);
PipelineAst matchingPipeLineAstEnd = MatchingPipelineAstEnd(pipelineAsts, token);
if (matchingPipeLineAstEnd == null)
{
continue;
Expand Down Expand Up @@ -412,10 +411,10 @@ private static CommandBaseAst LastPipeOnFirstLineWithPipeUsage(PipelineAst pipel
return lastPipeOnFirstLineWithPipeUsage;
}

private static PipelineAst MatchingPipelineAstEnd(List<Ast> pipelineAsts, ref int minimumPipelineAstIndex, Token token)
private static PipelineAst MatchingPipelineAstEnd(List<Ast> pipelineAsts, Token token)
{
PipelineAst matchingPipeLineAstEnd = null;
for (int i = minimumPipelineAstIndex; i < pipelineAsts.Count; i++)
for (int i = 0; i < pipelineAsts.Count; i++)
{
if (pipelineAsts[i].Extent.EndScriptPosition.LineNumber > token.Extent.EndScriptPosition.LineNumber)
{
Expand All @@ -425,7 +424,6 @@ private static PipelineAst MatchingPipelineAstEnd(List<Ast> pipelineAsts, ref in
if (PositionIsEqual(pipelineAsts[i].Extent.EndScriptPosition, token.Extent.EndScriptPosition))
{
matchingPipeLineAstEnd = pipelineAsts[i] as PipelineAst;
minimumPipelineAstIndex = i;
break;
}
}
Expand Down
22 changes: 21 additions & 1 deletion Tests/Rules/UseConsistentIndentation.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,13 @@ foo |
@{ IdempotentScriptDefinition = @'
foo |
bar -Parameter1
'@
},
@{ IdempotentScriptDefinition = @'
Get-TransportRule |
Where-Object @{ $_.name -match "a"} |
Select-Object @{ E = $SenderDomainIs | Sort-Object }
Foreach-Object { $_.FullName }
'@
}
) {
Expand All @@ -404,6 +411,20 @@ foo |
Invoke-Formatter -ScriptDefinition $IdempotentScriptDefinition -Settings $settings | Should -Be $idempotentScriptDefinition
}

It 'Should preserve script when using PipelineIndentation IncreaseIndentationAfterEveryPipeline' -TestCases @(
@{ PipelineIndentation = 'IncreaseIndentationForFirstPipeline' }
@{ PipelineIndentation = 'IncreaseIndentationAfterEveryPipeline' }
) {
param ($PipelineIndentation)
$IdempotentScriptDefinition = @'
Get-TransportRule |
Select-Object @{ Key = $SenderDomainIs | Sort-Object }
baz
'@
$settings.Rules.PSUseConsistentIndentation.PipelineIndentation = $PipelineIndentation
Invoke-Formatter -ScriptDefinition $IdempotentScriptDefinition -Settings $settings | Should -Be $idempotentScriptDefinition
}

It "Should preserve script when using PipelineIndentation <PipelineIndentation>" -TestCases @(
@{ PipelineIndentation = 'IncreaseIndentationForFirstPipeline' }
@{ PipelineIndentation = 'IncreaseIndentationAfterEveryPipeline' }
Expand Down Expand Up @@ -497,7 +518,6 @@ foo |
Test-CorrectionExtentFromContent @params
}


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