Skip to content

Commit

Permalink
PSUseConsistentWhitespace: When checking separators, ignore whitespac…
Browse files Browse the repository at this point in the history
…e violations between a separator and a comment (#2065)
  • Loading branch information
liamjpeters authored Feb 25, 2025
1 parent d6eb35e commit d30f10f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions Rules/UseConsistentWhitespace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ private IEnumerable<DiagnosticRecord> FindSeparatorViolations(TokenOperations to
{
return node.Next != null
&& node.Next.Value.Kind != TokenKind.NewLine
&& node.Next.Value.Kind != TokenKind.Comment
&& node.Next.Value.Kind != TokenKind.EndOfInput // semicolon can be followed by end of input
&& !IsPreviousTokenApartByWhitespace(node.Next);
};
Expand Down
42 changes: 42 additions & 0 deletions Tests/Rules/UseConsistentWhitespace.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,48 @@ if ($true) { Get-Item `
}
}

Context "CheckSeparator" {
BeforeAll {
$ruleConfiguration.CheckInnerBrace = $false
$ruleConfiguration.CheckOpenBrace = $false
$ruleConfiguration.CheckOpenParen = $false
$ruleConfiguration.CheckOperator = $false
$ruleConfiguration.CheckPipe = $false
$ruleConfiguration.CheckSeparator = $true
}

It "Should find a violation if there is no space after a comma" {
$def = '$Array = @(1,2)'
Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should -HaveCount 1
}

It "Should not find a violation if there is a space after a comma" {
$def = '$Array = @(1, 2)'
Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should -Be $null
}

It "Should not find a violation if there is a new-line after a comma" {
$def = @'
$Array = @(
1,
2
)
'@
Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should -Be $null
}

It "Should not find a violation if there is a comment after the separator" {
$def = @'
$Array = @(
'foo', # Comment Line 1
'FizzBuzz' # Comment Line 2
)
'@
Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should -BeNullOrEmpty
}

}


Context "CheckParameter" {
BeforeAll {
Expand Down

0 comments on commit d30f10f

Please sign in to comment.