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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Rules/UseConsistentWhitespace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -421,8 +421,8 @@ private IEnumerable<DiagnosticRecord> FindParameterViolations(Ast ast)
{
int numberOfRedundantWhiteSpaces = rightExtent.StartColumnNumber - expectedStartColumnNumberOfRightExtent;
var correction = new CorrectionExtent(
startLineNumber: leftExtent.StartLineNumber,
endLineNumber: leftExtent.EndLineNumber,
startLineNumber: leftExtent.EndLineNumber,
endLineNumber: rightExtent.StartLineNumber,
startColumnNumber: leftExtent.EndColumnNumber + 1,
endColumnNumber: leftExtent.EndColumnNumber + 1 + numberOfRedundantWhiteSpaces,
text: string.Empty,
Expand Down
38 changes: 37 additions & 1 deletion Tests/Rules/UseConsistentWhitespace.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ bar -h i `
Invoke-ScriptAnalyzer -ScriptDefinition "$def" -Settings $settings | Should -Be $null
}

It "Should not find no violation if there is always 1 space between parameters except when using colon syntax" {
It "Should not find a violation if there is always 1 space between parameters except when using colon syntax" {
$def = 'foo -bar $baz @splattedVariable -bat -parameterName:$parameterValue'
Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should -Be $null
}
Expand Down Expand Up @@ -585,6 +585,42 @@ bar -h i `
Should -Be "$expected"
}

It "Should fix script when a parameter value is a script block spanning multiple lines" {
$def = {foo {
bar
} -baz}

$expected = {foo {
bar
} -baz}
Invoke-Formatter -ScriptDefinition "$def" -Settings $settings |
Should -Be "$expected"
}

It "Should fix script when a parameter value is a hashtable spanning multiple lines" {
$def = {foo @{
a = 1
} -baz}

$expected = {foo @{
a = 1
} -baz}
Invoke-Formatter -ScriptDefinition "$def" -Settings $settings |
Should -Be "$expected"
}

It "Should fix script when a parameter value is an array spanning multiple lines" {
$def = {foo @(
1
) -baz}

$expected = {foo @(
1
) -baz}
Invoke-Formatter -ScriptDefinition "$def" -Settings $settings |
Should -Be "$expected"
}

It "Should fix script when redirects are involved and whitespace is not consistent" {
# Related to Issue #2000
$def = 'foo 3>&1 1>$null 2>&1'
Expand Down