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; } } diff --git a/Tests/Rules/UseConsistentIndentation.tests.ps1 b/Tests/Rules/UseConsistentIndentation.tests.ps1 index 9a0d195eb..1b556baed 100644 --- a/Tests/Rules/UseConsistentIndentation.tests.ps1 +++ b/Tests/Rules/UseConsistentIndentation.tests.ps1 @@ -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 } '@ } ) { @@ -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 " -TestCases @( @{ PipelineIndentation = 'IncreaseIndentationForFirstPipeline' } @{ PipelineIndentation = 'IncreaseIndentationAfterEveryPipeline' } @@ -497,7 +518,6 @@ foo | Test-CorrectionExtentFromContent @params } - It "Should indent properly after line continuation (backtick) character with pipeline" { $def = @' foo |