From a0841cd961dc03823eeb69b8b8188e40bc869a14 Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Fri, 25 Oct 2019 18:59:01 +0100 Subject: [PATCH 1/2] Fix complex case when PipelineIndentation is not set to default (NoIndentation) --- Rules/UseConsistentIndentation.cs | 13 +++++++------ Tests/Rules/UseConsistentIndentation.tests.ps1 | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/Rules/UseConsistentIndentation.cs b/Rules/UseConsistentIndentation.cs index 1da472bf5..c1567f8f6 100644 --- a/Rules/UseConsistentIndentation.cs +++ b/Rules/UseConsistentIndentation.cs @@ -129,6 +129,7 @@ public override IEnumerable AnalyzeScript(Ast ast, string file var tokens = Helper.Instance.Tokens; var diagnosticRecords = new List(); var indentationLevel = 0; + var currentIndenationLevelIncreaseDueToPipelines = 0; var onNewLine = true; var pipelineAsts = ast.FindAll(testAst => testAst is PipelineAst && (testAst as PipelineAst).PipelineElements.Count > 1, true); for (int tokenIndex = 0; tokenIndex < tokens.Length; tokenIndex++) @@ -160,6 +161,7 @@ public override IEnumerable AnalyzeScript(Ast ast, string file if (pipelineIndentationStyle == PipelineIndentationStyle.IncreaseIndentationAfterEveryPipeline) { AddViolation(token, indentationLevel++, diagnosticRecords, ref onNewLine); + currentIndenationLevelIncreaseDueToPipelines++; break; } if (pipelineIndentationStyle == PipelineIndentationStyle.IncreaseIndentationForFirstPipeline) @@ -168,6 +170,7 @@ public override IEnumerable AnalyzeScript(Ast ast, string file if (isFirstPipeInPipeline) { AddViolation(token, indentationLevel++, diagnosticRecords, ref onNewLine); + currentIndenationLevelIncreaseDueToPipelines++; } } break; @@ -238,13 +241,11 @@ public override IEnumerable AnalyzeScript(Ast ast, string file continue; } - if (pipelineIndentationStyle == PipelineIndentationStyle.IncreaseIndentationForFirstPipeline) + if (pipelineIndentationStyle == PipelineIndentationStyle.IncreaseIndentationForFirstPipeline || + pipelineIndentationStyle == PipelineIndentationStyle.IncreaseIndentationAfterEveryPipeline) { - indentationLevel = ClipNegative(indentationLevel - 1); - } - else if (pipelineIndentationStyle == PipelineIndentationStyle.IncreaseIndentationAfterEveryPipeline) - { - indentationLevel = ClipNegative(indentationLevel - (matchingPipeLineAstEnd.PipelineElements.Count - 1)); + indentationLevel = ClipNegative(indentationLevel - currentIndenationLevelIncreaseDueToPipelines); + currentIndenationLevelIncreaseDueToPipelines = 0; } } diff --git a/Tests/Rules/UseConsistentIndentation.tests.ps1 b/Tests/Rules/UseConsistentIndentation.tests.ps1 index 2cce7dd7c..1b6c8e70e 100644 --- a/Tests/Rules/UseConsistentIndentation.tests.ps1 +++ b/Tests/Rules/UseConsistentIndentation.tests.ps1 @@ -277,6 +277,23 @@ Describe 'describe' { $settings.Rules.PSUseConsistentIndentation.PipelineIndentation = $PipelineIndentation Invoke-Formatter -ScriptDefinition $idempotentScriptDefinition -Settings $settings | Should -Be $idempotentScriptDefinition } + It "Should preserve script when using PipelineIndentation for complex multi-line pipeline" -TestCases @( + @{ PipelineIndentation = 'IncreaseIndentationForFirstPipeline' } + @{ PipelineIndentation = 'IncreaseIndentationAfterEveryPipeline' } + @{ PipelineIndentation = 'NoIndentation' } + ) { + param ($PipelineIndentation) + $idempotentScriptDefinition = @' +function foo { + bar | baz { + Get-Item + } | Invoke-Item + $iShouldStayAtTheSameIndentationLevel +} +'@ + $settings.Rules.PSUseConsistentIndentation.PipelineIndentation = $PipelineIndentation + Invoke-Formatter -ScriptDefinition $idempotentScriptDefinition -Settings $settings | Should -Be $idempotentScriptDefinition + } It "Should indent pipelines correctly using NoIndentation option" { $def = @' From 67d3d51bac2c17a4a95b901ad64f084a26d1abe5 Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Mon, 11 Nov 2019 22:24:47 +0000 Subject: [PATCH 2/2] re-trigger ci