From a5a2e6b224ab960c73eb148d7683a332834f8ca5 Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Wed, 13 May 2020 22:09:38 +0000 Subject: [PATCH 1/6] Fix bug with PipelineIndentationStyle.None where break instead of continue statement was used. TODO: Fix test that starts to fail because of this now --- Rules/UseConsistentIndentation.cs | 4 +-- .../Rules/UseConsistentIndentation.tests.ps1 | 36 ++++++++++++++++++- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/Rules/UseConsistentIndentation.cs b/Rules/UseConsistentIndentation.cs index 8f17e2462..6c14fd415 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; @@ -227,7 +227,7 @@ public override IEnumerable AnalyzeScript(Ast ast, string file 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) diff --git a/Tests/Rules/UseConsistentIndentation.tests.ps1 b/Tests/Rules/UseConsistentIndentation.tests.ps1 index 3f527b2f0..5f67b4935 100644 --- a/Tests/Rules/UseConsistentIndentation.tests.ps1 +++ b/Tests/Rules/UseConsistentIndentation.tests.ps1 @@ -1,4 +1,4 @@ -$testRootDirectory = Split-Path -Parent $PSScriptRoot +$testRootDirectory = Split-Path -Parent $PSScriptRoot Import-Module (Join-Path $testRootDirectory "PSScriptAnalyzerTestHelper.psm1") @@ -235,6 +235,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' From 6d0a4d2113a9ac146f3c13f1ade9d86af47ca58d Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Thu, 14 May 2020 16:57:10 +0000 Subject: [PATCH 2/6] fix last failing test to make None not touch existing indendation --- Rules/UseConsistentIndentation.cs | 33 ++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/Rules/UseConsistentIndentation.cs b/Rules/UseConsistentIndentation.cs index 6c14fd415..4abc7ba15 100644 --- a/Rules/UseConsistentIndentation.cs +++ b/Rules/UseConsistentIndentation.cs @@ -220,8 +220,11 @@ public override IEnumerable AnalyzeScript(Ast ast, string file } } + if (pipelineIndentationStyle == PipelineIndentationStyle.None && PreviousLineEndedWithPipe(tokens, tokenIndex, token)) + { + continue; + } bool lineHasPipelineBeforeToken = LineHasPipelineBeforeToken(tokens, tokenIndex, token); - AddViolation(token, tempIndentationLevel, diagnosticRecords, ref onNewLine, lineHasPipelineBeforeToken); } break; @@ -284,6 +287,34 @@ 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 = token.Extent.StartLineNumber; + 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; From 6cf454f1d92b2e610aaf4f641518a8a896d3ab22 Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Thu, 11 Jun 2020 21:14:33 +0100 Subject: [PATCH 3/6] empty commit since Azure Pipelines did not trigger From 8f6769fc286987333a9683b92d71c9f0506692c9 Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Thu, 11 Jun 2020 21:28:07 +0100 Subject: [PATCH 4/6] empty commit since Azure Pipelines did not trigger From 82e993c84467c06a6d48ecae1ba400bc18c04b51 Mon Sep 17 00:00:00 2001 From: "Christoph Bergmeister [MVP]" Date: Thu, 11 Jun 2020 21:47:32 +0100 Subject: [PATCH 5/6] Apply suggestions from code review Co-authored-by: Robert Holt --- Rules/UseConsistentIndentation.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Rules/UseConsistentIndentation.cs b/Rules/UseConsistentIndentation.cs index 4abc7ba15..a5cdd70ae 100644 --- a/Rules/UseConsistentIndentation.cs +++ b/Rules/UseConsistentIndentation.cs @@ -224,6 +224,7 @@ public override IEnumerable AnalyzeScript(Ast ast, string file { continue; } + bool lineHasPipelineBeforeToken = LineHasPipelineBeforeToken(tokens, tokenIndex, token); AddViolation(token, tempIndentationLevel, diagnosticRecords, ref onNewLine, lineHasPipelineBeforeToken); } @@ -231,6 +232,7 @@ public override IEnumerable AnalyzeScript(Ast ast, string file } 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) @@ -312,6 +314,7 @@ private static bool PreviousLineEndedWithPipe(Token[] tokens, int tokenIndex, To break; } } while (searchLine == token.Extent.StartLineNumber - 1 && searchIndex >= 0); + return false; } From 13c5fcc87ac9d5afda74c8da9f2e2fd8ea1e0afc Mon Sep 17 00:00:00 2001 From: "Christoph Bergmeister [MVP]" Date: Thu, 11 Jun 2020 22:00:19 +0100 Subject: [PATCH 6/6] Apply suggestions from code review --- Rules/UseConsistentIndentation.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Rules/UseConsistentIndentation.cs b/Rules/UseConsistentIndentation.cs index a5cdd70ae..10b01a600 100644 --- a/Rules/UseConsistentIndentation.cs +++ b/Rules/UseConsistentIndentation.cs @@ -297,7 +297,7 @@ private static bool PreviousLineEndedWithPipe(Token[] tokens, int tokenIndex, To } int searchIndex = tokenIndex - 2; - int searchLine = token.Extent.StartLineNumber; + int searchLine; do { searchLine = tokens[searchIndex].Extent.StartLineNumber;