PSScriptAnalyzer
PSScriptAnalyzer copied to clipboard
PSUseConsistentWhitespace: Handle redirect operators which are not in stream order
PR Summary
When CheckParameter
is $true
for the rule PSUseConsistentWhitespace
, it checks whitespace between parameters by inspecting the AST. It gets all direct children of each CommandAst
and checks, in order, that each's extent is separated by exactly 1 character.
This relies on the implicit assumption that the AST returns the children in something resembling token order.
In the case of redirect operators, this is not always the case (they anecdotally appear to be returned in the order of the stream they're redirecting). See the issue here for more detailed breakdown.
This PR adds sorting of the children before subsequently performing the same checks on the tokens extents.
Running the below across PS5.1 and PS7.4 with and without the PR change applied shows the performance impact of the change to be minimal.
Measure-Command {
foreach ($i in 1..10000) {
Invoke-Formatter -ScriptDefinition "Invoke-Foo $i 3>&1 2>&1 1>`$null" -Settings @{
Rules = @{
PSUseConsistentWhitespace = @{
Enable = $true
CheckParameter = $true
}
}
}
}
}
PS5.1 Pre-PR | PS5.1 Post-PR | PS7.4.2 Pre-PR | PS7.4.2 Post-PR | |
---|---|---|---|---|
Invoke-Formatter |
231.3535187 | 156.0559906 | 16.8896873 | 13.4352551 |
Invoke-ScriptAnalyzer |
253.1291937 | 250.9393157 | 21.8151956 | 19.8125177 |
The 'speedup' is presumably that we're no longer emitting a diagnostic record
Fixes #2000
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