From 295c5348500b9a5349e1ef28b3ae0c9f7e3388da Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Sun, 26 Apr 2020 21:29:52 +0100 Subject: [PATCH 1/6] UseConsistentIndentation: When non-default options are used, cater for the case of a comment between pipe and newline --- Rules/UseConsistentIndentation.cs | 19 +++++++++++++++++-- .../Rules/UseConsistentIndentation.tests.ps1 | 12 ++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/Rules/UseConsistentIndentation.cs b/Rules/UseConsistentIndentation.cs index 43a4f071f..5d76abdaf 100644 --- a/Rules/UseConsistentIndentation.cs +++ b/Rules/UseConsistentIndentation.cs @@ -155,8 +155,8 @@ public override IEnumerable AnalyzeScript(Ast ast, string file 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); + bool pipelineIsFollowedByNewlineOrLineContinuation = + PipelineIsFollowedByNewlineOrLineContinuation(tokens, tokenIndex); if (!pipelineIsFollowedByNewlineOrLineContinuation) { break; @@ -254,6 +254,21 @@ public override IEnumerable AnalyzeScript(Ast ast, string file return diagnosticRecords; } + private static bool PipelineIsFollowedByNewlineOrLineContinuation(Token[] tokens, int tokenIndex) + { + if (tokenIndex == tokens.Length - 1) + { + return false; + } + var nextToken = tokens[tokenIndex + 1]; + if (nextToken.Kind == TokenKind.Comment && tokenIndex < tokens.Length - 2) + { + nextToken = tokens[tokenIndex + 2]; + } + return nextToken.Kind == TokenKind.NewLine || + nextToken.Kind == TokenKind.LineContinuation; + } + 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 06dbea290..3f527b2f0 100644 --- a/Tests/Rules/UseConsistentIndentation.tests.ps1 +++ b/Tests/Rules/UseConsistentIndentation.tests.ps1 @@ -156,6 +156,18 @@ foo | Invoke-FormatterAssertion $scriptDefinition $expected 3 $settings } + It "When a comment is after a pipeline and before the newline " { + $scriptDefinition = @' +foo | # comment +bar +'@ + $expected = @' +foo | # comment + bar +'@ + Invoke-FormatterAssertion $scriptDefinition $expected 1 $settings + } + It "Should find a violation if a pipleline element is not indented correctly" { $def = @' get-process | From 4398031151d03e54bc3dfe107ca54f5107ab6246 Mon Sep 17 00:00:00 2001 From: "Christoph Bergmeister [MVP]" Date: Mon, 27 Apr 2020 17:30:47 +0100 Subject: [PATCH 2/6] Update Rules/UseConsistentIndentation.cs Co-Authored-By: Robert Holt --- Rules/UseConsistentIndentation.cs | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/Rules/UseConsistentIndentation.cs b/Rules/UseConsistentIndentation.cs index 5d76abdaf..0bc7b0833 100644 --- a/Rules/UseConsistentIndentation.cs +++ b/Rules/UseConsistentIndentation.cs @@ -254,19 +254,33 @@ public override IEnumerable AnalyzeScript(Ast ast, string file return diagnosticRecords; } - private static bool PipelineIsFollowedByNewlineOrLineContinuation(Token[] tokens, int tokenIndex) + private static bool PipelineIsFollowedByNewlineOrLineContinuation(Token[] tokens, int startIndex) { - if (tokenIndex == tokens.Length - 1) + if (startIndex >= tokens.Length - 1) { return false; } - var nextToken = tokens[tokenIndex + 1]; - if (nextToken.Kind == TokenKind.Comment && tokenIndex < tokens.Length - 2) + + Token nextToken = null; + for (int i = startIndex + 1; i < tokens.Length; i++) { - nextToken = tokens[tokenIndex + 2]; + nextToken = tokens[i]; + + switch (nextToken.Kind) + { + case TokenKind.Comment: + continue; + + case TokenKind.NewLine: + case TokenKind.LineContinuation: + return true; + + default: + return false; } - return nextToken.Kind == TokenKind.NewLine || - nextToken.Kind == TokenKind.LineContinuation; + + // We've run out of tokens but haven't seen a newline + return false; } private static bool LineHasPipelineBeforeToken(Token[] tokens, int tokenIndex, Token token) From 65da16451a8000f46c2e58197b8fdd9e6dda2ebf Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Mon, 27 Apr 2020 22:27:09 +0100 Subject: [PATCH 3/6] re-trigger ci From 226259d58d78e44a28c218611d7fca2a1ec9eb0d Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Mon, 27 Apr 2020 22:37:03 +0100 Subject: [PATCH 4/6] try split test template --- .azure-pipelines-ci/ci.yaml | 11 ++++++++++- .../{test.yaml => test-powershell.yaml} | 7 +------ .azure-pipelines-ci/templates/test-pwsh.yaml | 19 +++++++++++++++++++ 3 files changed, 30 insertions(+), 7 deletions(-) rename .azure-pipelines-ci/templates/{test.yaml => test-powershell.yaml} (85%) create mode 100644 .azure-pipelines-ci/templates/test-pwsh.yaml diff --git a/.azure-pipelines-ci/ci.yaml b/.azure-pipelines-ci/ci.yaml index 0bc0c316b..665ad345e 100644 --- a/.azure-pipelines-ci/ci.yaml +++ b/.azure-pipelines-ci/ci.yaml @@ -34,6 +34,15 @@ stages: vmImage: vs2017-win2016 Windows_Server2019_PowerShell_Core: vmImage: windows-2019 + pool: + vmImage: $[ variables['vmImage'] ] + steps: + - template: templates/test-pwsh.yaml + - job: + strategy: + matrix: + Windows_Server2019_PowerShell_Core: + vmImage: windows-2019 Windows_Server2016_PowerShell_5_1: vmImage: vs2017-win2016 pwsh: false @@ -43,4 +52,4 @@ stages: pool: vmImage: $[ variables['vmImage'] ] steps: - - template: templates/test.yaml + - template: templates/test-powershell.yaml diff --git a/.azure-pipelines-ci/templates/test.yaml b/.azure-pipelines-ci/templates/test-powershell.yaml similarity index 85% rename from .azure-pipelines-ci/templates/test.yaml rename to .azure-pipelines-ci/templates/test-powershell.yaml index 55cea8a41..e832f04e4 100644 --- a/.azure-pipelines-ci/templates/test.yaml +++ b/.azure-pipelines-ci/templates/test-powershell.yaml @@ -1,8 +1,3 @@ -parameters: -- name: pwsh - type: boolean - default: true - steps: - task: DownloadPipelineArtifact@2 displayName: 'Download Pipeline Artifact: out Folder' @@ -13,7 +8,7 @@ steps: displayName: 'Test' inputs: targetType: inline - pwsh: $[ parameters.pwsh ] + pwsh: false script: | Import-Module .\tools\appveyor.psm1 Invoke-AppveyorTest -CheckoutPath $env:BUILD_SOURCESDIRECTORY diff --git a/.azure-pipelines-ci/templates/test-pwsh.yaml b/.azure-pipelines-ci/templates/test-pwsh.yaml new file mode 100644 index 000000000..2661b2157 --- /dev/null +++ b/.azure-pipelines-ci/templates/test-pwsh.yaml @@ -0,0 +1,19 @@ +steps: +- task: DownloadPipelineArtifact@2 + displayName: 'Download Pipeline Artifact: out Folder' + inputs: + artifactName: out + targetPath: '$(Build.SourcesDirectory)/out' +- task: PowerShell@2 + displayName: 'Test' + inputs: + targetType: inline + pwsh: true + script: | + Import-Module .\tools\appveyor.psm1 + Invoke-AppveyorTest -CheckoutPath $env:BUILD_SOURCESDIRECTORY +- task: PublishTestResults@2 + inputs: + testRunner: NUnit + testResultsFiles: 'TestResults.xml' + condition: succeededOrFailed() From 15c343507397f54a8f20f4f0f5b7ad013790970d Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Mon, 27 Apr 2020 22:43:22 +0100 Subject: [PATCH 5/6] Revert "try split test template" This reverts commit 226259d58d78e44a28c218611d7fca2a1ec9eb0d. --- .azure-pipelines-ci/ci.yaml | 11 +---------- .azure-pipelines-ci/templates/test-pwsh.yaml | 19 ------------------- .../{test-powershell.yaml => test.yaml} | 7 ++++++- 3 files changed, 7 insertions(+), 30 deletions(-) delete mode 100644 .azure-pipelines-ci/templates/test-pwsh.yaml rename .azure-pipelines-ci/templates/{test-powershell.yaml => test.yaml} (85%) diff --git a/.azure-pipelines-ci/ci.yaml b/.azure-pipelines-ci/ci.yaml index 665ad345e..0bc0c316b 100644 --- a/.azure-pipelines-ci/ci.yaml +++ b/.azure-pipelines-ci/ci.yaml @@ -34,15 +34,6 @@ stages: vmImage: vs2017-win2016 Windows_Server2019_PowerShell_Core: vmImage: windows-2019 - pool: - vmImage: $[ variables['vmImage'] ] - steps: - - template: templates/test-pwsh.yaml - - job: - strategy: - matrix: - Windows_Server2019_PowerShell_Core: - vmImage: windows-2019 Windows_Server2016_PowerShell_5_1: vmImage: vs2017-win2016 pwsh: false @@ -52,4 +43,4 @@ stages: pool: vmImage: $[ variables['vmImage'] ] steps: - - template: templates/test-powershell.yaml + - template: templates/test.yaml diff --git a/.azure-pipelines-ci/templates/test-pwsh.yaml b/.azure-pipelines-ci/templates/test-pwsh.yaml deleted file mode 100644 index 2661b2157..000000000 --- a/.azure-pipelines-ci/templates/test-pwsh.yaml +++ /dev/null @@ -1,19 +0,0 @@ -steps: -- task: DownloadPipelineArtifact@2 - displayName: 'Download Pipeline Artifact: out Folder' - inputs: - artifactName: out - targetPath: '$(Build.SourcesDirectory)/out' -- task: PowerShell@2 - displayName: 'Test' - inputs: - targetType: inline - pwsh: true - script: | - Import-Module .\tools\appveyor.psm1 - Invoke-AppveyorTest -CheckoutPath $env:BUILD_SOURCESDIRECTORY -- task: PublishTestResults@2 - inputs: - testRunner: NUnit - testResultsFiles: 'TestResults.xml' - condition: succeededOrFailed() diff --git a/.azure-pipelines-ci/templates/test-powershell.yaml b/.azure-pipelines-ci/templates/test.yaml similarity index 85% rename from .azure-pipelines-ci/templates/test-powershell.yaml rename to .azure-pipelines-ci/templates/test.yaml index e832f04e4..55cea8a41 100644 --- a/.azure-pipelines-ci/templates/test-powershell.yaml +++ b/.azure-pipelines-ci/templates/test.yaml @@ -1,3 +1,8 @@ +parameters: +- name: pwsh + type: boolean + default: true + steps: - task: DownloadPipelineArtifact@2 displayName: 'Download Pipeline Artifact: out Folder' @@ -8,7 +13,7 @@ steps: displayName: 'Test' inputs: targetType: inline - pwsh: false + pwsh: $[ parameters.pwsh ] script: | Import-Module .\tools\appveyor.psm1 Invoke-AppveyorTest -CheckoutPath $env:BUILD_SOURCESDIRECTORY From 17697643abb451e27eb03fb83f79699b8aa7da1b Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Tue, 28 Apr 2020 10:46:33 +0100 Subject: [PATCH 6/6] fix syntax due to missing brace (also: GitHub suggestions did not correctly insert whitespaces in there) --- Rules/UseConsistentIndentation.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Rules/UseConsistentIndentation.cs b/Rules/UseConsistentIndentation.cs index 0bc7b0833..8f17e2462 100644 --- a/Rules/UseConsistentIndentation.cs +++ b/Rules/UseConsistentIndentation.cs @@ -265,18 +265,19 @@ private static bool PipelineIsFollowedByNewlineOrLineContinuation(Token[] tokens for (int i = startIndex + 1; i < tokens.Length; i++) { nextToken = tokens[i]; - + switch (nextToken.Kind) { case TokenKind.Comment: continue; - + case TokenKind.NewLine: case TokenKind.LineContinuation: return true; - + default: return false; + } } // We've run out of tokens but haven't seen a newline