PSScriptAnalyzer icon indicating copy to clipboard operation
PSScriptAnalyzer copied to clipboard

PSAvoidTrailingWhitespace: Rule not applied when using formatter + single character lines with trailing whitespace are truncated

Open liamjpeters opened this issue 10 months ago • 2 comments

PR Summary

  • Added PSAvoidTrailingWhitespace to the list of rules considered by the formatter (an entry for the rule is still required in the Rules part of settings).

    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

liamjpeters avatar Apr 10 '24 08:04 liamjpeters

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.

Ju-l1a avatar Jul 17 '24 09:07 Ju-l1a

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

image

But is fixed when running:

Invoke-Formatter -ScriptDefinition $ScriptDef -Settings $Settings

image

Hope that helps!

liamjpeters avatar Jul 17 '24 11:07 liamjpeters