PSScriptAnalyzer
PSScriptAnalyzer copied to clipboard
PSUseConsistentIndentation: Check indentation of lines where first token is a LParen not followed by comment or new line
PR Summary
Currently if an LParen (
is the first token on a line and the next token is not a comment or new line, then the line's indentation is not checked.
This is due to this if-check:
https://github.com/PowerShell/PSScriptAnalyzer/blob/a754b950467aa9e78a1eba1a3423bbd055ed8772/Rules/UseConsistentIndentation.cs#L165-L177
AddViolation()
, which subsequently checks the lines indentation against the expected indentation, is not called if the conditional evaluates to true
.
This PR changes the logic to always call AddViolation()
, so the indentation is checked, but to only increase the indentation level when the conditional evaluates to false
.
Fixes #1994
I've run this rule recursively over some large code-bases, including the PSScriptAnalyzer
repo.
- In the current
1.22.0
we find2,927
violations. - With this PR applied, we find
2,931
violations. - I believe the settings I used (below) means the rule defaults to
spaces
and many of the files were usingtabs
- hence the large number of violations.$Settings = @{ IncludeRules = @('PSUseConsistentIndentation') Rules = @{ PSUseConsistentIndentation = @{ Enable = $true } } }
- All previously found violations were still found.
- There were 4 newly found violations:
-
\Tests\Engine\ModuleHelp.Tests.ps1\ModuleHelp.Tests.ps1
line 48 https://github.com/PowerShell/PSScriptAnalyzer/blob/a754b950467aa9e78a1eba1a3423bbd055ed8772/Tests/Engine/ModuleHelp.Tests.ps1#L47-L54This becomes:
Which is a little dubious 🤔
Edit: This is because the previous line has
6
opening LParens, and3
closing RParens. So theindentation
level is3
greater than the previous line -
\Tests\Engine\ModuleHelp.Tests.ps1\ModuleHelp.Tests.ps1
line 116 https://github.com/PowerShell/PSScriptAnalyzer/blob/a754b950467aa9e78a1eba1a3423bbd055ed8772/Tests/Engine/ModuleHelp.Tests.ps1#L115-L117This line wasn't previously being checked. It's using tabs, and as mentioned above the settings is defaulting to spaces.
-
\Tests\Engine\CommunityAnalyzerRules\CommunityAnalyzerRules.psm1\CommunityAnalyzerRules.psm1
line 518 https://github.com/PowerShell/PSScriptAnalyzer/blob/a754b950467aa9e78a1eba1a3423bbd055ed8772/Tests/Engine/CommunityAnalyzerRules/CommunityAnalyzerRules.psm1#L515-L521This is changed to:
-
\Tests\Rules\UseToExportFieldsInManifest.tests.ps1\UseToExportFieldsInManifest.tests.ps1
line 73This is a line that randomly uses tabs
-
PR Checklist
- [x] PR has a meaningful title
- Use the present tense and imperative mood when describing your changes
- [x] Summarized changes
- [x] Change is not breaking
- [x] Make sure all
.cs
,.ps1
and.psm1
files have the correct copyright header - [x] Make sure you've added a new test if existing tests do not effectively test the code changed and/or updated documentation
- [x] This PR is ready to merge and is not Work in Progress.
- If the PR is work in progress, please add the prefix
WIP:
to the beginning of the title and remove the prefix when the PR is ready.
- If the PR is work in progress, please add the prefix