diff --git a/Rules/UseConsistentIndentation.cs b/Rules/UseConsistentIndentation.cs index 1203bdf64..ecf726362 100644 --- a/Rules/UseConsistentIndentation.cs +++ b/Rules/UseConsistentIndentation.cs @@ -220,14 +220,13 @@ public override IEnumerable AnalyzeScript(Ast ast, string file // Check if the current token matches the end of a PipelineAst var matchingPipeLineAstEnd = pipelineAsts.FirstOrDefault(pipelineAst => PositionIsEqual(pipelineAst.Extent.EndScriptPosition, token.Extent.EndScriptPosition)) as PipelineAst; - if (matchingPipeLineAstEnd == null) { continue; } - - bool pipelineSpansOnlyOneLine = matchingPipeLineAstEnd.Extent.StartLineNumber == matchingPipeLineAstEnd.Extent.EndLineNumber; - if (pipelineSpansOnlyOneLine) + bool pipelinesSpanOnlyOneLine = matchingPipeLineAstEnd.PipelineElements[0].Extent.StartLineNumber == + matchingPipeLineAstEnd.PipelineElements[matchingPipeLineAstEnd.PipelineElements.Count-1].Extent.StartLineNumber; + if (pipelinesSpanOnlyOneLine) { continue; } diff --git a/Tests/Rules/UseConsistentIndentation.tests.ps1 b/Tests/Rules/UseConsistentIndentation.tests.ps1 index 986b900e8..b78039a56 100644 --- a/Tests/Rules/UseConsistentIndentation.tests.ps1 +++ b/Tests/Rules/UseConsistentIndentation.tests.ps1 @@ -191,6 +191,24 @@ function hello { Invoke-Formatter -ScriptDefinition $idempotentScriptDefinition -Settings $settings | Should -Be $idempotentScriptDefinition } + It "Should preserve script when using PipelineIndentation " -TestCases @( + @{ PipelineIndentation = 'IncreaseIndentationForFirstPipeline' } + @{ PipelineIndentation = 'IncreaseIndentationAfterEveryPipeline' } + @{ PipelineIndentation = 'NoIndentation' } + ) { + param ($PipelineIndentation) + $idempotentScriptDefinition = @' +function foo { + function bar { + Invoke-Something | ForEach-Object { + } + } +} +'@ + $settings.Rules.PSUseConsistentIndentation.PipelineIndentation = $PipelineIndentation + Invoke-Formatter -ScriptDefinition $idempotentScriptDefinition -Settings $settings | Should -Be $idempotentScriptDefinition + } + It "Should indent pipelines correctly using NoIndentation option" { $def = @' foo |