Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OSOE-501: Enable further analyzer rules for better code styling control and more #20

Merged
merged 25 commits into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
6fec06b
Disabling "Test Analysis Failure - NuGet PackageReference" step in th…
BenedekFarkas Jan 10, 2023
e7cdc28
Updating Invoke-Analyzer.ps1 to require PSScriptAnalyzer version 1.21…
BenedekFarkas Jan 10, 2023
e5b448e
Enabling a bunch of analyzer rules
BenedekFarkas Jan 10, 2023
386a3c1
PSScriptAnalyzerSettings: "Paren" in PSUseConsistentWhitespace.CheckO…
BenedekFarkas Jan 10, 2023
03046b4
PSScriptAnalyzerSettings: Setting PSUseConsistentWhitespace.CheckPara…
BenedekFarkas Jan 10, 2023
fc65ffc
Updating test-analysis-failure workflow to expect PSUseConsistentInde…
BenedekFarkas Jan 11, 2023
8ec5ef1
Updating Violate-Analyzers and test-analysis-failure to check for PSA…
BenedekFarkas Jan 11, 2023
01f8b64
Not enabling PSUseCorrectCasing yet due to https://github.com/PowerSh…
BenedekFarkas Jan 11, 2023
11b5985
Attempting to dry the configuration for expected code analysis errors…
BenedekFarkas Jan 11, 2023
8098721
Revert "Attempting to dry the configuration for expected code analysi…
BenedekFarkas Jan 11, 2023
40d9fa3
Not ignoring Information-level analyzer violations anymore, but disab…
BenedekFarkas Jan 17, 2023
cb224ab
Fixing PSAvoidUsingDoubleQuotesForConstantString analyzer violations
BenedekFarkas Jan 17, 2023
2de89bd
Updating Violate-Analyzers.ps1 to violate PSAvoidUsingDoubleQuotesFor…
BenedekFarkas Jan 17, 2023
2c6226c
Adding PSAvoidUsingDoubleQuotesForConstantString to the list of expec…
BenedekFarkas Jan 17, 2023
9d3723a
Update Lombiq.Analyzers.PowerShell/Rules/Measure-LineContinuation/Mea…
BenedekFarkas Jan 17, 2023
8dc81e2
Updating the explanation of Violate-Analyzers.ps1
Jan 17, 2023
08e4d67
Fixing message formatting in Invoke-Invoke-Analyzer.ps1
BenedekFarkas Jan 17, 2023
d000ed3
Improved message formatting for variable assignment and defining the …
BenedekFarkas Jan 17, 2023
32527c6
Merge remote-tracking branch 'origin/dev' into issue/OSOE-501
BenedekFarkas Jan 18, 2023
21203aa
Re-enabling PSUseCorrectCasing analyzer rule, because https://github.…
BenedekFarkas Jan 18, 2023
a5b3d4a
Merge branch 'dev' into issue/OSOE-501
sarahelsaig Jan 31, 2023
3feafe7
Revert temporary change.
sarahelsaig Jan 31, 2023
95e3045
Revert "Revert temporary change."
sarahelsaig Jan 31, 2023
a378811
Update PackageReference version.
sarahelsaig Jan 31, 2023
2fd7762
Revert temporary change.
sarahelsaig Jan 31, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/test-analysis-failure.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ 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
PSAvoidUsingDoubleQuotesForConstantString: Use single quotes when a string is constant.

call-test-analysis-failure-local:
name: Test Analysis Failure - Local ProjectReference
Expand All @@ -39,3 +42,6 @@ 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
PSAvoidUsingDoubleQuotesForConstantString: Use single quotes when a string is constant.
22 changes: 11 additions & 11 deletions Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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
Expand All @@ -94,10 +94,12 @@ 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'
@(
'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
BenedekFarkas marked this conversation as resolved.
Show resolved Hide resolved
exit -2
}
}
Expand All @@ -109,18 +111,16 @@ $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
($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)
{
$message = $result.RuleName + ": " + $result.Message
$message = $result.RuleName + ': ' + $result.Message
Write-FileError -Path $result.ScriptPath -Line $result.Line -Column $result.Column $message
}

Expand Down
45 changes: 45 additions & 0 deletions Lombiq.Analyzers.PowerShell/PSScriptAnalyzerSettings.psd1
Original file line number Diff line number Diff line change
@@ -1,8 +1,53 @@
@{
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'
)
Rules = @{
PSAvoidSemicolonsAsLineTerminators = @{
Enable = $true
}
PSAvoidUsingDoubleQuotesForConstantString = @{
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
CheckOpenParen = $true
CheckOperator = $true
CheckPipe = $true
CheckPipeForRedundantWhitespace = $true
CheckSeparator = $true
CheckParameter = $false
IgnoreAssignmentOperatorInsideHashTable = $false
}
PSUseCorrectCasing = @{
Enable = $true
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,31 @@ 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 use the full name of this'
'variable for consistency.'
) -join ' '
BenedekFarkas marked this conversation as resolved.
Show resolved Hide resolved
'RuleName' = 'PSAvoidUsingAutomaticVariableAlias'
'RuleSuppressionID' = 'PSAvoidUsingAutomaticVariableAlias'
'Severity' = 'Warning'
'SuggestedCorrections' = $suggestedCorrections
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ 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.'
0liver marked this conversation as resolved.
Show resolved Hide resolved
"RuleName" = "PSAvoidUsingLineContinuation"
"RuleSuppressionID" = "PSAvoidUsingLineContinuation"
"Severity" = "Warning"
'Extent' = $lineContinuationToken.Extent
'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'
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Lombiq.Analyzers.PowerShell" Version="1.2.0-beta.0.osoe-482" />
<PackageReference Include="Lombiq.Analyzers.PowerShell" Version="1.2.0" />
</ItemGroup>

<Target Name="CopyFiles" BeforeTargets="PrepareForBuild">
Expand Down
4 changes: 2 additions & 2 deletions TestSolutions/Violate-Analyzers.ps1
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
function Violate-Analyzers()
{
"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 { }

"Lombiq", `
"Orchard", "Hastlayer" | % { $_ }
'Orchard', 'Hastlayer' | % { $_ }