Skip to content

Commit d30f10f

Browse files
authored
PSUseConsistentWhitespace: When checking separators, ignore whitespace violations between a separator and a comment (#2065)
1 parent d6eb35e commit d30f10f

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

Rules/UseConsistentWhitespace.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@ private IEnumerable<DiagnosticRecord> FindSeparatorViolations(TokenOperations to
451451
{
452452
return node.Next != null
453453
&& node.Next.Value.Kind != TokenKind.NewLine
454+
&& node.Next.Value.Kind != TokenKind.Comment
454455
&& node.Next.Value.Kind != TokenKind.EndOfInput // semicolon can be followed by end of input
455456
&& !IsPreviousTokenApartByWhitespace(node.Next);
456457
};

Tests/Rules/UseConsistentWhitespace.tests.ps1

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,48 @@ if ($true) { Get-Item `
514514
}
515515
}
516516

517+
Context "CheckSeparator" {
518+
BeforeAll {
519+
$ruleConfiguration.CheckInnerBrace = $false
520+
$ruleConfiguration.CheckOpenBrace = $false
521+
$ruleConfiguration.CheckOpenParen = $false
522+
$ruleConfiguration.CheckOperator = $false
523+
$ruleConfiguration.CheckPipe = $false
524+
$ruleConfiguration.CheckSeparator = $true
525+
}
526+
527+
It "Should find a violation if there is no space after a comma" {
528+
$def = '$Array = @(1,2)'
529+
Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should -HaveCount 1
530+
}
531+
532+
It "Should not find a violation if there is a space after a comma" {
533+
$def = '$Array = @(1, 2)'
534+
Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should -Be $null
535+
}
536+
537+
It "Should not find a violation if there is a new-line after a comma" {
538+
$def = @'
539+
$Array = @(
540+
1,
541+
2
542+
)
543+
'@
544+
Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should -Be $null
545+
}
546+
547+
It "Should not find a violation if there is a comment after the separator" {
548+
$def = @'
549+
$Array = @(
550+
'foo', # Comment Line 1
551+
'FizzBuzz' # Comment Line 2
552+
)
553+
'@
554+
Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should -BeNullOrEmpty
555+
}
556+
557+
}
558+
517559

518560
Context "CheckParameter" {
519561
BeforeAll {

0 commit comments

Comments
 (0)