-
Notifications
You must be signed in to change notification settings - Fork 382
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
The PSAvoidTrailingWhitespace rule is not applied when using Invoke-Formatter #1992
Comments
This is possibly a duplicate of #1757 |
PSScriptAnalyzer/Engine/Formatter.cs Lines 38 to 50 in a754b95
|
Adding the rule to this list (and running build.ps1& importing the module) alone does not seem to change the behavior for me. Am I missing something? |
It looks as though the formatter considers each of the rules in the It does, however, skip over any rule that doesn't have (even empty) arguments passed in through settings. I'm not clear on the context of why this is done. PSScriptAnalyzer/Engine/Formatter.cs Lines 56 to 62 in a754b95
So for instance, once the rule is added as @bergmeister suggests: $script = @"
Function Get-Example {
'Example'
}
"
$settings = @{
Rules = @{
PSAvoidTrailingWhitespace = @{}
}
}
$formatted = Invoke-Formatter -ScriptDefinition $script -Settings $settings Runs the rule and "fixes" it, but I get a "fixed" script that no longer includes the closing curly-brace. Function Get-Example {
'Example'
This looks to be the same issue described in #1757 |
Interestingly: $script = @"
Function Get-Example {
'Example'
}
"
$settings = @{
Rules = @{
PSAvoidTrailingWhitespace = @{}
}
}
$formatted = Invoke-Formatter -ScriptDefinition $script -Settings $settings
results in: Function Get-Example {
'Example'
} and $script = @"
Function Get-Example {
'Example'
}
Function Get-Example {
'Example'
}
Function Get-Example {
'Example'
}
"@
$settings = @{
Rules = @{
PSAvoidTrailingWhitespace = @{}
}
}
$formatted = Invoke-Formatter -ScriptDefinition $script -Settings $settings results in: Function Get-Example {
'Example'
}
Function Get-Example {
'Example'
Function Get-Example {
'Example'
}
So it looks like The start column of the It's worked out here. PSScriptAnalyzer/Rules/AvoidTrailingWhitespace.cs Lines 56 to 64 in a754b95
It's looking backward through the line for a non-space, non-tab character, and if it's not finding it, it's assuming the whitespace starts at column 1 (assume columns are 1-indexed and not 0-indexed?). It's only checking up to character 1 in the string (loop condition is I think this bug should be simply changing Indeed doing so, I get correct looking results and all tests still pass. Happy to PR this, unless you want to have a go @Ju-l1a |
@liamjpeters I'd be grateful if you can do the PR so I can see how it's done properly :) Thank you for your help! |
Steps to reproduce
Using
Invoke-Formatter
does not apply thePSAvoidTrailingWhitespace
rule as shown by the following example script:Expected behavior
Actual behavior
Environment data
The text was updated successfully, but these errors were encountered: