diff --git a/Rules/UseConsistentIndentation.cs b/Rules/UseConsistentIndentation.cs index 8f17e2462..10b01a600 100644 --- a/Rules/UseConsistentIndentation.cs +++ b/Rules/UseConsistentIndentation.cs @@ -1,4 +1,4 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. using System; @@ -220,14 +220,19 @@ public override IEnumerable AnalyzeScript(Ast ast, string file } } - bool lineHasPipelineBeforeToken = LineHasPipelineBeforeToken(tokens, tokenIndex, token); + if (pipelineIndentationStyle == PipelineIndentationStyle.None && PreviousLineEndedWithPipe(tokens, tokenIndex, token)) + { + continue; + } + bool lineHasPipelineBeforeToken = LineHasPipelineBeforeToken(tokens, tokenIndex, token); AddViolation(token, tempIndentationLevel, diagnosticRecords, ref onNewLine, lineHasPipelineBeforeToken); } break; } - if (pipelineIndentationStyle == PipelineIndentationStyle.None) { break; } + if (pipelineIndentationStyle == PipelineIndentationStyle.None) { continue; } + // Check if the current token matches the end of a PipelineAst PipelineAst matchingPipeLineAstEnd = MatchingPipelineAstEnd(pipelineAsts, ref minimumPipelineAstIndex, token); if (matchingPipeLineAstEnd == null) @@ -284,6 +289,35 @@ private static bool PipelineIsFollowedByNewlineOrLineContinuation(Token[] tokens return false; } + private static bool PreviousLineEndedWithPipe(Token[] tokens, int tokenIndex, Token token) + { + if (tokenIndex < 2 || token.Extent.StartLineNumber == 1) + { + return false; + } + + int searchIndex = tokenIndex - 2; + int searchLine; + do + { + searchLine = tokens[searchIndex].Extent.StartLineNumber; + if (tokens[searchIndex].Kind == TokenKind.Comment) + { + searchIndex--; + } + else if (tokens[searchIndex].Kind == TokenKind.Pipe) + { + return true; + } + else + { + break; + } + } while (searchLine == token.Extent.StartLineNumber - 1 && searchIndex >= 0); + + return false; + } + private static bool LineHasPipelineBeforeToken(Token[] tokens, int tokenIndex, Token token) { int searchIndex = tokenIndex; diff --git a/Tests/Rules/UseConsistentIndentation.tests.ps1 b/Tests/Rules/UseConsistentIndentation.tests.ps1 index 1ec345e29..fe8468cf7 100644 --- a/Tests/Rules/UseConsistentIndentation.tests.ps1 +++ b/Tests/Rules/UseConsistentIndentation.tests.ps1 @@ -239,6 +239,40 @@ baz Test-CorrectionExtentFromContent @params } + It "Should indent hashtable correctly using option" -TestCases @( + @{ + PipelineIndentation = 'IncreaseIndentationForFirstPipeline' + }, + @{ + PipelineIndentation = 'IncreaseIndentationAfterEveryPipeline' + }, + @{ + PipelineIndentation = 'NoIndentation' + } + @{ + PipelineIndentation = 'None' + } + ) { + Param([string] $PipelineIndentation) + $scriptDefinition = @' +@{ + foo = "value1" + bar = "value2" +} +'@ + $settings = @{ + IncludeRules = @('PSUseConsistentIndentation') + Rules = @{ PSUseConsistentIndentation = @{ Enable = $true; PipelineIndentation = $PipelineIndentation } } + } + Invoke-Formatter -Settings $settings -ScriptDefinition $scriptDefinition | Should -Be @' +@{ + foo = "value1" + bar = "value2" +} +'@ + + } + It "Should indent pipelines correctly using option" -TestCases @( @{ PipelineIndentation = 'IncreaseIndentationForFirstPipeline'