From 6fec06bbeef7a38dc20b7e5e613b81b9c60c0b23 Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Tue, 10 Jan 2023 19:44:03 +0100 Subject: [PATCH 01/23] Disabling "Test Analysis Failure - NuGet PackageReference" step in the test-analysis-failure workflow --- .github/workflows/test-analysis-failure.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/test-analysis-failure.yml b/.github/workflows/test-analysis-failure.yml index 1f9453d6..eb3b632e 100644 --- a/.github/workflows/test-analysis-failure.yml +++ b/.github/workflows/test-analysis-failure.yml @@ -9,6 +9,7 @@ on: jobs: call-test-analysis-failure-nuget: + if: false name: Test Analysis Failure - NuGet PackageReference uses: Lombiq/GitHub-Actions/.github/workflows/test-analysis-failure.yml@dev with: From e7cdc280e3078b5f212e3e591994de9d78a1f64d Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Tue, 10 Jan 2023 20:02:27 +0100 Subject: [PATCH 02/23] Updating Invoke-Analyzer.ps1 to require PSScriptAnalyzer version 1.21.0 as its pre-installed on runners and needed for additional analyzers --- Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 b/Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 index 461373e9..40cbfcb1 100644 --- a/Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 +++ b/Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 @@ -84,7 +84,7 @@ else exit -1 } -$installVersion = "1.20.0" +$installVersion = "1.21.0" if ((Get-InstalledModule PSScriptAnalyzer -ErrorAction SilentlyContinue).Version -ne [Version]$installVersion) { try From e5b448eddd7528be3bf79164c6def1ac528441b0 Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Tue, 10 Jan 2023 20:55:25 +0100 Subject: [PATCH 03/23] Enabling a bunch of analyzer rules --- .../PSScriptAnalyzerSettings.psd1 | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/Lombiq.Analyzers.PowerShell/PSScriptAnalyzerSettings.psd1 b/Lombiq.Analyzers.PowerShell/PSScriptAnalyzerSettings.psd1 index 39478b29..16e1003d 100644 --- a/Lombiq.Analyzers.PowerShell/PSScriptAnalyzerSettings.psd1 +++ b/Lombiq.Analyzers.PowerShell/PSScriptAnalyzerSettings.psd1 @@ -5,4 +5,42 @@ # ShouldProcess support should be implemented in cases where it makes sense. 'PSUseShouldProcessForStateChangingFunctions' ) + Rules = @{ + PSAvoidSemicolonsAsLineTerminators = @{ + Enable = $true + } + PSPlaceCloseBrace = @{ + Enable = $true + IgnoreOneLineBlock = $true + NewLineAfter = $true + NoEmptyLineBefore = $false + } + PSPlaceOpenBrace = @{ + Enable = $true + IgnoreOneLineBlock = $true + NewLineAfter = $true + OnSameLine = $false + } + PSUseConsistentIndentation = @{ + Enable = $true + IndentationSize = 4 + PipelineIndentation = 'IncreaseIndentationForFirstPipeline' + Kind = 'space' + } + PSUseConsistentWhitespace = @{ + Enable = $true + CheckInnerBrace = $true + CheckOpenBrace = $true + CheckOpenParent = $true + CheckOperator = $true + CheckPipe = $true + CheckPipeForRedundantWhitespace = $true + CheckSeparator = $true + CheckParameter = $true + IgnoreAssignmentOperatorInsideHashTable = $false + } + PSUseCorrectCasing = @{ + Enable = $true + } + } } From 386a3c1a921bf25cdd29982af436579e792378de Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Tue, 10 Jan 2023 22:34:32 +0100 Subject: [PATCH 04/23] PSScriptAnalyzerSettings: "Paren" in PSUseConsistentWhitespace.CheckOpenParen is not a typo --- Lombiq.Analyzers.PowerShell/PSScriptAnalyzerSettings.psd1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lombiq.Analyzers.PowerShell/PSScriptAnalyzerSettings.psd1 b/Lombiq.Analyzers.PowerShell/PSScriptAnalyzerSettings.psd1 index 16e1003d..b42dbae3 100644 --- a/Lombiq.Analyzers.PowerShell/PSScriptAnalyzerSettings.psd1 +++ b/Lombiq.Analyzers.PowerShell/PSScriptAnalyzerSettings.psd1 @@ -31,7 +31,7 @@ Enable = $true CheckInnerBrace = $true CheckOpenBrace = $true - CheckOpenParent = $true + CheckOpenParen = $true CheckOperator = $true CheckPipe = $true CheckPipeForRedundantWhitespace = $true From 03046b4e11ac895df61bc8ee3e2ea5b3db84867e Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Tue, 10 Jan 2023 22:37:14 +0100 Subject: [PATCH 05/23] PSScriptAnalyzerSettings: Setting PSUseConsistentWhitespace.CheckParameter to false See: https://github.com/Lombiq/PowerShell-Analyzers/issues/16#issuecomment-1377835034 --- Lombiq.Analyzers.PowerShell/PSScriptAnalyzerSettings.psd1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lombiq.Analyzers.PowerShell/PSScriptAnalyzerSettings.psd1 b/Lombiq.Analyzers.PowerShell/PSScriptAnalyzerSettings.psd1 index b42dbae3..097adabd 100644 --- a/Lombiq.Analyzers.PowerShell/PSScriptAnalyzerSettings.psd1 +++ b/Lombiq.Analyzers.PowerShell/PSScriptAnalyzerSettings.psd1 @@ -36,7 +36,7 @@ CheckPipe = $true CheckPipeForRedundantWhitespace = $true CheckSeparator = $true - CheckParameter = $true + CheckParameter = $false IgnoreAssignmentOperatorInsideHashTable = $false } PSUseCorrectCasing = @{ From fc65ffc919c3266a0e72be18bf0d6bdcdcdf1daa Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Wed, 11 Jan 2023 12:45:35 +0100 Subject: [PATCH 06/23] Updating test-analysis-failure workflow to expect PSUseConsistentIndentation warning --- .github/workflows/test-analysis-failure.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/test-analysis-failure.yml b/.github/workflows/test-analysis-failure.yml index eb3b632e..c1c4c482 100644 --- a/.github/workflows/test-analysis-failure.yml +++ b/.github/workflows/test-analysis-failure.yml @@ -24,6 +24,7 @@ jobs: PSAvoidUsingLineContinuation: Using backtick (line continuation) makes the code harder to read and maintain. PSUseApprovedVerbs: The cmdlet 'Violate-Analyzers' uses an unapproved verb. PSUseSingularNouns: The cmdlet 'Violate-Analyzers' uses a plural noun. + PSUseConsistentIndentation: Indentation not consistent call-test-analysis-failure-local: name: Test Analysis Failure - Local ProjectReference @@ -40,3 +41,4 @@ jobs: PSAvoidUsingLineContinuation: Using backtick (line continuation) makes the code harder to read and maintain. PSUseApprovedVerbs: The cmdlet 'Violate-Analyzers' uses an unapproved verb. PSUseSingularNouns: The cmdlet 'Violate-Analyzers' uses a plural noun. + PSUseConsistentIndentation: Indentation not consistent From 8ec5ef152d69bbd810da89670ee2570df23652a1 Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Wed, 11 Jan 2023 12:49:16 +0100 Subject: [PATCH 07/23] Updating Violate-Analyzers and test-analysis-failure to check for PSAvoidUsingWriteHost --- .github/workflows/test-analysis-failure.yml | 2 ++ TestSolutions/Violate-Analyzers.ps1 | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test-analysis-failure.yml b/.github/workflows/test-analysis-failure.yml index c1c4c482..a3d81f70 100644 --- a/.github/workflows/test-analysis-failure.yml +++ b/.github/workflows/test-analysis-failure.yml @@ -24,6 +24,7 @@ jobs: PSAvoidUsingLineContinuation: Using backtick (line continuation) makes the code harder to read and maintain. PSUseApprovedVerbs: The cmdlet 'Violate-Analyzers' uses an unapproved verb. PSUseSingularNouns: The cmdlet 'Violate-Analyzers' uses a plural noun. + PSAvoidUsingWriteHost: File 'Violate-Analyzers.ps1' uses Write-Host. PSUseConsistentIndentation: Indentation not consistent call-test-analysis-failure-local: @@ -41,4 +42,5 @@ jobs: PSAvoidUsingLineContinuation: Using backtick (line continuation) makes the code harder to read and maintain. PSUseApprovedVerbs: The cmdlet 'Violate-Analyzers' uses an unapproved verb. PSUseSingularNouns: The cmdlet 'Violate-Analyzers' uses a plural noun. + PSAvoidUsingWriteHost: File 'Violate-Analyzers.ps1' uses Write-Host. PSUseConsistentIndentation: Indentation not consistent diff --git a/TestSolutions/Violate-Analyzers.ps1 b/TestSolutions/Violate-Analyzers.ps1 index 18137174..52870fd3 100644 --- a/TestSolutions/Violate-Analyzers.ps1 +++ b/TestSolutions/Violate-Analyzers.ps1 @@ -1,6 +1,6 @@ function Violate-Analyzers() { - "This file is intended to verify that PSScriptAnalyzer works and contains intentionally bad code." + Write-Host "This file is intended to verify that PSScriptAnalyzer works and contains intentionally bad code." } try { Violate-Analyzers } catch { } From 01f8b64bbe56dfe07ad32cb97a0621f29503a53d Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Wed, 11 Jan 2023 17:50:12 +0100 Subject: [PATCH 08/23] Not enabling PSUseCorrectCasing yet due to https://github.com/PowerShell/PSScriptAnalyzer/issues/1881 --- Lombiq.Analyzers.PowerShell/PSScriptAnalyzerSettings.psd1 | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Lombiq.Analyzers.PowerShell/PSScriptAnalyzerSettings.psd1 b/Lombiq.Analyzers.PowerShell/PSScriptAnalyzerSettings.psd1 index 097adabd..7435ebe6 100644 --- a/Lombiq.Analyzers.PowerShell/PSScriptAnalyzerSettings.psd1 +++ b/Lombiq.Analyzers.PowerShell/PSScriptAnalyzerSettings.psd1 @@ -39,8 +39,9 @@ CheckParameter = $false IgnoreAssignmentOperatorInsideHashTable = $false } - PSUseCorrectCasing = @{ - Enable = $true - } + # PSUseCorrectCasing is not enabled yet due to https://github.com/PowerShell/PSScriptAnalyzer/issues/1881. + # PSUseCorrectCasing = @{ + # Enable = $true + # } } } From 11b598599e192cdcb5963a5254c844aa4a95372c Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Wed, 11 Jan 2023 18:05:27 +0100 Subject: [PATCH 09/23] Attempting to dry the configuration for expected code analysis errors in the test-analysis-failure workflow --- .github/workflows/test-analysis-failure.yml | 34 +++++++++------------ 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/.github/workflows/test-analysis-failure.yml b/.github/workflows/test-analysis-failure.yml index a3d81f70..dab7b004 100644 --- a/.github/workflows/test-analysis-failure.yml +++ b/.github/workflows/test-analysis-failure.yml @@ -7,6 +7,18 @@ on: branches: - dev +env: + EXPECTED_CODE_ANALYSIS_ERRORS: | + MSB3073: The command exited with non-zero code. + PSAvoidUsingAutomaticVariableAlias: '$_' is an alias of '$PSItem'. + PSAvoidUsingCmdletAliases: '%' is an alias of 'ForEach-Object'. + PSAvoidUsingEmptyCatchBlock: Empty catch block is used. + PSAvoidUsingLineContinuation: Using backtick (line continuation) makes the code harder to read and maintain. + PSUseApprovedVerbs: The cmdlet 'Violate-Analyzers' uses an unapproved verb. + PSUseSingularNouns: The cmdlet 'Violate-Analyzers' uses a plural noun. + PSAvoidUsingWriteHost: File 'Violate-Analyzers.ps1' uses Write-Host. + PSUseConsistentIndentation: Indentation not consistent + jobs: call-test-analysis-failure-nuget: if: false @@ -16,16 +28,7 @@ jobs: machine-types: "['ubuntu-latest', 'windows-latest']" build-directory: TestSolutions/Lombiq.Analyzers.PowerShell.PackageReference timeout-minutes: 30 - build-expected-code-analysis-errors: | - MSB3073: The command exited with non-zero code. - PSAvoidUsingAutomaticVariableAlias: '$_' is an alias of '$PSItem'. - PSAvoidUsingCmdletAliases: '%' is an alias of 'ForEach-Object'. - PSAvoidUsingEmptyCatchBlock: Empty catch block is used. - PSAvoidUsingLineContinuation: Using backtick (line continuation) makes the code harder to read and maintain. - PSUseApprovedVerbs: The cmdlet 'Violate-Analyzers' uses an unapproved verb. - PSUseSingularNouns: The cmdlet 'Violate-Analyzers' uses a plural noun. - PSAvoidUsingWriteHost: File 'Violate-Analyzers.ps1' uses Write-Host. - PSUseConsistentIndentation: Indentation not consistent + build-expected-code-analysis-errors: ${{ env.EXPECTED_CODE_ANALYSIS_ERRORS }} call-test-analysis-failure-local: name: Test Analysis Failure - Local ProjectReference @@ -34,13 +37,4 @@ jobs: machine-types: "['ubuntu-latest', 'windows-latest']" build-directory: TestSolutions/Lombiq.Analyzers.PowerShell.ProjectReference timeout-minutes: 30 - build-expected-code-analysis-errors: | - MSB3073: The command exited with non-zero code. - PSAvoidUsingAutomaticVariableAlias: '$_' is an alias of '$PSItem'. - PSAvoidUsingCmdletAliases: '%' is an alias of 'ForEach-Object'. - PSAvoidUsingEmptyCatchBlock: Empty catch block is used. - PSAvoidUsingLineContinuation: Using backtick (line continuation) makes the code harder to read and maintain. - PSUseApprovedVerbs: The cmdlet 'Violate-Analyzers' uses an unapproved verb. - PSUseSingularNouns: The cmdlet 'Violate-Analyzers' uses a plural noun. - PSAvoidUsingWriteHost: File 'Violate-Analyzers.ps1' uses Write-Host. - PSUseConsistentIndentation: Indentation not consistent + build-expected-code-analysis-errors: ${{ env.EXPECTED_CODE_ANALYSIS_ERRORS }} From 809872169e1b8d5c89df930d7886f2867cd0749d Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Wed, 11 Jan 2023 18:22:34 +0100 Subject: [PATCH 10/23] Revert "Attempting to dry the configuration for expected code analysis errors in the test-analysis-failure workflow" This reverts commit 11b598599e192cdcb5963a5254c844aa4a95372c. --- .github/workflows/test-analysis-failure.yml | 34 ++++++++++++--------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/.github/workflows/test-analysis-failure.yml b/.github/workflows/test-analysis-failure.yml index dab7b004..a3d81f70 100644 --- a/.github/workflows/test-analysis-failure.yml +++ b/.github/workflows/test-analysis-failure.yml @@ -7,18 +7,6 @@ on: branches: - dev -env: - EXPECTED_CODE_ANALYSIS_ERRORS: | - MSB3073: The command exited with non-zero code. - PSAvoidUsingAutomaticVariableAlias: '$_' is an alias of '$PSItem'. - PSAvoidUsingCmdletAliases: '%' is an alias of 'ForEach-Object'. - PSAvoidUsingEmptyCatchBlock: Empty catch block is used. - PSAvoidUsingLineContinuation: Using backtick (line continuation) makes the code harder to read and maintain. - PSUseApprovedVerbs: The cmdlet 'Violate-Analyzers' uses an unapproved verb. - PSUseSingularNouns: The cmdlet 'Violate-Analyzers' uses a plural noun. - PSAvoidUsingWriteHost: File 'Violate-Analyzers.ps1' uses Write-Host. - PSUseConsistentIndentation: Indentation not consistent - jobs: call-test-analysis-failure-nuget: if: false @@ -28,7 +16,16 @@ jobs: machine-types: "['ubuntu-latest', 'windows-latest']" build-directory: TestSolutions/Lombiq.Analyzers.PowerShell.PackageReference timeout-minutes: 30 - build-expected-code-analysis-errors: ${{ env.EXPECTED_CODE_ANALYSIS_ERRORS }} + build-expected-code-analysis-errors: | + MSB3073: The command exited with non-zero code. + PSAvoidUsingAutomaticVariableAlias: '$_' is an alias of '$PSItem'. + PSAvoidUsingCmdletAliases: '%' is an alias of 'ForEach-Object'. + PSAvoidUsingEmptyCatchBlock: Empty catch block is used. + PSAvoidUsingLineContinuation: Using backtick (line continuation) makes the code harder to read and maintain. + PSUseApprovedVerbs: The cmdlet 'Violate-Analyzers' uses an unapproved verb. + PSUseSingularNouns: The cmdlet 'Violate-Analyzers' uses a plural noun. + PSAvoidUsingWriteHost: File 'Violate-Analyzers.ps1' uses Write-Host. + PSUseConsistentIndentation: Indentation not consistent call-test-analysis-failure-local: name: Test Analysis Failure - Local ProjectReference @@ -37,4 +34,13 @@ jobs: machine-types: "['ubuntu-latest', 'windows-latest']" build-directory: TestSolutions/Lombiq.Analyzers.PowerShell.ProjectReference timeout-minutes: 30 - build-expected-code-analysis-errors: ${{ env.EXPECTED_CODE_ANALYSIS_ERRORS }} + build-expected-code-analysis-errors: | + MSB3073: The command exited with non-zero code. + PSAvoidUsingAutomaticVariableAlias: '$_' is an alias of '$PSItem'. + PSAvoidUsingCmdletAliases: '%' is an alias of 'ForEach-Object'. + PSAvoidUsingEmptyCatchBlock: Empty catch block is used. + PSAvoidUsingLineContinuation: Using backtick (line continuation) makes the code harder to read and maintain. + PSUseApprovedVerbs: The cmdlet 'Violate-Analyzers' uses an unapproved verb. + PSUseSingularNouns: The cmdlet 'Violate-Analyzers' uses a plural noun. + PSAvoidUsingWriteHost: File 'Violate-Analyzers.ps1' uses Write-Host. + PSUseConsistentIndentation: Indentation not consistent From 40d9fa31591dd5843b0ae450ae67d34e91c5f788 Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Tue, 17 Jan 2023 12:24:31 +0100 Subject: [PATCH 11/23] Not ignoring Information-level analyzer violations anymore, but disabling some others analyzers we don't need for now --- Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 | 4 +--- Lombiq.Analyzers.PowerShell/PSScriptAnalyzerSettings.psd1 | 7 +++++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 b/Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 index 40cbfcb1..3f87c945 100644 --- a/Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 +++ b/Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 @@ -114,9 +114,7 @@ $results = Find-Recursively -IncludeFile "*.ps1", "*.psm1", "*.psd1" -ExcludeDir $IncludeTestSolutions -or -not ( $PSItem.Name -eq 'Violate-Analyzers.ps1' -and ($PSItem.Directory.Name -eq 'TestSolutions' -or $PSItem.Directory.Parent.Name -eq 'TestSolutions')) } | - ForEach-Object { Invoke-ScriptAnalyzer -Path $PSItem.FullName @analyzerParameters } | - # Only Warning and above (ignore "Information" type results). - Where-Object { $PSItem.Severity -ge [Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticSeverity]::Warning } + ForEach-Object { Invoke-ScriptAnalyzer -Path $PSItem.FullName @analyzerParameters } foreach ($result in $results) { diff --git a/Lombiq.Analyzers.PowerShell/PSScriptAnalyzerSettings.psd1 b/Lombiq.Analyzers.PowerShell/PSScriptAnalyzerSettings.psd1 index 7435ebe6..d72226ac 100644 --- a/Lombiq.Analyzers.PowerShell/PSScriptAnalyzerSettings.psd1 +++ b/Lombiq.Analyzers.PowerShell/PSScriptAnalyzerSettings.psd1 @@ -1,6 +1,10 @@ @{ ExcludeRules = @( + 'PSDscExamplesPresent', + 'PSDscTestsPresent', + 'PSReturnCorrectTypesForDSCFunctions', + 'PSProvideCommentHelp', # This rule expects us to implement a feature we will be unlikely to use in the majority of cases. Although # ShouldProcess support should be implemented in cases where it makes sense. 'PSUseShouldProcessForStateChangingFunctions' @@ -9,6 +13,9 @@ PSAvoidSemicolonsAsLineTerminators = @{ Enable = $true } + PSAvoidUsingDoubleQuotesForConstantString = @{ + Enable = $true + } PSPlaceCloseBrace = @{ Enable = $true IgnoreOneLineBlock = $true From cb224abe4b31206e15b468af0a1aec796f96a145 Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Tue, 17 Jan 2023 12:25:03 +0100 Subject: [PATCH 12/23] Fixing PSAvoidUsingDoubleQuotesForConstantString analyzer violations --- .../Invoke-Analyzer.ps1 | 16 +++++++-------- .../Measure-AutomaticVariableAlias.psm1 | 20 +++++++++---------- .../Measure-LineContinuation.psm1 | 12 +++++------ 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 b/Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 index 3f87c945..02818e3a 100644 --- a/Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 +++ b/Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 @@ -57,7 +57,7 @@ function Write-FileError([string] $Message, [string] $Path, [int] $Line = 0, [in } elseif ($ForMsBuild) { - if (-not $Message.Contains(":")) { $Message = ": $Message" } + if (-not $Message.Contains(':')) { $Message = ": $Message" } if ($Path) { @@ -84,7 +84,7 @@ else exit -1 } -$installVersion = "1.21.0" +$installVersion = '1.21.0' if ((Get-InstalledModule PSScriptAnalyzer -ErrorAction SilentlyContinue).Version -ne [Version]$installVersion) { try @@ -94,10 +94,10 @@ if ((Get-InstalledModule PSScriptAnalyzer -ErrorAction SilentlyContinue).Version } catch { - $infoUrl = "https://docs.microsoft.com/en-us/powershell/utility-modules/psscriptanalyzer/overview?view=ps-modules#installing-psscriptanalyzer" - Write-FileError ("Unable to detect Invoke-ScriptAnalyzer and failed to install PSScriptAnalyzer. If you " + - "are on Windows Powershell, open an administrator shell and type `"Install-Module -Name " + - "PSScriptAnalyzer -Force -RequiredVersion $installVersion`". Otherwise see $infoUrl to learn more.") + $infoUrl = 'https://docs.microsoft.com/en-us/powershell/utility-modules/psscriptanalyzer/overview?view=ps-modules#installing-psscriptanalyzer' + Write-FileError ('Unable to detect Invoke-ScriptAnalyzer and failed to install PSScriptAnalyzer. If you are' + + 'on Windows Powershell, open an administrator shell and type "Install-Module -Name PSScriptAnalyzer' + + " -Force -RequiredVersion $installVersion`". Otherwise see $infoUrl to learn more.") exit -2 } } @@ -109,7 +109,7 @@ $analyzerParameters = @{ IncludeDefaultRules = $true Fix = $Fix } -$results = Find-Recursively -IncludeFile "*.ps1", "*.psm1", "*.psd1" -ExcludeDirectory node_modules | +$results = Find-Recursively -IncludeFile '*.ps1', '*.psm1', '*.psd1' -ExcludeDirectory node_modules | Where-Object { # Exclude /TestSolutions/Violate-Analyzers.ps1 and /TestSolutions/*/Violate-Analyzers.ps1 $IncludeTestSolutions -or -not ( $PSItem.Name -eq 'Violate-Analyzers.ps1' -and @@ -118,7 +118,7 @@ $results = Find-Recursively -IncludeFile "*.ps1", "*.psm1", "*.psd1" -ExcludeDir foreach ($result in $results) { - $message = $result.RuleName + ": " + $result.Message + $message = $result.RuleName + ': ' + $result.Message Write-FileError -Path $result.ScriptPath -Line $result.Line -Column $result.Column $message } diff --git a/Lombiq.Analyzers.PowerShell/Rules/Measure-AutomaticVariableAlias/Measure-AutomaticVariableAlias.psm1 b/Lombiq.Analyzers.PowerShell/Rules/Measure-AutomaticVariableAlias/Measure-AutomaticVariableAlias.psm1 index c0daf0df..2a0eb567 100644 --- a/Lombiq.Analyzers.PowerShell/Rules/Measure-AutomaticVariableAlias/Measure-AutomaticVariableAlias.psm1 +++ b/Lombiq.Analyzers.PowerShell/Rules/Measure-AutomaticVariableAlias/Measure-AutomaticVariableAlias.psm1 @@ -31,29 +31,29 @@ function Measure-AutomaticVariableAlias try { # Filter down tokens to just variable tokens with the name "_". - foreach ($automaticVariableAliasToken in $Token | Where-Object { $PSItem.GetType().Name -eq "VariableToken" -and $PSItem.Name -eq "_" }) + foreach ($automaticVariableAliasToken in $Token | Where-Object { $PSItem.GetType().Name -eq 'VariableToken' -and $PSItem.Name -eq '_' }) { - $correctionTypeName = "Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.CorrectionExtent" + $correctionTypeName = 'Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.CorrectionExtent' $correctionExtent = New-Object -TypeName $correctionTypeName -ArgumentList @( $automaticVariableAliasToken.Extent.StartLineNumber $automaticVariableAliasToken.Extent.EndLineNumber $automaticVariableAliasToken.Extent.StartColumnNumber $automaticVariableAliasToken.Extent.EndColumnNumber '$PSItem' - 'Replaced the usage of the alias of the automatic variable ''$_'' with its full name ''$PSItem''.' + 'Replaced the usage of the alias of the automatic variable "$_" with its full name "$PSItem".' ) $suggestedCorrections = New-Object System.Collections.ObjectModel.Collection[$correctionTypeName] $suggestedCorrections.add($correctionExtent) | Out-Null $results += [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord]@{ - "Extent" = $automaticVariableAliasToken.Extent - "Message" = '''$_'' is an alias of the automatic variable ''$PSItem''. Please consider using the full name of this' + - ' variable for consistency.' - "RuleName" = "PSAvoidUsingAutomaticVariableAlias" - "RuleSuppressionID" = "PSAvoidUsingAutomaticVariableAlias" - "Severity" = "Warning" - "SuggestedCorrections" = $suggestedCorrections + 'Extent' = $automaticVariableAliasToken.Extent + 'Message' = '"$_" is an alias of the automatic variable "$PSItem". Please consider using the full' + + ' name of this variable for consistency.' + 'RuleName' = 'PSAvoidUsingAutomaticVariableAlias' + 'RuleSuppressionID' = 'PSAvoidUsingAutomaticVariableAlias' + 'Severity' = 'Warning' + 'SuggestedCorrections' = $suggestedCorrections } } diff --git a/Lombiq.Analyzers.PowerShell/Rules/Measure-LineContinuation/Measure-LineContinuation.psm1 b/Lombiq.Analyzers.PowerShell/Rules/Measure-LineContinuation/Measure-LineContinuation.psm1 index 53309e98..5b925a45 100644 --- a/Lombiq.Analyzers.PowerShell/Rules/Measure-LineContinuation/Measure-LineContinuation.psm1 +++ b/Lombiq.Analyzers.PowerShell/Rules/Measure-LineContinuation/Measure-LineContinuation.psm1 @@ -38,12 +38,12 @@ function Measure-LineContinuation $PSItem.Kind -eq [System.Management.Automation.Language.TokenKind]::LineContinuation }) { $results += [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord]@{ - "Extent" = $lineContinuationToken.Extent - "Message" = 'Using backtick (line continuation) makes the code harder to read and' + - ' maintain. Please consider using parameter splatting instead.' - "RuleName" = "PSAvoidUsingLineContinuation" - "RuleSuppressionID" = "PSAvoidUsingLineContinuation" - "Severity" = "Warning" + 'Extent' = $lineContinuationToken.Extent + 'Message' = 'Using backtick (line continuation) makes the code harder to read and maintain.' + + ' Please consider using parameter splatting instead.' + 'RuleName' = 'PSAvoidUsingLineContinuation' + 'RuleSuppressionID' = 'PSAvoidUsingLineContinuation' + 'Severity' = 'Warning' } } From 2de89bdd7148eb3711ff903a470e903c226b3380 Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Tue, 17 Jan 2023 15:39:47 +0100 Subject: [PATCH 13/23] Updating Violate-Analyzers.ps1 to violate PSAvoidUsingDoubleQuotesForConstantString only once --- TestSolutions/Violate-Analyzers.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/TestSolutions/Violate-Analyzers.ps1 b/TestSolutions/Violate-Analyzers.ps1 index 52870fd3..9f773bb2 100644 --- a/TestSolutions/Violate-Analyzers.ps1 +++ b/TestSolutions/Violate-Analyzers.ps1 @@ -1,9 +1,9 @@ function Violate-Analyzers() { - Write-Host "This file is intended to verify that PSScriptAnalyzer works and contains intentionally bad code." + Write-Host 'This file is intended to verify that PSScriptAnalyzer works and contains intentionally bad code.' } try { Violate-Analyzers } catch { } "Lombiq", ` -"Orchard", "Hastlayer" | % { $_ } +'Orchard', 'Hastlayer' | % { $_ } From 2c6226c70bf290ba0134bce66ad25d997cc05d77 Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Tue, 17 Jan 2023 15:42:56 +0100 Subject: [PATCH 14/23] Adding PSAvoidUsingDoubleQuotesForConstantString to the list of expected analyzer violations in the test-analysis-failure workflow --- .github/workflows/test-analysis-failure.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/test-analysis-failure.yml b/.github/workflows/test-analysis-failure.yml index a3d81f70..6e779d69 100644 --- a/.github/workflows/test-analysis-failure.yml +++ b/.github/workflows/test-analysis-failure.yml @@ -26,6 +26,7 @@ jobs: PSUseSingularNouns: The cmdlet 'Violate-Analyzers' uses a plural noun. PSAvoidUsingWriteHost: File 'Violate-Analyzers.ps1' uses Write-Host. PSUseConsistentIndentation: Indentation not consistent + PSAvoidUsingDoubleQuotesForConstantString: Use single quotes when a string is constant. call-test-analysis-failure-local: name: Test Analysis Failure - Local ProjectReference @@ -44,3 +45,4 @@ jobs: PSUseSingularNouns: The cmdlet 'Violate-Analyzers' uses a plural noun. PSAvoidUsingWriteHost: File 'Violate-Analyzers.ps1' uses Write-Host. PSUseConsistentIndentation: Indentation not consistent + PSAvoidUsingDoubleQuotesForConstantString: Use single quotes when a string is constant. From 9d3723aab139c43c64e12056201c3e7fcdc14d09 Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Tue, 17 Jan 2023 16:09:23 +0100 Subject: [PATCH 15/23] Update Lombiq.Analyzers.PowerShell/Rules/Measure-LineContinuation/Measure-LineContinuation.psm1 Co-authored-by: 0liver <0liver@users.noreply.github.com> --- .../Measure-LineContinuation/Measure-LineContinuation.psm1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lombiq.Analyzers.PowerShell/Rules/Measure-LineContinuation/Measure-LineContinuation.psm1 b/Lombiq.Analyzers.PowerShell/Rules/Measure-LineContinuation/Measure-LineContinuation.psm1 index 5b925a45..004bdfd2 100644 --- a/Lombiq.Analyzers.PowerShell/Rules/Measure-LineContinuation/Measure-LineContinuation.psm1 +++ b/Lombiq.Analyzers.PowerShell/Rules/Measure-LineContinuation/Measure-LineContinuation.psm1 @@ -40,7 +40,7 @@ function Measure-LineContinuation $results += [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord]@{ 'Extent' = $lineContinuationToken.Extent 'Message' = 'Using backtick (line continuation) makes the code harder to read and maintain.' + - ' Please consider using parameter splatting instead.' + ' Please use parameter splatting instead.' 'RuleName' = 'PSAvoidUsingLineContinuation' 'RuleSuppressionID' = 'PSAvoidUsingLineContinuation' 'Severity' = 'Warning' From 8dc81e25bed028e050df0df17c4dd4bb1edd73d8 Mon Sep 17 00:00:00 2001 From: Oliver Friedrich Date: Tue, 17 Jan 2023 16:20:09 +0100 Subject: [PATCH 16/23] Updating the explanation of Violate-Analyzers.ps1 --- TestSolutions/Violate-Analyzers.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TestSolutions/Violate-Analyzers.ps1 b/TestSolutions/Violate-Analyzers.ps1 index 9f773bb2..ba1ce7e6 100644 --- a/TestSolutions/Violate-Analyzers.ps1 +++ b/TestSolutions/Violate-Analyzers.ps1 @@ -1,6 +1,6 @@ function Violate-Analyzers() { - Write-Host 'This file is intended to verify that PSScriptAnalyzer works and contains intentionally bad code.' + Write-Host 'This file contains intentionally bad code to verify that PSScriptAnalyzer works correctly.' } try { Violate-Analyzers } catch { } From 08e4d67a2a086bb32327a6ef627ae18f9c7cd6e4 Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Tue, 17 Jan 2023 16:54:33 +0100 Subject: [PATCH 17/23] Fixing message formatting in Invoke-Invoke-Analyzer.ps1 --- Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 b/Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 index 02818e3a..21b546f9 100644 --- a/Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 +++ b/Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 @@ -96,8 +96,8 @@ if ((Get-InstalledModule PSScriptAnalyzer -ErrorAction SilentlyContinue).Version { $infoUrl = 'https://docs.microsoft.com/en-us/powershell/utility-modules/psscriptanalyzer/overview?view=ps-modules#installing-psscriptanalyzer' Write-FileError ('Unable to detect Invoke-ScriptAnalyzer and failed to install PSScriptAnalyzer. If you are' + - 'on Windows Powershell, open an administrator shell and type "Install-Module -Name PSScriptAnalyzer' + - " -Force -RequiredVersion $installVersion`". Otherwise see $infoUrl to learn more.") + ' on Windows Powershell, open an administrator shell and type "Install-Module -Name PSScriptAnalyzer' + + " -RequiredVersion $installVersion`". Otherwise see $infoUrl to learn more.") exit -2 } } From d000ed36044294702358edc891ba654f7f9ad4c6 Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Tue, 17 Jan 2023 22:47:11 +0100 Subject: [PATCH 18/23] Improved message formatting for variable assignment and defining the first positional parameter with piping --- Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 | 8 +++++--- .../Measure-AutomaticVariableAlias.psm1 | 6 ++++-- .../Measure-LineContinuation.psm1 | 6 ++++-- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 b/Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 index 21b546f9..020d2672 100644 --- a/Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 +++ b/Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 @@ -95,9 +95,11 @@ if ((Get-InstalledModule PSScriptAnalyzer -ErrorAction SilentlyContinue).Version catch { $infoUrl = 'https://docs.microsoft.com/en-us/powershell/utility-modules/psscriptanalyzer/overview?view=ps-modules#installing-psscriptanalyzer' - Write-FileError ('Unable to detect Invoke-ScriptAnalyzer and failed to install PSScriptAnalyzer. If you are' + - ' on Windows Powershell, open an administrator shell and type "Install-Module -Name PSScriptAnalyzer' + - " -RequiredVersion $installVersion`". Otherwise see $infoUrl to learn more.") + @( + 'Unable to detect Invoke-ScriptAnalyzer and failed to install PSScriptAnalyzer. If you are on Windows' + 'Powershell, open an administrator shell and type "Install-Module -Name PSScriptAnalyzer -RequiredVersion' + "$installVersion`". Otherwise see $infoUrl to learn more." + ) -join ' ' | Write-FileError exit -2 } } diff --git a/Lombiq.Analyzers.PowerShell/Rules/Measure-AutomaticVariableAlias/Measure-AutomaticVariableAlias.psm1 b/Lombiq.Analyzers.PowerShell/Rules/Measure-AutomaticVariableAlias/Measure-AutomaticVariableAlias.psm1 index 2a0eb567..ccff0ed0 100644 --- a/Lombiq.Analyzers.PowerShell/Rules/Measure-AutomaticVariableAlias/Measure-AutomaticVariableAlias.psm1 +++ b/Lombiq.Analyzers.PowerShell/Rules/Measure-AutomaticVariableAlias/Measure-AutomaticVariableAlias.psm1 @@ -48,8 +48,10 @@ function Measure-AutomaticVariableAlias $results += [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord]@{ 'Extent' = $automaticVariableAliasToken.Extent - 'Message' = '"$_" is an alias of the automatic variable "$PSItem". Please consider using the full' + - ' name of this variable for consistency.' + 'Message' = @( + '"$_" is an alias of the automatic variable "$PSItem". Please use the full name of this' + 'variable for consistency.' + ) -join ' ' 'RuleName' = 'PSAvoidUsingAutomaticVariableAlias' 'RuleSuppressionID' = 'PSAvoidUsingAutomaticVariableAlias' 'Severity' = 'Warning' diff --git a/Lombiq.Analyzers.PowerShell/Rules/Measure-LineContinuation/Measure-LineContinuation.psm1 b/Lombiq.Analyzers.PowerShell/Rules/Measure-LineContinuation/Measure-LineContinuation.psm1 index 004bdfd2..34c94188 100644 --- a/Lombiq.Analyzers.PowerShell/Rules/Measure-LineContinuation/Measure-LineContinuation.psm1 +++ b/Lombiq.Analyzers.PowerShell/Rules/Measure-LineContinuation/Measure-LineContinuation.psm1 @@ -39,8 +39,10 @@ function Measure-LineContinuation { $results += [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord]@{ 'Extent' = $lineContinuationToken.Extent - 'Message' = 'Using backtick (line continuation) makes the code harder to read and maintain.' + - ' Please use parameter splatting instead.' + 'Message' = @( + 'Using backtick (line continuation) makes the code harder to read and maintain. Please use' + 'parameter splatting instead.' + ) -join ' ' 'RuleName' = 'PSAvoidUsingLineContinuation' 'RuleSuppressionID' = 'PSAvoidUsingLineContinuation' 'Severity' = 'Warning' From 21203aaaeef1c4609de5ff0bf77702cf83108cce Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Wed, 18 Jan 2023 19:08:55 +0100 Subject: [PATCH 19/23] Re-enabling PSUseCorrectCasing analyzer rule, because https://github.com/PowerShell/PSScriptAnalyzer/issues/1881 no longer blocks it --- Lombiq.Analyzers.PowerShell/PSScriptAnalyzerSettings.psd1 | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Lombiq.Analyzers.PowerShell/PSScriptAnalyzerSettings.psd1 b/Lombiq.Analyzers.PowerShell/PSScriptAnalyzerSettings.psd1 index d72226ac..677a637b 100644 --- a/Lombiq.Analyzers.PowerShell/PSScriptAnalyzerSettings.psd1 +++ b/Lombiq.Analyzers.PowerShell/PSScriptAnalyzerSettings.psd1 @@ -46,9 +46,8 @@ CheckParameter = $false IgnoreAssignmentOperatorInsideHashTable = $false } - # PSUseCorrectCasing is not enabled yet due to https://github.com/PowerShell/PSScriptAnalyzer/issues/1881. - # PSUseCorrectCasing = @{ - # Enable = $true - # } + PSUseCorrectCasing = @{ + Enable = $true + } } } From 3feafe7f669b8fbcadcaef351da9ebfa0b9a407f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20El-Saig?= Date: Tue, 31 Jan 2023 21:56:43 +0100 Subject: [PATCH 20/23] Revert temporary change. --- .github/workflows/test-analysis-failure.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/test-analysis-failure.yml b/.github/workflows/test-analysis-failure.yml index 6e779d69..42a3a838 100644 --- a/.github/workflows/test-analysis-failure.yml +++ b/.github/workflows/test-analysis-failure.yml @@ -9,7 +9,6 @@ on: jobs: call-test-analysis-failure-nuget: - if: false name: Test Analysis Failure - NuGet PackageReference uses: Lombiq/GitHub-Actions/.github/workflows/test-analysis-failure.yml@dev with: From 95e3045abccf76c5b2fe0ff11364e798d403b11a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20El-Saig?= Date: Tue, 31 Jan 2023 21:59:31 +0100 Subject: [PATCH 21/23] Revert "Revert temporary change." This reverts commit 3feafe7f669b8fbcadcaef351da9ebfa0b9a407f. --- .github/workflows/test-analysis-failure.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/test-analysis-failure.yml b/.github/workflows/test-analysis-failure.yml index 42a3a838..6e779d69 100644 --- a/.github/workflows/test-analysis-failure.yml +++ b/.github/workflows/test-analysis-failure.yml @@ -9,6 +9,7 @@ on: jobs: call-test-analysis-failure-nuget: + if: false name: Test Analysis Failure - NuGet PackageReference uses: Lombiq/GitHub-Actions/.github/workflows/test-analysis-failure.yml@dev with: From a378811a76aea2e9b63ae90890c41b1104699c51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20El-Saig?= Date: Tue, 31 Jan 2023 22:09:12 +0100 Subject: [PATCH 22/23] Update PackageReference version. --- .../Lombiq.Analyzers.PowerShell.PackageReference.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TestSolutions/Lombiq.Analyzers.PowerShell.PackageReference/Lombiq.Analyzers.PowerShell.PackageReference.csproj b/TestSolutions/Lombiq.Analyzers.PowerShell.PackageReference/Lombiq.Analyzers.PowerShell.PackageReference.csproj index 724512a8..cb739980 100644 --- a/TestSolutions/Lombiq.Analyzers.PowerShell.PackageReference/Lombiq.Analyzers.PowerShell.PackageReference.csproj +++ b/TestSolutions/Lombiq.Analyzers.PowerShell.PackageReference/Lombiq.Analyzers.PowerShell.PackageReference.csproj @@ -7,7 +7,7 @@ - + From 2fd776212d78e8e77a60462185f1accf49b4a01c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20El-Saig?= Date: Tue, 31 Jan 2023 22:09:57 +0100 Subject: [PATCH 23/23] Revert temporary change. --- .github/workflows/test-analysis-failure.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/test-analysis-failure.yml b/.github/workflows/test-analysis-failure.yml index 6e779d69..42a3a838 100644 --- a/.github/workflows/test-analysis-failure.yml +++ b/.github/workflows/test-analysis-failure.yml @@ -9,7 +9,6 @@ on: jobs: call-test-analysis-failure-nuget: - if: false name: Test Analysis Failure - NuGet PackageReference uses: Lombiq/GitHub-Actions/.github/workflows/test-analysis-failure.yml@dev with: