From 231b398ce39698fe2a218b7e42970cefdf17cb1b Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Mon, 20 Jan 2020 07:36:57 +0000 Subject: [PATCH 1/2] Add PipelineIndentationStyle.None option for scenarios where indentation is custom, inconsistent or the user does not like any of the 3 pipeline indentation styles --- RuleDocumentation/UseConsistentIndentation.md | 1 + Rules/UseConsistentIndentation.cs | 5 ++++- .../Rules/UseConsistentIndentation.tests.ps1 | 22 +++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/RuleDocumentation/UseConsistentIndentation.md b/RuleDocumentation/UseConsistentIndentation.md index deca041ee..81ff32ca8 100644 --- a/RuleDocumentation/UseConsistentIndentation.md +++ b/RuleDocumentation/UseConsistentIndentation.md @@ -53,6 +53,7 @@ foo | bar | baz ``` +- None: Do not change any existing pipeline indentation. #### Kind: string (Default value is `space`) diff --git a/Rules/UseConsistentIndentation.cs b/Rules/UseConsistentIndentation.cs index c1567f8f6..2c9f7af6d 100644 --- a/Rules/UseConsistentIndentation.cs +++ b/Rules/UseConsistentIndentation.cs @@ -90,7 +90,8 @@ private enum PipelineIndentationStyle { IncreaseIndentationForFirstPipeline, IncreaseIndentationAfterEveryPipeline, - NoIndentation + NoIndentation, + None } // TODO make this configurable @@ -152,6 +153,7 @@ public override IEnumerable AnalyzeScript(Ast ast, string file break; case TokenKind.Pipe: + if (pipelineIndentationStyle == PipelineIndentationStyle.None) { break; } bool pipelineIsFollowedByNewlineOrLineContinuation = tokenIndex < tokens.Length - 1 && tokenIndex > 0 && (tokens[tokenIndex + 1].Kind == TokenKind.NewLine || tokens[tokenIndex + 1].Kind == TokenKind.LineContinuation); if (!pipelineIsFollowedByNewlineOrLineContinuation) @@ -225,6 +227,7 @@ public override IEnumerable AnalyzeScript(Ast ast, string file break; } + if (pipelineIndentationStyle == PipelineIndentationStyle.None) { break; } // 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; diff --git a/Tests/Rules/UseConsistentIndentation.tests.ps1 b/Tests/Rules/UseConsistentIndentation.tests.ps1 index 1b6c8e70e..5f3fd9e36 100644 --- a/Tests/Rules/UseConsistentIndentation.tests.ps1 +++ b/Tests/Rules/UseConsistentIndentation.tests.ps1 @@ -225,10 +225,29 @@ baz Test-CorrectionExtentFromContent @params } + It 'Should preserve script when using PipelineIndentation None' -TestCases @( + @{ IdempotentScriptDefinition = @' +foo | +bar +'@ + } + @{ IdempotentScriptDefinition = @' +foo | + bar +'@ + } + ) { + param ($IdempotentScriptDefinition) + + $settings.Rules.PSUseConsistentIndentation.PipelineIndentation = 'None' + Invoke-Formatter -ScriptDefinition $IdempotentScriptDefinition -Settings $settings | Should -Be $idempotentScriptDefinition + } + It "Should preserve script when using PipelineIndentation " -TestCases @( @{ PipelineIndentation = 'IncreaseIndentationForFirstPipeline' } @{ PipelineIndentation = 'IncreaseIndentationAfterEveryPipeline' } @{ PipelineIndentation = 'NoIndentation' } + @{ PipelineIndentation = 'None' } ) { param ($PipelineIndentation) $idempotentScriptDefinition = @' @@ -246,6 +265,7 @@ function hello { @{ PipelineIndentation = 'IncreaseIndentationForFirstPipeline' } @{ PipelineIndentation = 'IncreaseIndentationAfterEveryPipeline' } @{ PipelineIndentation = 'NoIndentation' } + @{ PipelineIndentation = 'None' } ) { param ($PipelineIndentation) $idempotentScriptDefinition = @' @@ -264,6 +284,7 @@ function foo { @{ PipelineIndentation = 'IncreaseIndentationForFirstPipeline' } @{ PipelineIndentation = 'IncreaseIndentationAfterEveryPipeline' } @{ PipelineIndentation = 'NoIndentation' } + @{ PipelineIndentation = 'None' } ) { param ($PipelineIndentation) $idempotentScriptDefinition = @' @@ -281,6 +302,7 @@ Describe 'describe' { @{ PipelineIndentation = 'IncreaseIndentationForFirstPipeline' } @{ PipelineIndentation = 'IncreaseIndentationAfterEveryPipeline' } @{ PipelineIndentation = 'NoIndentation' } + @{ PipelineIndentation = 'None' } ) { param ($PipelineIndentation) $idempotentScriptDefinition = @' From e19e2d60be548eeddf9c2e2461e1cc8b11150373 Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Mon, 20 Jan 2020 09:48:58 +0000 Subject: [PATCH 2/2] re-trigger ci