From 0ea441cd333b50b08b0011b6593e018451993561 Mon Sep 17 00:00:00 2001 From: Liam Peters Date: Wed, 10 Apr 2024 08:41:34 +0100 Subject: [PATCH 1/3] Formatter: Added PSAvoidTrailingWhitespace to the list of rules considered by the formatter --- Engine/Formatter.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Engine/Formatter.cs b/Engine/Formatter.cs index 5a93854c5..a6a25f0fb 100644 --- a/Engine/Formatter.cs +++ b/Engine/Formatter.cs @@ -47,6 +47,7 @@ public static string Format( "PSAvoidUsingDoubleQuotesForConstantString", "PSAvoidSemicolonsAsLineTerminators", "PSAvoidExclaimOperator", + "PSAvoidTrailingWhitespace", }; var text = new EditableText(scriptDefinition); From f1f53f67222d4e6f6c2cb292a89b0f8b83ef5192 Mon Sep 17 00:00:00 2001 From: Liam Peters Date: Wed, 10 Apr 2024 08:42:41 +0100 Subject: [PATCH 2/3] Rules/AvoidTrailingWhitespace: Fixed issue where lines with a single character, followed by multiple white-spaces were truncated when fixed/formatted --- Rules/AvoidTrailingWhitespace.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Rules/AvoidTrailingWhitespace.cs b/Rules/AvoidTrailingWhitespace.cs index 47f576d5b..a7567d6e6 100644 --- a/Rules/AvoidTrailingWhitespace.cs +++ b/Rules/AvoidTrailingWhitespace.cs @@ -54,7 +54,7 @@ public IEnumerable AnalyzeScript(Ast ast, string fileName) } int startColumnOfTrailingWhitespace = 1; - for (int i = line.Length - 2; i > 0; i--) + for (int i = line.Length - 2; i >= 0; i--) { if (line[i] != ' ' && line[i] != '\t') { From e996733de8d7e9d62a167e9ab5e4c8fd66927ee0 Mon Sep 17 00:00:00 2001 From: Liam Peters Date: Wed, 10 Apr 2024 09:40:39 +0100 Subject: [PATCH 3/3] Tests/Rules/AvoidTrailingWhitespace: Added test for usage of Invoke-Formatter with PSAvoidTrailingWhitespace and also checking that single-character lines that have trailing whitespace are not removed --- Tests/Rules/AvoidTrailingWhitespace.tests.ps1 | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/Tests/Rules/AvoidTrailingWhitespace.tests.ps1 b/Tests/Rules/AvoidTrailingWhitespace.tests.ps1 index f130a77c5..e5a45f0d3 100644 --- a/Tests/Rules/AvoidTrailingWhitespace.tests.ps1 +++ b/Tests/Rules/AvoidTrailingWhitespace.tests.ps1 @@ -9,6 +9,9 @@ BeforeAll { $settings = @{ IncludeRules = @($ruleName) + Rules = @{ + $ruleName = @{} + } } } @@ -34,4 +37,26 @@ Describe "AvoidTrailingWhitespace" { $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings Test-CorrectionExtentFromContent $def $violations 1 $Whitespace '' } + + It 'Should be used by Invoke-Formatter, when in settings, replacing trailing ' -TestCases $testCases { + param ( + [string] $Whitespace + ) + # Test also guards against regression where single-character lines, with trailing whitespace + # would be removed entirely. See issues #1757, #1992 + $def = @" +Function Get-Example { + 'Example'$Whitespace +}$Whitespace +"@ + + $expected = @" +Function Get-Example { + 'Example' } +"@ + $formatted = Invoke-Formatter -ScriptDefinition $def -Settings $settings + $formatted | Should -Be $expected + } + +} \ No newline at end of file