Skip to content
Draft
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Rules/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -988,7 +988,7 @@
<value>Use space before open parenthesis.</value>
</data>
<data name="UseConsistentWhitespaceErrorOperator" xml:space="preserve">
<value>Use space before and after binary and assignment operators.</value>
<value>Use space around binary, assignment and unary operators that start with a dash.</value>
</data>
<data name="UseConsistentWhitespaceErrorSeparatorComma" xml:space="preserve">
<value>Use space after a comma.</value>
Expand Down
28 changes: 28 additions & 0 deletions Rules/UseConsistentWhitespace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ private bool IsOperator(Token token)
return TokenTraits.HasTrait(token.Kind, TokenFlags.AssignmentOperator)
|| TokenTraits.HasTrait(token.Kind, TokenFlags.BinaryPrecedenceAdd)
|| TokenTraits.HasTrait(token.Kind, TokenFlags.BinaryPrecedenceMultiply)
|| TokenTraits.HasTrait(token.Kind, TokenFlags.UnaryOperator) && token.Text.StartsWith("-")
|| token.Kind == TokenKind.AndAnd
|| token.Kind == TokenKind.OrOr;
}
Expand Down Expand Up @@ -519,6 +520,33 @@ private IEnumerable<DiagnosticRecord> FindOperatorViolations(TokenOperations tok
|| tokenNode.Next == null
|| tokenNode.Value.Kind == TokenKind.DotDot)
{
// for cases like '-split$a' as the other checks below in this function assume a preceding token
if (TokenTraits.HasTrait(tokenNode.Value.Kind, TokenFlags.UnaryOperator) &&
tokenNode.Value.Text.StartsWith("-"))
{
var hasWhitespaceAfterOperator = tokenNode.Next.Value.Kind == TokenKind.NewLine
|| IsPreviousTokenOnSameLineAndApartByWhitespace(tokenNode.Next);
if (!hasWhitespaceAfterOperator)
{
yield return new DiagnosticRecord(
GetError(ErrorKind.Operator),
tokenNode.Value.Extent,
GetName(),
GetDiagnosticSeverity(),
tokenOperations.Ast.Extent.File,
null,
new List<CorrectionExtent>()
{
new CorrectionExtent(
tokenNode.Value.Extent.StartLineNumber,
tokenNode.Value.Extent.EndLineNumber,
tokenNode.Value.Extent.StartColumnNumber,
tokenNode.Value.Extent.EndColumnNumber,
$"{tokenNode.Value.Text} ",
tokenNode.Value.Extent.File)
});
}
}
continue;
}

Expand Down
34 changes: 34 additions & 0 deletions Tests/Rules/UseConsistentWhitespace.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,40 @@ $x = $true -and
Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings | Should -Be $null
}


It 'Should not find violation if there are whitespaces of size 1 around a unary operator starting with a dash' {
Invoke-ScriptAnalyzer -ScriptDefinition '$x -join $y' -Settings $settings | Should -BeNullOrEmpty
}

It 'Should find a violation if no whitespace around a unary operator starting with a dash' {
$def = '$x=1'
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
Test-CorrectionExtentFromContent $def $violations 1 '=' ' = '
}

It 'Should find a violation if no whitespace before a unary operator starting with a dash' {
$def = '$x-join $Y'
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
Test-CorrectionExtentFromContent $def $violations 1 '' ' '
}

It 'Should find a violation if no whitespace after a unary operator starting with a dash' {
$def = '$x -join$y'
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
Test-CorrectionExtentFromContent $def $violations 1 '' ' '
}

It 'Should find a violation if there is a whitespaces not of size 1 around a unary operator starting with a dash' {
$def = '$x -join $y'
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
Test-CorrectionExtentFromContent $def $violations 1 ' -join ' ' -join '
}

It 'Should find a violation if there is no whitespace after a unary operator with a dash but nothing that preceds it' {
$def = '-join$x'
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
Test-CorrectionExtentFromContent $def $violations 1 '-join' '-join '
}
}

Context "When a comma is not followed by a space" {
Expand Down