Skip to content

Commit

Permalink
PSUseConsistentWhitespace: Correctly fix whitespace between command p…
Browse files Browse the repository at this point in the history
…arameters when parameter value spans multiple lines (#2064)

* Added test coverage for the scenarios of parameter values spanning multiple lines

* Fix erroneous double-negative in test name

* As the correction takes place on the whitespace between two extents, the correction should begin on the last line of the left extent and end on the first line of the right extent
  • Loading branch information
liamjpeters authored Feb 25, 2025
1 parent 56c6ea1 commit 9fa10d4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
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

0 comments on commit 9fa10d4

Please sign in to comment.