From cc68e40e6da42192bd5c36a8d91be4f30ae5155f Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Fri, 18 Nov 2022 18:22:47 +0100 Subject: [PATCH 01/28] Removing alias allowlist from PSScriptAnalyzerSettings --- Lombiq.Analyzers.PowerShell/PSScriptAnalyzerSettings.psd1 | 7 ------- 1 file changed, 7 deletions(-) diff --git a/Lombiq.Analyzers.PowerShell/PSScriptAnalyzerSettings.psd1 b/Lombiq.Analyzers.PowerShell/PSScriptAnalyzerSettings.psd1 index a6d424eb..b92bb77e 100644 --- a/Lombiq.Analyzers.PowerShell/PSScriptAnalyzerSettings.psd1 +++ b/Lombiq.Analyzers.PowerShell/PSScriptAnalyzerSettings.psd1 @@ -9,11 +9,4 @@ # resolved. 'PSReviewUnusedParameter' ) - 'Rules' = - @{ - 'PSAvoidUsingCmdletAliases' = - @{ - 'allowlist' = @('%', '?') - } - } } From d9d1eb65333ba497aff0c4427c644b40c9d8ca1c Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Fri, 18 Nov 2022 18:29:48 +0100 Subject: [PATCH 02/28] Fixing (new) analyzer violations in Invoke-Analyzer --- Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 b/Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 index 5908b222..bc1b5bfb 100644 --- a/Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 +++ b/Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 @@ -1,4 +1,4 @@ -param( +param( $SettingsPath = (Join-Path (Split-Path -Parent $MyInvocation.MyCommand.Path) 'PSScriptAnalyzerSettings.psd1'), [Switch] $ForGitHubActions, [Switch] $ForMsBuild, @@ -25,7 +25,7 @@ function Find-Recursively([string] $Path = '.', [string[]] $IncludeFile, [string foreach ($child in (Get-ChildItem $Here.FullName -Force)) { if ($child -is [System.IO.DirectoryInfo]) { Find-Inner $child } - elseif (($IncludeFile | ? { $child.name -like $_ }).Count) { $child } + elseif (($IncludeFile | Where-Object { $child.name -like $_ }).Count) { $child } } } @@ -89,13 +89,13 @@ if ((Get-InstalledModule PSScriptAnalyzer -ErrorAction SilentlyContinue).Version } $results = Find-Recursively -IncludeFile "*.ps1", "*.psm1", "*.psd1" -ExcludeDirectory node_modules | - ? { # Exclude /TestSolutions/Violate-Analyzers.ps1 and /TestSolutions/*/Violate-Analyzers.ps1 + Where-Object { # Exclude /TestSolutions/Violate-Analyzers.ps1 and /TestSolutions/*/Violate-Analyzers.ps1 $IncludeTestSolutions -or -not ( $_.Name -eq 'Violate-Analyzers.ps1' -and ($_.Directory.Name -eq 'TestSolutions' -or $_.Directory.Parent.Name -eq 'TestSolutions')) } | - % { Invoke-ScriptAnalyzer $_.FullName -Settings $SettingsPath.FullName } | + ForEach-Object { Invoke-ScriptAnalyzer $_.FullName -Settings $SettingsPath.FullName } | # Only Warning and above (ignore "Information" type results). - ? { $_.Severity -ge [Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticSeverity]::Warning } + Where-Object { $_.Severity -ge [Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticSeverity]::Warning } foreach ($result in $results) { From 418362e9a69e17a070d254506f50e5b8a425667d Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Fri, 18 Nov 2022 19:51:26 +0100 Subject: [PATCH 03/28] Updating Invoke-Analyzer to use $PSItem instead of $_ and fixing line lengths --- Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 b/Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 index bc1b5bfb..8b271a04 100644 --- a/Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 +++ b/Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 @@ -5,10 +5,11 @@ [Switch] $IncludeTestSolutions ) -# This is like Get-ChildItem -Recurse -Include $IncludeFile | ? { $_.FullName -notlike "*\$ExcludeDirectory\*" } but -# much faster. For example, this is relevant for ignoring node_modules. +# This is like Get-ChildItem -Recurse -Include $IncludeFile | ? { $PSItem.FullName -notlike "*\$ExcludeDirectory\*" } +# but much faster. For example, this is relevant for ignoring node_modules. # - Measure-Command { Find-Recursively -Path . -IncludeFile *.ps1 -ExcludeDirectory node_modules } => 3.83s -# - Measure-Command { Get-ChildItem -Recurse -Force -Include $IncludeFile | ? { $_.FullName -notlike "*\$ExcludeDirectory\*" } } => 111.27s +# - Measure-Command { Get-ChildItem -Recurse -Force -Include $IncludeFile | ? { $PSItem.FullName -notlike +# "*\$ExcludeDirectory\*" } } => 111.27s function Find-Recursively([string] $Path = '.', [string[]] $IncludeFile, [string] $ExcludeDirectory) { $ExcludeDirectory = $ExcludeDirectory.ToUpperInvariant() @@ -25,7 +26,7 @@ function Find-Recursively([string] $Path = '.', [string[]] $IncludeFile, [string foreach ($child in (Get-ChildItem $Here.FullName -Force)) { if ($child -is [System.IO.DirectoryInfo]) { Find-Inner $child } - elseif (($IncludeFile | Where-Object { $child.name -like $_ }).Count) { $child } + elseif (($IncludeFile | Where-Object { $child.name -like $PSItem }).Count) { $child } } } @@ -91,11 +92,11 @@ if ((Get-InstalledModule PSScriptAnalyzer -ErrorAction SilentlyContinue).Version $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 ( - $_.Name -eq 'Violate-Analyzers.ps1' -and - ($_.Directory.Name -eq 'TestSolutions' -or $_.Directory.Parent.Name -eq 'TestSolutions')) } | - ForEach-Object { Invoke-ScriptAnalyzer $_.FullName -Settings $SettingsPath.FullName } | + $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 -Settings $SettingsPath.FullName } | # Only Warning and above (ignore "Information" type results). - Where-Object { $_.Severity -ge [Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticSeverity]::Warning } + Where-Object { $PSItem.Severity -ge [Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticSeverity]::Warning } foreach ($result in $results) { From 8544014e8b8ca9b005b2041369c42df5f852c588 Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Fri, 18 Nov 2022 20:40:46 +0100 Subject: [PATCH 04/28] Updating comment in Violate-Analyzers.ps1 as aliases will no longer be allowed --- 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 a761c847..182baab1 100644 --- a/TestSolutions/Violate-Analyzers.ps1 +++ b/TestSolutions/Violate-Analyzers.ps1 @@ -5,4 +5,4 @@ function Violate-Analyzers() try { echo Violate-Analyzers } catch { } -Get-ChildItem . | % { "This is permitted by our settings file." } +Get-ChildItem . | % { $_ } From 93bc3e941c55a7e9934773533dadcb4cc67eaeff Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Fri, 18 Nov 2022 20:43:28 +0100 Subject: [PATCH 05/28] Updating Invoke-Analyzer.ps1 to allow the inclusion of custom analyzer rules --- Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 b/Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 index 8b271a04..fe98c82a 100644 --- a/Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 +++ b/Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 @@ -89,12 +89,18 @@ if ((Get-InstalledModule PSScriptAnalyzer -ErrorAction SilentlyContinue).Version } } +$analyzerParameters = @{ + "Settings" = $SettingsPath.FullName + "CustomRulePath" = Join-Path -Path (Split-Path $MyInvocation.MyCommand.Path -Parent) -ChildPath Rules + "RecurseCustomRulePath" = $true + "IncludeDefaultRules" = $true +} $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 ($PSItem.Directory.Name -eq 'TestSolutions' -or $PSItem.Directory.Parent.Name -eq 'TestSolutions')) } | - ForEach-Object { Invoke-ScriptAnalyzer -Path $PSItem.FullName -Settings $SettingsPath.FullName } | + 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 } From 58ea559de20b685d8e9a03af898d60b9917e3f00 Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Fri, 18 Nov 2022 21:43:06 +0100 Subject: [PATCH 06/28] Adding the Measure-AutomaticVariableAlias custom analyzer rule --- .../Measure-AutomaticVariableAlias.psm1 | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 Lombiq.Analyzers.PowerShell/Rules/Measure-AutomaticVariableAlias/Measure-AutomaticVariableAlias.psm1 diff --git a/Lombiq.Analyzers.PowerShell/Rules/Measure-AutomaticVariableAlias/Measure-AutomaticVariableAlias.psm1 b/Lombiq.Analyzers.PowerShell/Rules/Measure-AutomaticVariableAlias/Measure-AutomaticVariableAlias.psm1 new file mode 100644 index 00000000..08f8261e --- /dev/null +++ b/Lombiq.Analyzers.PowerShell/Rules/Measure-AutomaticVariableAlias/Measure-AutomaticVariableAlias.psm1 @@ -0,0 +1,70 @@ +<# +.SYNOPSIS + Replaces usages of the alias of the automatic variable ($_) with its original form ($PSItem). +.DESCRIPTION + Replaces usages of the alias of the automatic variable ($_) with its original form ($PSItem), similarly to the + AvoidUsingCmdletAliases rule. +.EXAMPLE + Measure-AutomaticVariableAlias -Token $Token +.INPUTS + [System.Management.Automation.Language.Token[]] +.OUTPUTS + [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord[]] +.NOTES + Copied from + https://github.com/PowerShell/PSScriptAnalyzer/blob/master/Tests/Engine/CommunityAnalyzerRules/CommunityAnalyzerRules.psm1#L613. +#> +function Measure-AutomaticVariableAlias +{ + [CmdletBinding()] + [OutputType([Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord[]])] + Param + ( + [Parameter(Mandatory = $true)] + [ValidateNotNullOrEmpty()] + [System.Management.Automation.Language.Token[]] + $Token + ) + + Process + { + $results = @() + + try + { + # Finds the usages of the alias ($_) of the automatic variable ($PSItem). + $lcTokens = $Token | Where-Object { $PSItem.GetType().Name -eq "VariableToken" -and $PSItem.Name -eq "_" } + + foreach ($lcToken in $lcTokens) + { + $correctionTypeName = "Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.CorrectionExtent" + $correctionExtent = New-Object -TypeName $correctionTypeName -ArgumentList @( + $lcToken.Extent.StartLineNumber + $lcToken.Extent.EndLineNumber + $lcToken.Extent.StartColumnNumber + $lcToken.Extent.EndColumnNumber + "`$PSItem" + "Replaced the alias of the automatic variable (`$_) with `"`$PSItem`"." + ) + + $suggestedCorrections = New-Object System.Collections.ObjectModel.Collection[$correctionTypeName] + $suggestedCorrections.add($correctionExtent) | Out-Null + + $results += [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord]@{ + "Message" = "Use the full name of the automatic variable (`$PSItem) instead of `"`$_`"!" + "Extent" = $lcToken.Extent + "RuleName" = $PSCmdlet.MyInvocation.InvocationName + "Severity" = "Warning" + "RuleSuppressionID" = "PSAvoidAutomaticVariableAlias" + "SuggestedCorrections" = $suggestedCorrections + } + } + + return $results + } + catch + { + $PSCmdlet.ThrowTerminatingError($PSItem) + } + } +} From fdc5acc07df3ad1107904e186dd6875fec1da054 Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Mon, 21 Nov 2022 13:48:53 +0100 Subject: [PATCH 07/28] Updating comments, user messages and variable names in Measure-AutomaticVariableAlias custom rule --- .../Measure-AutomaticVariableAlias.psm1 | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/Lombiq.Analyzers.PowerShell/Rules/Measure-AutomaticVariableAlias/Measure-AutomaticVariableAlias.psm1 b/Lombiq.Analyzers.PowerShell/Rules/Measure-AutomaticVariableAlias/Measure-AutomaticVariableAlias.psm1 index 08f8261e..220cd86e 100644 --- a/Lombiq.Analyzers.PowerShell/Rules/Measure-AutomaticVariableAlias/Measure-AutomaticVariableAlias.psm1 +++ b/Lombiq.Analyzers.PowerShell/Rules/Measure-AutomaticVariableAlias/Measure-AutomaticVariableAlias.psm1 @@ -11,7 +11,7 @@ .OUTPUTS [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord[]] .NOTES - Copied from + Copied (and modified version of) https://github.com/PowerShell/PSScriptAnalyzer/blob/master/Tests/Engine/CommunityAnalyzerRules/CommunityAnalyzerRules.psm1#L613. #> function Measure-AutomaticVariableAlias @@ -32,27 +32,25 @@ function Measure-AutomaticVariableAlias try { - # Finds the usages of the alias ($_) of the automatic variable ($PSItem). - $lcTokens = $Token | Where-Object { $PSItem.GetType().Name -eq "VariableToken" -and $PSItem.Name -eq "_" } - - foreach ($lcToken in $lcTokens) + # 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 "_" }) { $correctionTypeName = "Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.CorrectionExtent" $correctionExtent = New-Object -TypeName $correctionTypeName -ArgumentList @( - $lcToken.Extent.StartLineNumber - $lcToken.Extent.EndLineNumber - $lcToken.Extent.StartColumnNumber - $lcToken.Extent.EndColumnNumber + $automaticVariableAliasToken.Extent.StartLineNumber + $automaticVariableAliasToken.Extent.EndLineNumber + $automaticVariableAliasToken.Extent.StartColumnNumber + $automaticVariableAliasToken.Extent.EndColumnNumber "`$PSItem" - "Replaced the alias of the automatic variable (`$_) with `"`$PSItem`"." + "Replaced the alias of the automatic variable `"`$_`" with `"`$PSItem`"." ) $suggestedCorrections = New-Object System.Collections.ObjectModel.Collection[$correctionTypeName] $suggestedCorrections.add($correctionExtent) | Out-Null $results += [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord]@{ - "Message" = "Use the full name of the automatic variable (`$PSItem) instead of `"`$_`"!" - "Extent" = $lcToken.Extent + "Message" = "Use the full name of the automatic variable `"`$PSItem`" instead of `"`$_`"!" + "Extent" = $automaticVariableAliasToken.Extent "RuleName" = $PSCmdlet.MyInvocation.InvocationName "Severity" = "Warning" "RuleSuppressionID" = "PSAvoidAutomaticVariableAlias" From d870a9dd8c4e2217e63a4bdf33dfbe38571fe019 Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Mon, 21 Nov 2022 13:49:19 +0100 Subject: [PATCH 08/28] Adding "Fix" switch to Invoke-Analyzers.ps1 --- Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 b/Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 index fe98c82a..3954294c 100644 --- a/Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 +++ b/Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 @@ -2,7 +2,8 @@ $SettingsPath = (Join-Path (Split-Path -Parent $MyInvocation.MyCommand.Path) 'PSScriptAnalyzerSettings.psd1'), [Switch] $ForGitHubActions, [Switch] $ForMsBuild, - [Switch] $IncludeTestSolutions + [Switch] $IncludeTestSolutions, + [switch] $Fix ) # This is like Get-ChildItem -Recurse -Include $IncludeFile | ? { $PSItem.FullName -notlike "*\$ExcludeDirectory\*" } @@ -94,6 +95,7 @@ $analyzerParameters = @{ "CustomRulePath" = Join-Path -Path (Split-Path $MyInvocation.MyCommand.Path -Parent) -ChildPath Rules "RecurseCustomRulePath" = $true "IncludeDefaultRules" = $true + "Fix" = $Fix } $results = Find-Recursively -IncludeFile "*.ps1", "*.psm1", "*.psd1" -ExcludeDirectory node_modules | Where-Object { # Exclude /TestSolutions/Violate-Analyzers.ps1 and /TestSolutions/*/Violate-Analyzers.ps1 From b0415e2dbdc05e4b08a1ac4900f5df4d12da5aa6 Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Mon, 21 Nov 2022 14:25:12 +0100 Subject: [PATCH 09/28] Fixing Measure-AutomaticVariableAlias to correctly surface a rule ID, updating unit test --- .../UnitTests/PowerShellAnalysisTests.cs | 1 + .../Measure-AutomaticVariableAlias.psm1 | 8 ++++---- TestSolutions/Violate-Analyzers.ps1 | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Lombiq.Analyzers.PowerShell.Tests/UnitTests/PowerShellAnalysisTests.cs b/Lombiq.Analyzers.PowerShell.Tests/UnitTests/PowerShellAnalysisTests.cs index 08d6fa20..52cbd136 100644 --- a/Lombiq.Analyzers.PowerShell.Tests/UnitTests/PowerShellAnalysisTests.cs +++ b/Lombiq.Analyzers.PowerShell.Tests/UnitTests/PowerShellAnalysisTests.cs @@ -51,6 +51,7 @@ await _powerShell private static void MessageShouldContainViolationCodes(string message) { + message.ShouldContain("PSAvoidAutomaticVariableAlias"); message.ShouldContain("PSAvoidUsingEmptyCatchBlock"); message.ShouldContain("PSAvoidUsingCmdletAliases"); message.ShouldContain("PSUseApprovedVerbs"); diff --git a/Lombiq.Analyzers.PowerShell/Rules/Measure-AutomaticVariableAlias/Measure-AutomaticVariableAlias.psm1 b/Lombiq.Analyzers.PowerShell/Rules/Measure-AutomaticVariableAlias/Measure-AutomaticVariableAlias.psm1 index 220cd86e..a24687f3 100644 --- a/Lombiq.Analyzers.PowerShell/Rules/Measure-AutomaticVariableAlias/Measure-AutomaticVariableAlias.psm1 +++ b/Lombiq.Analyzers.PowerShell/Rules/Measure-AutomaticVariableAlias/Measure-AutomaticVariableAlias.psm1 @@ -42,18 +42,18 @@ function Measure-AutomaticVariableAlias $automaticVariableAliasToken.Extent.StartColumnNumber $automaticVariableAliasToken.Extent.EndColumnNumber "`$PSItem" - "Replaced the alias of the automatic variable `"`$_`" with `"`$PSItem`"." + "Replaced the alias of the automatic variable '`$' with '`$PSItem'." ) $suggestedCorrections = New-Object System.Collections.ObjectModel.Collection[$correctionTypeName] $suggestedCorrections.add($correctionExtent) | Out-Null $results += [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord]@{ - "Message" = "Use the full name of the automatic variable `"`$PSItem`" instead of `"`$_`"!" "Extent" = $automaticVariableAliasToken.Extent - "RuleName" = $PSCmdlet.MyInvocation.InvocationName + "Message" = "Use the full name of the automatic variable '`$PSItem' instead of '`$_'!" + "RuleName" = "PSAvoidUsingAutomaticVariableAlias" + "RuleSuppressionID" = "PSAvoidUsingAutomaticVariableAlias" "Severity" = "Warning" - "RuleSuppressionID" = "PSAvoidAutomaticVariableAlias" "SuggestedCorrections" = $suggestedCorrections } } diff --git a/TestSolutions/Violate-Analyzers.ps1 b/TestSolutions/Violate-Analyzers.ps1 index 182baab1..fb488d69 100644 --- a/TestSolutions/Violate-Analyzers.ps1 +++ b/TestSolutions/Violate-Analyzers.ps1 @@ -3,6 +3,6 @@ function Violate-Analyzers() "This file is intended made to verify that PSScriptAnalyzer works and contains intentionally bad code." } -try { echo Violate-Analyzers } catch { } +try { Violate-Analyzers } catch { } Get-ChildItem . | % { $_ } From edfe4eb422db6db580f5d2448e6267c50b5817fd Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Mon, 21 Nov 2022 14:38:51 +0100 Subject: [PATCH 10/28] Attempting to fix unit test by reordering assertions to match Violate-Analyzers.ps1 --- .../UnitTests/PowerShellAnalysisTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lombiq.Analyzers.PowerShell.Tests/UnitTests/PowerShellAnalysisTests.cs b/Lombiq.Analyzers.PowerShell.Tests/UnitTests/PowerShellAnalysisTests.cs index 52cbd136..0796dfd8 100644 --- a/Lombiq.Analyzers.PowerShell.Tests/UnitTests/PowerShellAnalysisTests.cs +++ b/Lombiq.Analyzers.PowerShell.Tests/UnitTests/PowerShellAnalysisTests.cs @@ -51,10 +51,10 @@ await _powerShell private static void MessageShouldContainViolationCodes(string message) { - message.ShouldContain("PSAvoidAutomaticVariableAlias"); message.ShouldContain("PSAvoidUsingEmptyCatchBlock"); message.ShouldContain("PSAvoidUsingCmdletAliases"); message.ShouldContain("PSUseApprovedVerbs"); message.ShouldContain("PSUseSingularNouns"); + message.ShouldContain("PSAvoidAutomaticVariableAlias"); } } From b52dca12ba1af92155ef64b8a1c05245dc5bb7d6 Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Mon, 21 Nov 2022 14:51:33 +0100 Subject: [PATCH 11/28] Attempting to fix unit test by reordering assertions to match Violate-Analyzers.ps1 --- .../UnitTests/PowerShellAnalysisTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lombiq.Analyzers.PowerShell.Tests/UnitTests/PowerShellAnalysisTests.cs b/Lombiq.Analyzers.PowerShell.Tests/UnitTests/PowerShellAnalysisTests.cs index 0796dfd8..4cfb25bb 100644 --- a/Lombiq.Analyzers.PowerShell.Tests/UnitTests/PowerShellAnalysisTests.cs +++ b/Lombiq.Analyzers.PowerShell.Tests/UnitTests/PowerShellAnalysisTests.cs @@ -52,9 +52,9 @@ await _powerShell private static void MessageShouldContainViolationCodes(string message) { message.ShouldContain("PSAvoidUsingEmptyCatchBlock"); - message.ShouldContain("PSAvoidUsingCmdletAliases"); message.ShouldContain("PSUseApprovedVerbs"); message.ShouldContain("PSUseSingularNouns"); + message.ShouldContain("PSAvoidUsingCmdletAliases"); message.ShouldContain("PSAvoidAutomaticVariableAlias"); } } From 18a463410d8f8096da45c85d262d451829710367 Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Mon, 21 Nov 2022 14:59:29 +0100 Subject: [PATCH 12/28] Attempting to fix unit test by reordering assertions to match Violate-Analyzers.ps1 --- .../UnitTests/PowerShellAnalysisTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lombiq.Analyzers.PowerShell.Tests/UnitTests/PowerShellAnalysisTests.cs b/Lombiq.Analyzers.PowerShell.Tests/UnitTests/PowerShellAnalysisTests.cs index 4cfb25bb..3022faac 100644 --- a/Lombiq.Analyzers.PowerShell.Tests/UnitTests/PowerShellAnalysisTests.cs +++ b/Lombiq.Analyzers.PowerShell.Tests/UnitTests/PowerShellAnalysisTests.cs @@ -51,10 +51,10 @@ await _powerShell private static void MessageShouldContainViolationCodes(string message) { + message.ShouldContain("PSAvoidAutomaticVariableAlias"); + message.ShouldContain("PSAvoidUsingCmdletAliases"); message.ShouldContain("PSAvoidUsingEmptyCatchBlock"); message.ShouldContain("PSUseApprovedVerbs"); message.ShouldContain("PSUseSingularNouns"); - message.ShouldContain("PSAvoidUsingCmdletAliases"); - message.ShouldContain("PSAvoidAutomaticVariableAlias"); } } From 6307430c2dae136c9107191886822ffc15039b57 Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Mon, 21 Nov 2022 15:23:09 +0100 Subject: [PATCH 13/28] Fixing PowerShellAnalysisTests by referencing the correct analyzer rule ID --- .../UnitTests/PowerShellAnalysisTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lombiq.Analyzers.PowerShell.Tests/UnitTests/PowerShellAnalysisTests.cs b/Lombiq.Analyzers.PowerShell.Tests/UnitTests/PowerShellAnalysisTests.cs index 3022faac..5dec71c3 100644 --- a/Lombiq.Analyzers.PowerShell.Tests/UnitTests/PowerShellAnalysisTests.cs +++ b/Lombiq.Analyzers.PowerShell.Tests/UnitTests/PowerShellAnalysisTests.cs @@ -51,7 +51,7 @@ await _powerShell private static void MessageShouldContainViolationCodes(string message) { - message.ShouldContain("PSAvoidAutomaticVariableAlias"); + message.ShouldContain("PSAvoidUsingAutomaticVariableAlias"); message.ShouldContain("PSAvoidUsingCmdletAliases"); message.ShouldContain("PSAvoidUsingEmptyCatchBlock"); message.ShouldContain("PSUseApprovedVerbs"); From 6c46c5e911520ca6a22292046eae2e8f90dfdba7 Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Mon, 21 Nov 2022 16:05:48 +0100 Subject: [PATCH 14/28] Updating the user-facing message of PSAvoidUsingAutomaticVariableAlias to match the style of existing analyzer rules, updating workflow to expect this analyzer --- .github/workflows/test-analysis-failure.yml | 2 ++ .../Measure-AutomaticVariableAlias.psm1 | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test-analysis-failure.yml b/.github/workflows/test-analysis-failure.yml index f6ab4deb..d0e4e90b 100644 --- a/.github/workflows/test-analysis-failure.yml +++ b/.github/workflows/test-analysis-failure.yml @@ -17,6 +17,7 @@ jobs: timeout-minutes: 30 build-expected-code-analysis-errors: | MSB3073: The command exited with non-zero code. + PSAvoidUsingAutomaticVariableAlias: '$_' is an alias of '$PSItem'. PSAvoidUsingEmptyCatchBlock: Empty catch block is used. PSAvoidUsingCmdletAliases: 'echo' is an alias of 'Write-Output'. PSUseApprovedVerbs: The cmdlet 'Violate-Analyzers' uses an unapproved verb. @@ -31,6 +32,7 @@ jobs: timeout-minutes: 30 build-expected-code-analysis-errors: | MSB3073: The command exited with non-zero code. + PSAvoidUsingAutomaticVariableAlias: '$_' is an alias of '$PSItem'. PSAvoidUsingEmptyCatchBlock: Empty catch block is used. PSAvoidUsingCmdletAliases: 'echo' is an alias of 'Write-Output'. PSUseApprovedVerbs: The cmdlet 'Violate-Analyzers' uses an unapproved verb. diff --git a/Lombiq.Analyzers.PowerShell/Rules/Measure-AutomaticVariableAlias/Measure-AutomaticVariableAlias.psm1 b/Lombiq.Analyzers.PowerShell/Rules/Measure-AutomaticVariableAlias/Measure-AutomaticVariableAlias.psm1 index a24687f3..9702959d 100644 --- a/Lombiq.Analyzers.PowerShell/Rules/Measure-AutomaticVariableAlias/Measure-AutomaticVariableAlias.psm1 +++ b/Lombiq.Analyzers.PowerShell/Rules/Measure-AutomaticVariableAlias/Measure-AutomaticVariableAlias.psm1 @@ -42,7 +42,7 @@ function Measure-AutomaticVariableAlias $automaticVariableAliasToken.Extent.StartColumnNumber $automaticVariableAliasToken.Extent.EndColumnNumber "`$PSItem" - "Replaced the alias of the automatic variable '`$' with '`$PSItem'." + "Replaced the alias of the automatic variable '`$_' with '`$PSItem'." ) $suggestedCorrections = New-Object System.Collections.ObjectModel.Collection[$correctionTypeName] @@ -50,7 +50,7 @@ function Measure-AutomaticVariableAlias $results += [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord]@{ "Extent" = $automaticVariableAliasToken.Extent - "Message" = "Use the full name of the automatic variable '`$PSItem' instead of '`$_'!" + "Message" = "'`$_' is an alias of '`$PSItem'." "RuleName" = "PSAvoidUsingAutomaticVariableAlias" "RuleSuppressionID" = "PSAvoidUsingAutomaticVariableAlias" "Severity" = "Warning" From c9a09c689646b7a7309a1f06e75163ed90b0ba4b Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Mon, 21 Nov 2022 16:23:26 +0100 Subject: [PATCH 15/28] Updating user-facing messages in Measure-AutomaticVariableAlias --- .github/workflows/test-analysis-failure.yml | 4 ++-- .../Measure-AutomaticVariableAlias.psm1 | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/.github/workflows/test-analysis-failure.yml b/.github/workflows/test-analysis-failure.yml index d0e4e90b..097556de 100644 --- a/.github/workflows/test-analysis-failure.yml +++ b/.github/workflows/test-analysis-failure.yml @@ -18,8 +18,8 @@ jobs: 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. - PSAvoidUsingCmdletAliases: 'echo' is an alias of 'Write-Output'. PSUseApprovedVerbs: The cmdlet 'Violate-Analyzers' uses an unapproved verb. PSUseSingularNouns: The cmdlet 'Violate-Analyzers' uses a plural noun. @@ -33,7 +33,7 @@ jobs: 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. - PSAvoidUsingCmdletAliases: 'echo' is an alias of 'Write-Output'. PSUseApprovedVerbs: The cmdlet 'Violate-Analyzers' uses an unapproved verb. PSUseSingularNouns: The cmdlet 'Violate-Analyzers' uses a plural noun. diff --git a/Lombiq.Analyzers.PowerShell/Rules/Measure-AutomaticVariableAlias/Measure-AutomaticVariableAlias.psm1 b/Lombiq.Analyzers.PowerShell/Rules/Measure-AutomaticVariableAlias/Measure-AutomaticVariableAlias.psm1 index 9702959d..38cb0af9 100644 --- a/Lombiq.Analyzers.PowerShell/Rules/Measure-AutomaticVariableAlias/Measure-AutomaticVariableAlias.psm1 +++ b/Lombiq.Analyzers.PowerShell/Rules/Measure-AutomaticVariableAlias/Measure-AutomaticVariableAlias.psm1 @@ -42,7 +42,7 @@ function Measure-AutomaticVariableAlias $automaticVariableAliasToken.Extent.StartColumnNumber $automaticVariableAliasToken.Extent.EndColumnNumber "`$PSItem" - "Replaced the alias of the automatic variable '`$_' with '`$PSItem'." + "Replaced the usage of the alias of the automatic variable '`$_' with its full content '`$PSItem'." ) $suggestedCorrections = New-Object System.Collections.ObjectModel.Collection[$correctionTypeName] @@ -50,7 +50,8 @@ function Measure-AutomaticVariableAlias $results += [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord]@{ "Extent" = $automaticVariableAliasToken.Extent - "Message" = "'`$_' is an alias of '`$PSItem'." + "Message" = "'`$_' is an alias of '`$PSItem'. Alias can introduce possible problems" + + " and make scripts hard to maintain. Please consider changing alias to its full content." "RuleName" = "PSAvoidUsingAutomaticVariableAlias" "RuleSuppressionID" = "PSAvoidUsingAutomaticVariableAlias" "Severity" = "Warning" From 62bc1c21cdbcc9c1ee76c05a2b06c83c9afcb45e Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Tue, 22 Nov 2022 19:30:37 +0100 Subject: [PATCH 16/28] test-analysis-failure workflow: Adding MSB3075 to the expected list of errors, which (it seems) is only thrown (instead of MSB3073) when there are exactly 5 analyzer violations --- .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 097556de..69fd60b7 100644 --- a/.github/workflows/test-analysis-failure.yml +++ b/.github/workflows/test-analysis-failure.yml @@ -17,6 +17,7 @@ jobs: timeout-minutes: 30 build-expected-code-analysis-errors: | MSB3073: The command exited with non-zero code. + MSB3075 PSAvoidUsingAutomaticVariableAlias: '$_' is an alias of '$PSItem'. PSAvoidUsingCmdletAliases: '%' is an alias of 'ForEach-Object'. PSAvoidUsingEmptyCatchBlock: Empty catch block is used. @@ -32,6 +33,7 @@ jobs: timeout-minutes: 30 build-expected-code-analysis-errors: | MSB3073: The command exited with non-zero code. + MSB3075 PSAvoidUsingAutomaticVariableAlias: '$_' is an alias of '$PSItem'. PSAvoidUsingCmdletAliases: '%' is an alias of 'ForEach-Object'. PSAvoidUsingEmptyCatchBlock: Empty catch block is used. From 9a8dbc06976606ed6d1d1e3579875062bc8d74ae Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Wed, 23 Nov 2022 12:49:24 +0100 Subject: [PATCH 17/28] Fixing that Invoke-Analyzer.ps1 shouldn't use the number of violations as exit code, because it trips up MSBuild --- .github/workflows/test-analysis-failure.yml | 2 -- Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 | 5 ++++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test-analysis-failure.yml b/.github/workflows/test-analysis-failure.yml index 69fd60b7..097556de 100644 --- a/.github/workflows/test-analysis-failure.yml +++ b/.github/workflows/test-analysis-failure.yml @@ -17,7 +17,6 @@ jobs: timeout-minutes: 30 build-expected-code-analysis-errors: | MSB3073: The command exited with non-zero code. - MSB3075 PSAvoidUsingAutomaticVariableAlias: '$_' is an alias of '$PSItem'. PSAvoidUsingCmdletAliases: '%' is an alias of 'ForEach-Object'. PSAvoidUsingEmptyCatchBlock: Empty catch block is used. @@ -33,7 +32,6 @@ jobs: timeout-minutes: 30 build-expected-code-analysis-errors: | MSB3073: The command exited with non-zero code. - MSB3075 PSAvoidUsingAutomaticVariableAlias: '$_' is an alias of '$PSItem'. PSAvoidUsingCmdletAliases: '%' is an alias of 'ForEach-Object'. PSAvoidUsingEmptyCatchBlock: Empty catch block is used. diff --git a/Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 b/Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 index 3954294c..1a4e34cf 100644 --- a/Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 +++ b/Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 @@ -112,4 +112,7 @@ foreach ($result in $results) Write-FileError -Path $result.ScriptPath -Line $result.Line -Column $result.Column $message } -exit $results.Count +# Exit code indicates the existence of analyzer violations instead of the number of violations, because exiting with +# code 5 (when there are 5 violations) changes how MSBuild interprets the results and yields the error MSB3075 instead +# of MSB3073 for some reason. +exit $results.Count -eq 0 ? 0 : 1 From ad4f6847204c0a727ca40e7741cd6af55ca7605d Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Wed, 23 Nov 2022 13:20:35 +0100 Subject: [PATCH 18/28] Updating Lombiq.Analyzers.PowerShell.PackageReference test project to use the beta package --- .../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 b19ce4c5..eac19ed0 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 44c244b62295fdc05c5058c70e4ccaff8f5eee5a Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Wed, 23 Nov 2022 13:56:13 +0100 Subject: [PATCH 19/28] Fixing that custom analyzer rules should be included in the NuGet package --- Lombiq.Analyzers.PowerShell/Lombiq.Analyzers.PowerShell.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/Lombiq.Analyzers.PowerShell/Lombiq.Analyzers.PowerShell.csproj b/Lombiq.Analyzers.PowerShell/Lombiq.Analyzers.PowerShell.csproj index 8c2bbcff..6df8acba 100644 --- a/Lombiq.Analyzers.PowerShell/Lombiq.Analyzers.PowerShell.csproj +++ b/Lombiq.Analyzers.PowerShell/Lombiq.Analyzers.PowerShell.csproj @@ -29,6 +29,7 @@ + From 40cedf9a1c89877c18ced0f8f780ba06e828c7eb Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Wed, 23 Nov 2022 14:33:31 +0100 Subject: [PATCH 20/28] Updating Lombiq.Analyzers.PowerShell.PackageReference test project to use the new beta package --- .../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 eac19ed0..59c77b27 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 9c64cd9e091064c19385adbb63da132b477203c7 Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Wed, 23 Nov 2022 17:28:32 +0100 Subject: [PATCH 21/28] Adding static-code-analysis workflow so that Invoke-Analyzer.ps1 can analyze itself --- .github/workflows/static-code-analysis.yml | 38 ++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 .github/workflows/static-code-analysis.yml diff --git a/.github/workflows/static-code-analysis.yml b/.github/workflows/static-code-analysis.yml new file mode 100644 index 00000000..f73b4f72 --- /dev/null +++ b/.github/workflows/static-code-analysis.yml @@ -0,0 +1,38 @@ +name: Static Code Analysis + +# Runs for PRs opened for any branch, and pushes to the dev branch. +on: + pull_request: + push: + branches: + - dev + +defaults: + run: + shell: pwsh + +jobs: + powershell-static-code-analysis: + name: PowerShell Static Code Analysis + strategy: + matrix: + machine-type: [ubuntu-latest, windows-latest] + runs-on: ${{ matrix.machine-type }} + + steps: + - uses: actions/checkout@v3 + with: + submodules: 'recursive' + + - name: Windows PowerShell Static Code Analysis + run: | + if ($Env:RUNNER_OS -ne 'Windows') + { + Write-Output "This step is only for Windows. The current runner is $Env:RUNNER_OS so it's skipped." + exit 0 + } + powershell .\Lombiq.Analyzers.PowerShell\Invoke-Analyzer.ps1 -ForGitHubActions + + - name: PowerShell 7+ Static Code Analysis + if: always() # Even if the above fails, let's check both. + run: .\Lombiq.Analyzers.PowerShell\Invoke-Analyzer.ps1 -ForGitHubActions From 9f0d4bbf2157d57617b5af5b6a7567977b3f8810 Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Wed, 23 Nov 2022 17:33:43 +0100 Subject: [PATCH 22/28] Fixing Windows PowerShell compatbility with how exit code is returned and also simplifying it --- Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 b/Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 index 1a4e34cf..a57f5857 100644 --- a/Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 +++ b/Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1 @@ -115,4 +115,7 @@ foreach ($result in $results) # Exit code indicates the existence of analyzer violations instead of the number of violations, because exiting with # code 5 (when there are 5 violations) changes how MSBuild interprets the results and yields the error MSB3075 instead # of MSB3073 for some reason. -exit $results.Count -eq 0 ? 0 : 1 +if ($results.Count -ne 0) +{ + exit 1 +} From ed60fb1d115bf42fb474d1112952848987bfeadb Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Wed, 23 Nov 2022 17:51:02 +0100 Subject: [PATCH 23/28] Update Lombiq.Analyzers.PowerShell/Rules/Measure-AutomaticVariableAlias/Measure-AutomaticVariableAlias.psm1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Dávid El-Saig --- .../Measure-AutomaticVariableAlias.psm1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lombiq.Analyzers.PowerShell/Rules/Measure-AutomaticVariableAlias/Measure-AutomaticVariableAlias.psm1 b/Lombiq.Analyzers.PowerShell/Rules/Measure-AutomaticVariableAlias/Measure-AutomaticVariableAlias.psm1 index 38cb0af9..9d56e5be 100644 --- a/Lombiq.Analyzers.PowerShell/Rules/Measure-AutomaticVariableAlias/Measure-AutomaticVariableAlias.psm1 +++ b/Lombiq.Analyzers.PowerShell/Rules/Measure-AutomaticVariableAlias/Measure-AutomaticVariableAlias.psm1 @@ -11,7 +11,7 @@ .OUTPUTS [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord[]] .NOTES - Copied (and modified version of) + Copied (and modified) version of https://github.com/PowerShell/PSScriptAnalyzer/blob/master/Tests/Engine/CommunityAnalyzerRules/CommunityAnalyzerRules.psm1#L613. #> function Measure-AutomaticVariableAlias From d3f0ae46e8de016d3fad939345236f53ac054ca0 Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Wed, 23 Nov 2022 17:52:12 +0100 Subject: [PATCH 24/28] Update TestSolutions/Violate-Analyzers.ps1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Dávid El-Saig --- 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 fb488d69..aba85983 100644 --- a/TestSolutions/Violate-Analyzers.ps1 +++ b/TestSolutions/Violate-Analyzers.ps1 @@ -1,6 +1,6 @@ function Violate-Analyzers() { - "This file is intended made to verify that PSScriptAnalyzer works and contains intentionally bad code." + "This file is intended to verify that PSScriptAnalyzer works and contains intentionally bad code." } try { Violate-Analyzers } catch { } From 87e060a7d3d337d529e5ecd0278be04725b4338f Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Wed, 23 Nov 2022 17:47:32 +0100 Subject: [PATCH 25/28] Measure-AutomaticVariableAlias: Improving the syntax and wording of user messages --- .../Measure-AutomaticVariableAlias.psm1 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Lombiq.Analyzers.PowerShell/Rules/Measure-AutomaticVariableAlias/Measure-AutomaticVariableAlias.psm1 b/Lombiq.Analyzers.PowerShell/Rules/Measure-AutomaticVariableAlias/Measure-AutomaticVariableAlias.psm1 index 9d56e5be..9e42d737 100644 --- a/Lombiq.Analyzers.PowerShell/Rules/Measure-AutomaticVariableAlias/Measure-AutomaticVariableAlias.psm1 +++ b/Lombiq.Analyzers.PowerShell/Rules/Measure-AutomaticVariableAlias/Measure-AutomaticVariableAlias.psm1 @@ -41,8 +41,8 @@ function Measure-AutomaticVariableAlias $automaticVariableAliasToken.Extent.EndLineNumber $automaticVariableAliasToken.Extent.StartColumnNumber $automaticVariableAliasToken.Extent.EndColumnNumber - "`$PSItem" - "Replaced the usage of the alias of the automatic variable '`$_' with its full content '`$PSItem'." + '$PSItem' + 'Replaced the usage of the alias of the automatic variable ''$_'' with its full name ''$PSItem''.' ) $suggestedCorrections = New-Object System.Collections.ObjectModel.Collection[$correctionTypeName] @@ -50,8 +50,8 @@ function Measure-AutomaticVariableAlias $results += [Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord]@{ "Extent" = $automaticVariableAliasToken.Extent - "Message" = "'`$_' is an alias of '`$PSItem'. Alias can introduce possible problems" + - " and make scripts hard to maintain. Please consider changing alias to its full content." + "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" From 403b4dfaf311c959a5b5308b92450b832984313b Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Wed, 23 Nov 2022 19:47:52 +0100 Subject: [PATCH 26/28] Updating help text of Measure-AutomaticVariableAlias --- .../Measure-AutomaticVariableAlias.psm1 | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Lombiq.Analyzers.PowerShell/Rules/Measure-AutomaticVariableAlias/Measure-AutomaticVariableAlias.psm1 b/Lombiq.Analyzers.PowerShell/Rules/Measure-AutomaticVariableAlias/Measure-AutomaticVariableAlias.psm1 index 9e42d737..8e2a7e24 100644 --- a/Lombiq.Analyzers.PowerShell/Rules/Measure-AutomaticVariableAlias/Measure-AutomaticVariableAlias.psm1 +++ b/Lombiq.Analyzers.PowerShell/Rules/Measure-AutomaticVariableAlias/Measure-AutomaticVariableAlias.psm1 @@ -1,9 +1,8 @@ <# .SYNOPSIS - Replaces usages of the alias of the automatic variable ($_) with its original form ($PSItem). + Detects the usages of the alias ($_) of the automatic variable ($PSItem) and suggests to correct them. .DESCRIPTION - Replaces usages of the alias of the automatic variable ($_) with its original form ($PSItem), similarly to the - AvoidUsingCmdletAliases rule. + The full name of the automatic variable ($PSItem) should be used instead of its alias ($_) for consistency. .EXAMPLE Measure-AutomaticVariableAlias -Token $Token .INPUTS From 898b470568e0ed3bf727af6288eb83c64b8179bf Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Wed, 23 Nov 2022 19:48:04 +0100 Subject: [PATCH 27/28] Updating Readme with instructions for custom analyzer rules --- Readme.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Readme.md b/Readme.md index 15d718ae..ad81d946 100644 --- a/Readme.md +++ b/Readme.md @@ -80,6 +80,11 @@ Occasionally there is good reason to ignore an analyzer warning. In this case ad Suppressing a specific line or range (like `#pragma warning disable` in C#) is not currently supported. See [the associated PSScriptAnalyzer issue](https://github.com/PowerShell/PSScriptAnalyzer/issues/849). +### Implementing custom analyzer rules + +Check out [the official documentation](https://learn.microsoft.com/en-us/powershell/utility-modules/psscriptanalyzer/create-custom-rule) for instructions and [CommunityAnalyzerRules](https://github.com/PowerShell/PSScriptAnalyzer/blob/master/Tests/Engine/CommunityAnalyzerRules/CommunityAnalyzerRules.psm1) for additional inspiration. +Custom rule modules should be placed in the `Lombiq.Analyzers.PowerShell\Rules` folder to be automatically detected by the `Invoke-Analyzer` script. + ## Contributing and support Bug reports, feature requests, comments, questions, code contributions and love letters are warmly welcome. You can send them to us via GitHub issues and pull requests. Please adhere to our [open-source guidelines](https://lombiq.com/open-source-guidelines) while doing so. From bf49a0684389b3dc89e9e8124ef9885517960f3f Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Thu, 24 Nov 2022 09:26:36 +0100 Subject: [PATCH 28/28] Updating Measure-AutomaticVariableAlias help text to make it clear(er) as $PSItem is not the only automatic variable, but only one with an alias --- .../Measure-AutomaticVariableAlias.psm1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lombiq.Analyzers.PowerShell/Rules/Measure-AutomaticVariableAlias/Measure-AutomaticVariableAlias.psm1 b/Lombiq.Analyzers.PowerShell/Rules/Measure-AutomaticVariableAlias/Measure-AutomaticVariableAlias.psm1 index 8e2a7e24..ce70c4c7 100644 --- a/Lombiq.Analyzers.PowerShell/Rules/Measure-AutomaticVariableAlias/Measure-AutomaticVariableAlias.psm1 +++ b/Lombiq.Analyzers.PowerShell/Rules/Measure-AutomaticVariableAlias/Measure-AutomaticVariableAlias.psm1 @@ -1,8 +1,8 @@ <# .SYNOPSIS - Detects the usages of the alias ($_) of the automatic variable ($PSItem) and suggests to correct them. + Detects the usages of the alias $_ of the automatic variable $PSItem and suggests to correct them. .DESCRIPTION - The full name of the automatic variable ($PSItem) should be used instead of its alias ($_) for consistency. + The full name of the automatic variable $PSItem should be used instead of its alias $_ for consistency. .EXAMPLE Measure-AutomaticVariableAlias -Token $Token .INPUTS