From 1541e8d710177afedcc93a481b190dc49a6a7484 Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Thu, 14 May 2020 16:59:49 +0000 Subject: [PATCH] UseConsistentWhitespace: Fix CheckParameter bug when using interpolated string --- Rules/UseConsistentWhitespace.cs | 3 ++- Tests/Rules/UseConsistentWhitespace.tests.ps1 | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Rules/UseConsistentWhitespace.cs b/Rules/UseConsistentWhitespace.cs index f589d9e45..86795d705 100644 --- a/Rules/UseConsistentWhitespace.cs +++ b/Rules/UseConsistentWhitespace.cs @@ -390,7 +390,8 @@ private IEnumerable FindParameterViolations(Ast ast) testAst => testAst is CommandAst, true); foreach (CommandAst commandAst in commandAsts) { - List commandParameterAstElements = commandAst.FindAll(testAst => true, searchNestedScriptBlocks: false).ToList(); + List commandParameterAstElements = commandAst.FindAll( + testAst => testAst.Parent == commandAst, searchNestedScriptBlocks: false).ToList(); for (int i = 0; i < commandParameterAstElements.Count - 1; i++) { IScriptExtent leftExtent = commandParameterAstElements[i].Extent; diff --git a/Tests/Rules/UseConsistentWhitespace.tests.ps1 b/Tests/Rules/UseConsistentWhitespace.tests.ps1 index 23bf18350..38437076e 100644 --- a/Tests/Rules/UseConsistentWhitespace.tests.ps1 +++ b/Tests/Rules/UseConsistentWhitespace.tests.ps1 @@ -474,11 +474,11 @@ bar -h i ` } It "Should fix script to always have 1 space between parameters except when using colon syntax but not by default" { - $def = 'foo -bar $baz -ParameterName: $ParameterValue' + $def = 'foo -bar $baz -ParameterName: $ParameterValue "$PSScriptRoot\module.psd1"' Invoke-Formatter -ScriptDefinition $def | Should -BeExactly $def -Because 'CheckParameter configuration is not turned on by default (yet) as the setting is new' Invoke-Formatter -ScriptDefinition $def -Settings $settings | - Should -BeExactly 'foo -bar $baz -ParameterName: $ParameterValue' + Should -BeExactly 'foo -bar $baz -ParameterName: $ParameterValue "$PSScriptRoot\module.psd1"' } It "Should fix script when newlines are involved" {