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 = @'