From 1a7119a73d1a132d10c740ed5726ca10e750af5f Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Sat, 15 Jun 2019 22:30:38 +0100 Subject: [PATCH 1/3] Fix indentation problem when PipelineIndentationStyle is set to IncreaseIndentationForFirstPipeline or IncreaseIndentationAfterEveryPipeline The determination if the pipelineAst is on one line needs to be tweaked to include the case where pipelines are on the same line but a multi-line expressions follows --- Rules/UseConsistentIndentation.cs | 7 +++---- Tests/Rules/UseConsistentIndentation.tests.ps1 | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/Rules/UseConsistentIndentation.cs b/Rules/UseConsistentIndentation.cs index 1203bdf64..b43907a9a 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 pipelinesSpansOnlyOneLine = matchingPipeLineAstEnd.PipelineElements[0].Extent.StartLineNumber == + matchingPipeLineAstEnd.PipelineElements[matchingPipeLineAstEnd.PipelineElements.Count-1].Extent.StartLineNumber; + if (pipelinesSpansOnlyOneLine) { 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 | From 2b8f22068478baa11c45ec2aa6f2c266dd3f5fe1 Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Sat, 15 Jun 2019 22:58:20 +0100 Subject: [PATCH 2/3] restart ci From 063d42fc953f8af1e3ad3a406b3d63d947b203f7 Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Sat, 6 Jul 2019 12:05:12 +0100 Subject: [PATCH 3/3] Apply variables naming suggestion --- Rules/UseConsistentIndentation.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Rules/UseConsistentIndentation.cs b/Rules/UseConsistentIndentation.cs index b43907a9a..ecf726362 100644 --- a/Rules/UseConsistentIndentation.cs +++ b/Rules/UseConsistentIndentation.cs @@ -224,9 +224,9 @@ public override IEnumerable AnalyzeScript(Ast ast, string file { continue; } - bool pipelinesSpansOnlyOneLine = matchingPipeLineAstEnd.PipelineElements[0].Extent.StartLineNumber == + bool pipelinesSpanOnlyOneLine = matchingPipeLineAstEnd.PipelineElements[0].Extent.StartLineNumber == matchingPipeLineAstEnd.PipelineElements[matchingPipeLineAstEnd.PipelineElements.Count-1].Extent.StartLineNumber; - if (pipelinesSpansOnlyOneLine) + if (pipelinesSpanOnlyOneLine) { continue; }