Skip to content

Commit e650b46

Browse files
authored
Fix edge case of UseConsistentIndentation where child pipeline is on the same line as hashtable (#1838)
* Fix edge case of UseConsistentIndentation where child pipeline being on the same line as hashtable * add test cases * include multiple lines in test case
1 parent bcd078a commit e650b46

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

Rules/UseConsistentIndentation.cs

+3-5
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
133133
var currentIndenationLevelIncreaseDueToPipelines = 0;
134134
var onNewLine = true;
135135
var pipelineAsts = ast.FindAll(testAst => testAst is PipelineAst && (testAst as PipelineAst).PipelineElements.Count > 1, true).ToList();
136-
int minimumPipelineAstIndex = 0;
137136
/*
138137
When an LParen and LBrace are on the same line, it can lead to too much de-indentation.
139138
In order to prevent the RParen code from de-indenting too much, we keep a stack of when we skipped the indentation
@@ -273,7 +272,7 @@ caused by tokens that require a closing RParen (which are LParen, AtParen and Do
273272
if (pipelineIndentationStyle == PipelineIndentationStyle.None) { continue; }
274273

275274
// Check if the current token matches the end of a PipelineAst
276-
PipelineAst matchingPipeLineAstEnd = MatchingPipelineAstEnd(pipelineAsts, ref minimumPipelineAstIndex, token);
275+
PipelineAst matchingPipeLineAstEnd = MatchingPipelineAstEnd(pipelineAsts, token);
277276
if (matchingPipeLineAstEnd == null)
278277
{
279278
continue;
@@ -412,10 +411,10 @@ private static CommandBaseAst LastPipeOnFirstLineWithPipeUsage(PipelineAst pipel
412411
return lastPipeOnFirstLineWithPipeUsage;
413412
}
414413

415-
private static PipelineAst MatchingPipelineAstEnd(List<Ast> pipelineAsts, ref int minimumPipelineAstIndex, Token token)
414+
private static PipelineAst MatchingPipelineAstEnd(List<Ast> pipelineAsts, Token token)
416415
{
417416
PipelineAst matchingPipeLineAstEnd = null;
418-
for (int i = minimumPipelineAstIndex; i < pipelineAsts.Count; i++)
417+
for (int i = 0; i < pipelineAsts.Count; i++)
419418
{
420419
if (pipelineAsts[i].Extent.EndScriptPosition.LineNumber > token.Extent.EndScriptPosition.LineNumber)
421420
{
@@ -425,7 +424,6 @@ private static PipelineAst MatchingPipelineAstEnd(List<Ast> pipelineAsts, ref in
425424
if (PositionIsEqual(pipelineAsts[i].Extent.EndScriptPosition, token.Extent.EndScriptPosition))
426425
{
427426
matchingPipeLineAstEnd = pipelineAsts[i] as PipelineAst;
428-
minimumPipelineAstIndex = i;
429427
break;
430428
}
431429
}

Tests/Rules/UseConsistentIndentation.tests.ps1

+21-1
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,13 @@ foo |
395395
@{ IdempotentScriptDefinition = @'
396396
foo |
397397
bar -Parameter1
398+
'@
399+
},
400+
@{ IdempotentScriptDefinition = @'
401+
Get-TransportRule |
402+
Where-Object @{ $_.name -match "a"} |
403+
Select-Object @{ E = $SenderDomainIs | Sort-Object }
404+
Foreach-Object { $_.FullName }
398405
'@
399406
}
400407
) {
@@ -404,6 +411,20 @@ foo |
404411
Invoke-Formatter -ScriptDefinition $IdempotentScriptDefinition -Settings $settings | Should -Be $idempotentScriptDefinition
405412
}
406413

414+
It 'Should preserve script when using PipelineIndentation IncreaseIndentationAfterEveryPipeline' -TestCases @(
415+
@{ PipelineIndentation = 'IncreaseIndentationForFirstPipeline' }
416+
@{ PipelineIndentation = 'IncreaseIndentationAfterEveryPipeline' }
417+
) {
418+
param ($PipelineIndentation)
419+
$IdempotentScriptDefinition = @'
420+
Get-TransportRule |
421+
Select-Object @{ Key = $SenderDomainIs | Sort-Object }
422+
baz
423+
'@
424+
$settings.Rules.PSUseConsistentIndentation.PipelineIndentation = $PipelineIndentation
425+
Invoke-Formatter -ScriptDefinition $IdempotentScriptDefinition -Settings $settings | Should -Be $idempotentScriptDefinition
426+
}
427+
407428
It "Should preserve script when using PipelineIndentation <PipelineIndentation>" -TestCases @(
408429
@{ PipelineIndentation = 'IncreaseIndentationForFirstPipeline' }
409430
@{ PipelineIndentation = 'IncreaseIndentationAfterEveryPipeline' }
@@ -497,7 +518,6 @@ foo |
497518
Test-CorrectionExtentFromContent @params
498519
}
499520

500-
501521
It "Should indent properly after line continuation (backtick) character with pipeline" {
502522
$def = @'
503523
foo |

0 commit comments

Comments
 (0)