Skip to content

Tweak UseConsistentWhiteSpace formatting rule to exclude first unary operator when being used in argument #949

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

Merged
Merged
Show file tree
Hide file tree
Changes from 5 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 RuleDocumentation/UseConsistentWhitespace.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Checks if there is space between a keyword and its corresponding open parenthesi

#### CheckOperator: bool (Default value is `$true`)

Checks if a binary operator is surrounded on both sides by a space. E.g. `$x = 1`.
Checks if a binary or unary operator is surrounded on both sides by a space. E.g. `$x = 1`.

#### CheckSeparator: bool (Default value is `$true`)

Expand Down
8 changes: 8 additions & 0 deletions Rules/UseConsistentWhitespace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,14 @@ private IEnumerable<DiagnosticRecord> FindOperatorViolations(TokenOperations tok
continue;
}

// exclude unary operator for cases like $foo.bar(-$Var)
if (TokenTraits.HasTrait(tokenNode.Value.Kind, TokenFlags.UnaryOperator) &&
tokenNode.Previous.Value.Kind == TokenKind.LParen &&
tokenNode.Next.Value.Kind == TokenKind.Variable)
{
continue;
}

var hasWhitespaceBefore = IsPreviousTokenOnSameLineAndApartByWhitespace(tokenNode);
var hasWhitespaceAfter = tokenNode.Next.Value.Kind == TokenKind.NewLine
|| IsPreviousTokenOnSameLineAndApartByWhitespace(tokenNode.Next);
Expand Down
9 changes: 9 additions & 0 deletions Tests/Engine/InvokeFormatter.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ function foo {

Invoke-Formatter $def $settings | Should -Be $expected
}

It "Should not expand unary operators when being used as a single negative argument" {
$script = '$foo.bar(-$a)'
Invoke-Formatter '$foo.bar(-$a)' -Settings CodeFormatting | Should -Be $script
}

It "Should expand unary operators when not being used as a single negative argument" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm not sure it's not just parens where this be applied. Take the following:

PS> Invoke-Formatter -ScriptDefinition '$r = $a -lt -$var'                                                                                                       
$r = $a -lt - $var

shouldn't that be $r = $a -lt -$var?

Copy link
Collaborator Author

@bergmeister bergmeister Mar 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with you. I extracted this into the new issue #950 to separate concerns and finish this PR. I am sure there are other special cases as well but I guess the best approach is to find them one by one and find heuristics to detect each case I suppose.

Invoke-Formatter '$foo.bar(-$a+$b+$c)' -Settings CodeFormatting | Should -Be '$foo.bar(-$a + $b + $c)'
}
}

Context "When a range is given" {
Expand Down