From 85d1457c675c26473a05e2a5ddc8dc6db9d82585 Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Tue, 6 Sep 2022 16:26:08 +0100 Subject: [PATCH 1/3] Fix edge case of UseConsistentIndentation where child pipeline being on the same line as hashtable --- Rules/UseConsistentIndentation.cs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Rules/UseConsistentIndentation.cs b/Rules/UseConsistentIndentation.cs index 254b5be51..da5cfb8a5 100644 --- a/Rules/UseConsistentIndentation.cs +++ b/Rules/UseConsistentIndentation.cs @@ -133,7 +133,6 @@ public override IEnumerable 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 @@ -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; @@ -412,10 +411,10 @@ private static CommandBaseAst LastPipeOnFirstLineWithPipeUsage(PipelineAst pipel return lastPipeOnFirstLineWithPipeUsage; } - private static PipelineAst MatchingPipelineAstEnd(List pipelineAsts, ref int minimumPipelineAstIndex, Token token) + private static PipelineAst MatchingPipelineAstEnd(List 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) { @@ -425,7 +424,6 @@ private static PipelineAst MatchingPipelineAstEnd(List pipelineAsts, ref in if (PositionIsEqual(pipelineAsts[i].Extent.EndScriptPosition, token.Extent.EndScriptPosition)) { matchingPipeLineAstEnd = pipelineAsts[i] as PipelineAst; - minimumPipelineAstIndex = i; break; } } From dc1898f5a6b58629c085b07d87f6b9531ce3a64e Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Tue, 6 Sep 2022 16:58:33 +0100 Subject: [PATCH 2/3] add test cases --- .../Rules/UseConsistentIndentation.tests.ps1 | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/Tests/Rules/UseConsistentIndentation.tests.ps1 b/Tests/Rules/UseConsistentIndentation.tests.ps1 index 9a0d195eb..ada875537 100644 --- a/Tests/Rules/UseConsistentIndentation.tests.ps1 +++ b/Tests/Rules/UseConsistentIndentation.tests.ps1 @@ -395,6 +395,12 @@ foo | @{ IdempotentScriptDefinition = @' foo | bar -Parameter1 +'@ + }, + @{ IdempotentScriptDefinition = @' +Get-TransportRule | +Select-Object @{ E = $SenderDomainIs | Sort-Object } +baz '@ } ) { @@ -404,6 +410,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 " -TestCases @( @{ PipelineIndentation = 'IncreaseIndentationForFirstPipeline' } @{ PipelineIndentation = 'IncreaseIndentationAfterEveryPipeline' } @@ -497,7 +517,6 @@ foo | Test-CorrectionExtentFromContent @params } - It "Should indent properly after line continuation (backtick) character with pipeline" { $def = @' foo | From 61b0ffa539de3fba30736834d779c46ed689c066 Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Wed, 14 Sep 2022 22:23:24 +0100 Subject: [PATCH 3/3] include multiple lines in test case --- Tests/Rules/UseConsistentIndentation.tests.ps1 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Tests/Rules/UseConsistentIndentation.tests.ps1 b/Tests/Rules/UseConsistentIndentation.tests.ps1 index ada875537..1b556baed 100644 --- a/Tests/Rules/UseConsistentIndentation.tests.ps1 +++ b/Tests/Rules/UseConsistentIndentation.tests.ps1 @@ -399,8 +399,9 @@ foo | }, @{ IdempotentScriptDefinition = @' Get-TransportRule | +Where-Object @{ $_.name -match "a"} | Select-Object @{ E = $SenderDomainIs | Sort-Object } -baz +Foreach-Object { $_.FullName } '@ } ) {