From c921ea4236bbc676f540d0413575b397f6a79590 Mon Sep 17 00:00:00 2001 From: "Christoph Bergmeister (MVP)" Date: Sun, 24 Mar 2019 03:18:20 +0000 Subject: [PATCH 1/3] Fix issue whereby single-line pipeline reduces indentation level incorrectly (problem only showed up when there was already existing indentation due to ClipNegative --- Rules/UseConsistentIndentation.cs | 24 ++++++++++++------- .../Rules/UseConsistentIndentation.tests.ps1 | 17 +++++++++++++ 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/Rules/UseConsistentIndentation.cs b/Rules/UseConsistentIndentation.cs index 06724c3b7..aa210ef97 100644 --- a/Rules/UseConsistentIndentation.cs +++ b/Rules/UseConsistentIndentation.cs @@ -220,16 +220,22 @@ 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) + if (matchingPipeLineAstEnd == null) { - if (pipelineIndentationStyle == PipelineIndentationStyle.IncreaseIndentationForFirstPipeline) - { - indentationLevel = ClipNegative(indentationLevel - 1); - } - else if (pipelineIndentationStyle == PipelineIndentationStyle.IncreaseIndentationAfterEveryPipeline) - { - indentationLevel = ClipNegative(indentationLevel - (matchingPipeLineAstEnd.PipelineElements.Count - 1)); - } + break; + } + var pipelineSpansOnlyOneLine = matchingPipeLineAstEnd.Extent.StartLineNumber == matchingPipeLineAstEnd.Extent.EndLineNumber; + if (pipelineSpansOnlyOneLine) + { + break; + } + if (pipelineIndentationStyle == PipelineIndentationStyle.IncreaseIndentationForFirstPipeline) + { + indentationLevel = ClipNegative(indentationLevel - 1); + } + else if (pipelineIndentationStyle == PipelineIndentationStyle.IncreaseIndentationAfterEveryPipeline) + { + indentationLevel = ClipNegative(indentationLevel - (matchingPipeLineAstEnd.PipelineElements.Count - 1)); } } diff --git a/Tests/Rules/UseConsistentIndentation.tests.ps1 b/Tests/Rules/UseConsistentIndentation.tests.ps1 index 408423dc7..986b900e8 100644 --- a/Tests/Rules/UseConsistentIndentation.tests.ps1 +++ b/Tests/Rules/UseConsistentIndentation.tests.ps1 @@ -174,6 +174,23 @@ baz Test-CorrectionExtentFromContent @params } + It "Should preserve script when using PipelineIndentation " -TestCases @( + @{ PipelineIndentation = 'IncreaseIndentationForFirstPipeline' } + @{ PipelineIndentation = 'IncreaseIndentationAfterEveryPipeline' } + @{ PipelineIndentation = 'NoIndentation' } + ) { + param ($PipelineIndentation) + $idempotentScriptDefinition = @' +function hello { + if ($true) { + "hello" | Out-Host + } +} +'@ + $settings.Rules.PSUseConsistentIndentation.PipelineIndentation = $PipelineIndentation + Invoke-Formatter -ScriptDefinition $idempotentScriptDefinition -Settings $settings | Should -Be $idempotentScriptDefinition + } + It "Should indent pipelines correctly using NoIndentation option" { $def = @' foo | From c72345c8a9ffa965d1ca799909d9c46eeedcbce7 Mon Sep 17 00:00:00 2001 From: Robert Holt Date: Mon, 25 Mar 2019 21:01:25 +0000 Subject: [PATCH 2/3] Apply suggestions from code review Co-Authored-By: bergmeister --- Rules/UseConsistentIndentation.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Rules/UseConsistentIndentation.cs b/Rules/UseConsistentIndentation.cs index aa210ef97..6bee39b6f 100644 --- a/Rules/UseConsistentIndentation.cs +++ b/Rules/UseConsistentIndentation.cs @@ -220,15 +220,18 @@ 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) { break; } - var pipelineSpansOnlyOneLine = matchingPipeLineAstEnd.Extent.StartLineNumber == matchingPipeLineAstEnd.Extent.EndLineNumber; + + bool pipelineSpansOnlyOneLine = matchingPipeLineAstEnd.Extent.StartLineNumber == matchingPipeLineAstEnd.Extent.EndLineNumber; if (pipelineSpansOnlyOneLine) { break; } + if (pipelineIndentationStyle == PipelineIndentationStyle.IncreaseIndentationForFirstPipeline) { indentationLevel = ClipNegative(indentationLevel - 1); From 78e91f244ea6f5051660556937355c36746298b1 Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Wed, 27 Mar 2019 21:36:49 +0000 Subject: [PATCH 3/3] Fix tests by replacing break with continue (forgot I was inside for loop and not switch statement), simple mistake --- Rules/UseConsistentIndentation.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Rules/UseConsistentIndentation.cs b/Rules/UseConsistentIndentation.cs index 6bee39b6f..7b8ba51a4 100644 --- a/Rules/UseConsistentIndentation.cs +++ b/Rules/UseConsistentIndentation.cs @@ -223,13 +223,13 @@ public override IEnumerable AnalyzeScript(Ast ast, string file if (matchingPipeLineAstEnd == null) { - break; + continue; } bool pipelineSpansOnlyOneLine = matchingPipeLineAstEnd.Extent.StartLineNumber == matchingPipeLineAstEnd.Extent.EndLineNumber; if (pipelineSpansOnlyOneLine) { - break; + continue; } if (pipelineIndentationStyle == PipelineIndentationStyle.IncreaseIndentationForFirstPipeline)