Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UseConsistentWhitespace.CheckOperator: Add unary operators that start with a dash (-split, -join, -not, -bnot, isplit, csplit) #1602

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
30 changes: 30 additions & 0 deletions Rules/UseConsistentWhitespace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,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 @@ -529,6 +530,35 @@ 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("-")
&& tokenNode.Value.Text.Length > 1
&& Char.IsLetter(tokenNode.Value.Text[1]))
{
bool 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
35 changes: 35 additions & 0 deletions Tests/Rules/UseConsistentWhitespace.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,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 '
}

It "Should find violation if not asked to ignore assignment operator in hash table" {
$def = @'
$ht = @{
Expand All @@ -207,6 +241,7 @@ $ht = @{
$ruleConfiguration.CheckSeparator = $false
$ruleConfiguration.IgnoreAssignmentOperatorInsideHashTable = $true
}

It "Should not find violation if assignment operator is in multi-line hash table" {
$def = @'
$ht = @{
Expand Down