Skip to content

PSUseConsistentWhitespace: Correctly fix whitespace between command parameters when parameter value spans multiple lines #2064

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

Conversation

liamjpeters
Copy link
Contributor

PR Summary

Changes the suggested correction extent of PSUseConsistentWhitespace's parameter spacing violations.

When enabled, the CheckParameter setting of this rule checks the spacing between parameters of a command.

An erroneous correction extent was being generated when parameter values spanned multiple lines (such as for arrays, scriptblocks, and hashtables.

For instance, using the below settings to isolate only the CheckParameter part of the rule:

$Settings = @{
    IncludeRules = @('PSUseConsistentWhitespace')
    Rules        = @{
        PSUseConsistentWhitespace = @{
            Enable                                  = $true
            CheckInnerBrace                         = $false
            CheckOpenBrace                          = $false
            CheckOpenParen                          = $false
            CheckOperator                           = $false
            CheckPipe                               = $false
            CheckPipeForRedundantWhitespace         = $false
            CheckSeparator                          = $false
            CheckParameter                          = $true
            IgnoreAssignmentOperatorInsideHashTable = $false
        }
    }
}

Running the formatter against:

foo { 
    bar
}  -baz

foo @{ 
    1 = 1
}  -baz

foo @(
    1
)  -baz

Note: There are multiple spaces before each of the -baz parameters

Resulted in:

fo-baz

fo-baz

fo-baz

Fixes #2060

PR Checklist

@andyleejordan andyleejordan force-pushed the #2060WhiteSpaceBetweenParametersIssue branch from ad83312 to 78de299 Compare February 25, 2025 00:39
Copy link
Member

@andyleejordan andyleejordan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice fix, thank you.

@andyleejordan andyleejordan enabled auto-merge (squash) February 25, 2025 00:41
@andyleejordan andyleejordan merged commit 9fa10d4 into PowerShell:main Feb 25, 2025
4 checks passed
@liamjpeters liamjpeters deleted the #2060WhiteSpaceBetweenParametersIssue branch February 25, 2025 14:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unexpected string with powershell.codeFormatting.whitespaceBetweenParameters enabled
2 participants