Skip to content

Commit 9fa10d4

Browse files
authored
PSUseConsistentWhitespace: Correctly fix whitespace between command parameters 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
1 parent 56c6ea1 commit 9fa10d4

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

Rules/UseConsistentWhitespace.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,8 +421,8 @@ private IEnumerable<DiagnosticRecord> FindParameterViolations(Ast ast)
421421
{
422422
int numberOfRedundantWhiteSpaces = rightExtent.StartColumnNumber - expectedStartColumnNumberOfRightExtent;
423423
var correction = new CorrectionExtent(
424-
startLineNumber: leftExtent.StartLineNumber,
425-
endLineNumber: leftExtent.EndLineNumber,
424+
startLineNumber: leftExtent.EndLineNumber,
425+
endLineNumber: rightExtent.StartLineNumber,
426426
startColumnNumber: leftExtent.EndColumnNumber + 1,
427427
endColumnNumber: leftExtent.EndColumnNumber + 1 + numberOfRedundantWhiteSpaces,
428428
text: string.Empty,

Tests/Rules/UseConsistentWhitespace.tests.ps1

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ bar -h i `
535535
Invoke-ScriptAnalyzer -ScriptDefinition "$def" -Settings $settings | Should -Be $null
536536
}
537537

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

588+
It "Should fix script when a parameter value is a script block spanning multiple lines" {
589+
$def = {foo {
590+
bar
591+
} -baz}
592+
593+
$expected = {foo {
594+
bar
595+
} -baz}
596+
Invoke-Formatter -ScriptDefinition "$def" -Settings $settings |
597+
Should -Be "$expected"
598+
}
599+
600+
It "Should fix script when a parameter value is a hashtable spanning multiple lines" {
601+
$def = {foo @{
602+
a = 1
603+
} -baz}
604+
605+
$expected = {foo @{
606+
a = 1
607+
} -baz}
608+
Invoke-Formatter -ScriptDefinition "$def" -Settings $settings |
609+
Should -Be "$expected"
610+
}
611+
612+
It "Should fix script when a parameter value is an array spanning multiple lines" {
613+
$def = {foo @(
614+
1
615+
) -baz}
616+
617+
$expected = {foo @(
618+
1
619+
) -baz}
620+
Invoke-Formatter -ScriptDefinition "$def" -Settings $settings |
621+
Should -Be "$expected"
622+
}
623+
588624
It "Should fix script when redirects are involved and whitespace is not consistent" {
589625
# Related to Issue #2000
590626
$def = 'foo 3>&1 1>$null 2>&1'

0 commit comments

Comments
 (0)