From dd3cfd3a91c0dc9c576d9c8e715ff50c00236902 Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Mon, 2 Mar 2020 08:41:09 +0000 Subject: [PATCH 1/3] When pipelineIndentationStyle is non-default setting: fix edge --- Rules/UseConsistentIndentation.cs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/Rules/UseConsistentIndentation.cs b/Rules/UseConsistentIndentation.cs index 2c9f7af6d..abb0f207e 100644 --- a/Rules/UseConsistentIndentation.cs +++ b/Rules/UseConsistentIndentation.cs @@ -168,7 +168,9 @@ public override IEnumerable AnalyzeScript(Ast ast, string file } if (pipelineIndentationStyle == PipelineIndentationStyle.IncreaseIndentationForFirstPipeline) { - bool isFirstPipeInPipeline = pipelineAsts.Any(pipelineAst => PositionIsEqual(((PipelineAst)pipelineAst).PipelineElements[0].Extent.EndScriptPosition, tokens[tokenIndex - 1].Extent.EndScriptPosition)); + bool isFirstPipeInPipeline = pipelineAsts.Any(pipelineAst => + PositionIsEqual(LastPipeOnFirstLineWithPipeUsage((PipelineAst)pipelineAst).Extent.EndScriptPosition, + tokens[tokenIndex - 1].Extent.EndScriptPosition)); if (isFirstPipeInPipeline) { AddViolation(token, indentationLevel++, diagnosticRecords, ref onNewLine); @@ -255,6 +257,21 @@ public override IEnumerable AnalyzeScript(Ast ast, string file return diagnosticRecords; } + private static CommandBaseAst LastPipeOnFirstLineWithPipeUsage(PipelineAst pipelineAst) + { + CommandBaseAst lastPipeOnFirstLineWithPipeUsage = pipelineAst.PipelineElements[0]; + foreach (CommandBaseAst pipelineElement in pipelineAst.PipelineElements.Skip(1)) + { + if (pipelineElement.Extent.StartLineNumber == pipelineAst.PipelineElements[0].Extent.StartLineNumber || + pipelineElement.Extent.StartLineNumber == pipelineAst.PipelineElements[0].Extent.EndLineNumber || + pipelineElement.Extent.EndLineNumber == pipelineAst.PipelineElements[0].Extent.EndLineNumber) + { + lastPipeOnFirstLineWithPipeUsage = pipelineElement; + } + } + return lastPipeOnFirstLineWithPipeUsage; + } + private static bool PositionIsEqual(IScriptPosition position1, IScriptPosition position2) { return position1.ColumnNumber == position2.ColumnNumber && From c2526517671a46f28f260e3c2d1d80ba781cb5e5 Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Mon, 2 Mar 2020 13:01:02 +0000 Subject: [PATCH 2/3] add test cases --- .../Rules/UseConsistentIndentation.tests.ps1 | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/Tests/Rules/UseConsistentIndentation.tests.ps1 b/Tests/Rules/UseConsistentIndentation.tests.ps1 index 5f3fd9e36..d876d3af8 100644 --- a/Tests/Rules/UseConsistentIndentation.tests.ps1 +++ b/Tests/Rules/UseConsistentIndentation.tests.ps1 @@ -225,6 +225,48 @@ baz Test-CorrectionExtentFromContent @params } + It "Should indent pipelines correctly using option" -TestCases @( + @{ + PipelineIndentation = 'IncreaseIndentationForFirstPipeline' + ExpectCorrection = $true + }, + @{ + PipelineIndentation = 'IncreaseIndentationAfterEveryPipeline' + ExpectCorrection = $true + }, + @{ + PipelineIndentation = 'NoIndentation' + ExpectCorrection = $false + } + @{ + PipelineIndentation = 'None' + ExpectCorrection = $false + } + ) { + Param([string] $PipelineIndentation, [bool] $ExpectCorrection) + $def = @' +foo | bar | +baz +'@ + $settings.Rules.PSUseConsistentIndentation.PipelineIndentation = $PipelineIndentation + $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings + if ($ExpectCorrection) { + $violations.Count | Should -Be 1 + $params = @{ + RawContent = $def + DiagnosticRecord = $violations[0] + CorrectionsCount = 1 + ViolationText = "baz" + CorrectionText = (New-Object -TypeName String -ArgumentList $indentationUnit, ($indentationSize * 1)) + 'baz' + } + Test-CorrectionExtentFromContent @params + } + else + { + $violations | Should -BeNullOrEmpty + } + } + It 'Should preserve script when using PipelineIndentation None' -TestCases @( @{ IdempotentScriptDefinition = @' foo | From ab454feb15390af15dba0f325c900e4d5f94d3cc Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Sat, 4 Apr 2020 20:40:26 +0100 Subject: [PATCH 3/3] Apply test code simplification suggested by @JamesWTruher --- Tests/Rules/UseConsistentIndentation.tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/Rules/UseConsistentIndentation.tests.ps1 b/Tests/Rules/UseConsistentIndentation.tests.ps1 index d876d3af8..2e9a03fd7 100644 --- a/Tests/Rules/UseConsistentIndentation.tests.ps1 +++ b/Tests/Rules/UseConsistentIndentation.tests.ps1 @@ -257,7 +257,7 @@ baz DiagnosticRecord = $violations[0] CorrectionsCount = 1 ViolationText = "baz" - CorrectionText = (New-Object -TypeName String -ArgumentList $indentationUnit, ($indentationSize * 1)) + 'baz' + CorrectionText = $indentationUnit * $indentationSize + 'baz' } Test-CorrectionExtentFromContent @params }