From d9dd8606ebb450bce6f09fb7a762e6264584e6cf Mon Sep 17 00:00:00 2001 From: Liam Peters Date: Fri, 21 Feb 2025 14:39:40 +0000 Subject: [PATCH 1/3] Added test coverage for the scenarios of parameter values spanning multiple lines --- Tests/Rules/UseConsistentWhitespace.tests.ps1 | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/Tests/Rules/UseConsistentWhitespace.tests.ps1 b/Tests/Rules/UseConsistentWhitespace.tests.ps1 index 30d8cce57..d02890f3b 100644 --- a/Tests/Rules/UseConsistentWhitespace.tests.ps1 +++ b/Tests/Rules/UseConsistentWhitespace.tests.ps1 @@ -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' From ac0bce4ce09407a3534ee8de473adce158b995e8 Mon Sep 17 00:00:00 2001 From: Liam Peters Date: Fri, 21 Feb 2025 14:38:54 +0000 Subject: [PATCH 2/3] Fix erroneous double-negative in test name --- Tests/Rules/UseConsistentWhitespace.tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/Rules/UseConsistentWhitespace.tests.ps1 b/Tests/Rules/UseConsistentWhitespace.tests.ps1 index d02890f3b..362025d0d 100644 --- a/Tests/Rules/UseConsistentWhitespace.tests.ps1 +++ b/Tests/Rules/UseConsistentWhitespace.tests.ps1 @@ -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 } From 78de299a7a104a1e81d3b25c2b0036a20e1be7c4 Mon Sep 17 00:00:00 2001 From: Liam Peters Date: Fri, 21 Feb 2025 14:37:35 +0000 Subject: [PATCH 3/3] 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 --- Rules/UseConsistentWhitespace.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Rules/UseConsistentWhitespace.cs b/Rules/UseConsistentWhitespace.cs index a062e5d3f..de4c0e515 100644 --- a/Rules/UseConsistentWhitespace.cs +++ b/Rules/UseConsistentWhitespace.cs @@ -421,8 +421,8 @@ private IEnumerable 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,