PSScriptAnalyzer
PSScriptAnalyzer copied to clipboard
PSAvoidTrailingWhitespace: Rule not applied when using formatter + single character lines with trailing whitespace are truncated
PR Summary
-
Added
PSAvoidTrailingWhitespace
to the list of rules considered by the formatter (an entry for the rule is still required in theRules
part ofsettings
).Fixes #1992
-
Fixed an issue where lines starting with and containing a single character, that have trailing white-space, are truncated when formatted/fixed. See comment in #1992 for explanation.
Fixes #1757
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
I don't know if this is to do with this issue specifically but I've found that with this branch the fix for PSAvoidTrailingWhitespace
gets applied when using Invoke-ScriptAnalyzer -Fix ...
but not when using Invoke-Formatter ...
. I don't seem to have this problem for any other rules I've tried.
Edit: actually I'm getting the same for PSPossibleIncorrectComparisonWithNull
.
Hey @Ju-l1a 👋,
You need to tell the formatter to include that rule when carrying out the formatting.
$Settings = @{
IncludeRules = @("PSAvoidTrailingWhitespace")
Rules = @{
"PSAvoidTrailingWhitespace" = @{}
}
}
So the below code, with lots of trailing whitespace:
$ScriptDef = @"
Function Get-Example {
'Example'`t`t`t
}`t`t`t
"@
Is not altered when running:
Invoke-Formatter -ScriptDefinition $ScriptDef
But is fixed when running:
Invoke-Formatter -ScriptDefinition $ScriptDef -Settings $Settings
Hope that helps!