From aadd648d89fb9c45873012c8ee4c1109ca223c61 Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Mon, 26 Mar 2018 22:55:40 +0100 Subject: [PATCH 1/5] exclude unary operator form UseConsistentWhitespace rule --- Rules/UseConsistentWhitespace.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Rules/UseConsistentWhitespace.cs b/Rules/UseConsistentWhitespace.cs index 180bfb798..5780109d7 100644 --- a/Rules/UseConsistentWhitespace.cs +++ b/Rules/UseConsistentWhitespace.cs @@ -160,6 +160,12 @@ public override SourceType GetSourceType() private bool IsOperator(Token token) { + // exclude unary operator for cases like $foo.bar(-$Var) + if (TokenTraits.HasTrait(token.Kind, TokenFlags.UnaryOperator)) + { + return false; + } + return TokenTraits.HasTrait(token.Kind, TokenFlags.AssignmentOperator) || TokenTraits.HasTrait(token.Kind, TokenFlags.BinaryPrecedenceAdd) || TokenTraits.HasTrait(token.Kind, TokenFlags.BinaryPrecedenceMultiply) From ebb2387dece619d6c339bed7df92c03820bf6419 Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Mon, 26 Mar 2018 23:08:04 +0100 Subject: [PATCH 2/5] fix failing test by being more precise about the use case of the unary operator enhancement --- Rules/UseConsistentWhitespace.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Rules/UseConsistentWhitespace.cs b/Rules/UseConsistentWhitespace.cs index 5780109d7..9fd3fd9b9 100644 --- a/Rules/UseConsistentWhitespace.cs +++ b/Rules/UseConsistentWhitespace.cs @@ -160,12 +160,6 @@ public override SourceType GetSourceType() private bool IsOperator(Token token) { - // exclude unary operator for cases like $foo.bar(-$Var) - if (TokenTraits.HasTrait(token.Kind, TokenFlags.UnaryOperator)) - { - return false; - } - return TokenTraits.HasTrait(token.Kind, TokenFlags.AssignmentOperator) || TokenTraits.HasTrait(token.Kind, TokenFlags.BinaryPrecedenceAdd) || TokenTraits.HasTrait(token.Kind, TokenFlags.BinaryPrecedenceMultiply) @@ -313,6 +307,12 @@ private IEnumerable 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) + { + continue; + } + var hasWhitespaceBefore = IsPreviousTokenOnSameLineAndApartByWhitespace(tokenNode); var hasWhitespaceAfter = tokenNode.Next.Value.Kind == TokenKind.NewLine || IsPreviousTokenOnSameLineAndApartByWhitespace(tokenNode.Next); From 752356c1bee74115e55443b7cd15e9496b0c19fc Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Mon, 26 Mar 2018 23:20:23 +0100 Subject: [PATCH 3/5] add test and docs --- RuleDocumentation/UseConsistentWhitespace.md | 3 ++- Tests/Engine/InvokeFormatter.tests.ps1 | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/RuleDocumentation/UseConsistentWhitespace.md b/RuleDocumentation/UseConsistentWhitespace.md index 6fa553b00..07887a840 100644 --- a/RuleDocumentation/UseConsistentWhitespace.md +++ b/RuleDocumentation/UseConsistentWhitespace.md @@ -38,7 +38,8 @@ 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`. +For the special case of `$foo.bar(-$Var)`, it does not add spaces. #### CheckSeparator: bool (Default value is `$true`) diff --git a/Tests/Engine/InvokeFormatter.tests.ps1 b/Tests/Engine/InvokeFormatter.tests.ps1 index 86543e09c..1bd4fd28e 100644 --- a/Tests/Engine/InvokeFormatter.tests.ps1 +++ b/Tests/Engine/InvokeFormatter.tests.ps1 @@ -30,6 +30,11 @@ function foo { Invoke-Formatter $def $settings | Should -Be $expected } + + It "Should not expand unary operators when being used as a negative argument" { + $script = '$foo.bar(-$Var)' + Invoke-Formatter '$foo.bar(-$Var)' -Settings CodeFormatting | Should -Be $script + } } Context "When a range is given" { From d0e0bd4f259925b96d58b650f13a8aa2f4436cf0 Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Mon, 26 Mar 2018 23:43:44 +0100 Subject: [PATCH 4/5] tweak fix to only apply to first argument --- RuleDocumentation/UseConsistentWhitespace.md | 1 - Rules/UseConsistentWhitespace.cs | 4 +++- Tests/Engine/InvokeFormatter.tests.ps1 | 10 +++++++--- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/RuleDocumentation/UseConsistentWhitespace.md b/RuleDocumentation/UseConsistentWhitespace.md index 07887a840..192f7441a 100644 --- a/RuleDocumentation/UseConsistentWhitespace.md +++ b/RuleDocumentation/UseConsistentWhitespace.md @@ -39,7 +39,6 @@ Checks if there is space between a keyword and its corresponding open parenthesi #### CheckOperator: bool (Default value is `$true`) Checks if a binary or unary operator is surrounded on both sides by a space. E.g. `$x = 1`. -For the special case of `$foo.bar(-$Var)`, it does not add spaces. #### CheckSeparator: bool (Default value is `$true`) diff --git a/Rules/UseConsistentWhitespace.cs b/Rules/UseConsistentWhitespace.cs index 9fd3fd9b9..12a47dfbd 100644 --- a/Rules/UseConsistentWhitespace.cs +++ b/Rules/UseConsistentWhitespace.cs @@ -308,7 +308,9 @@ private IEnumerable FindOperatorViolations(TokenOperations tok } // exclude unary operator for cases like $foo.bar(-$Var) - if (TokenTraits.HasTrait(tokenNode.Value.Kind, TokenFlags.UnaryOperator) && tokenNode.Previous.Value.Kind == TokenKind.LParen) + if (TokenTraits.HasTrait(tokenNode.Value.Kind, TokenFlags.UnaryOperator) && + tokenNode.Previous.Value.Kind == TokenKind.LParen && + tokenNode.Next.Value.Kind == TokenKind.Variable) { continue; } diff --git a/Tests/Engine/InvokeFormatter.tests.ps1 b/Tests/Engine/InvokeFormatter.tests.ps1 index 1bd4fd28e..4dd8c9f6f 100644 --- a/Tests/Engine/InvokeFormatter.tests.ps1 +++ b/Tests/Engine/InvokeFormatter.tests.ps1 @@ -31,9 +31,13 @@ function foo { Invoke-Formatter $def $settings | Should -Be $expected } - It "Should not expand unary operators when being used as a negative argument" { - $script = '$foo.bar(-$Var)' - Invoke-Formatter '$foo.bar(-$Var)' -Settings CodeFormatting | Should -Be $script + 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" { + Invoke-Formatter '$foo.bar(-$a+$b+$c)' -Settings CodeFormatting | Should -Be '$foo.bar(- $a + $b + $c)' } } From 72df26aa3a00dcc33c653a596ccefafe08a2f57d Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Tue, 27 Mar 2018 00:00:19 +0100 Subject: [PATCH 5/5] Fix accidental space in test expectation --- Tests/Engine/InvokeFormatter.tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/Engine/InvokeFormatter.tests.ps1 b/Tests/Engine/InvokeFormatter.tests.ps1 index 4dd8c9f6f..5c0e54a88 100644 --- a/Tests/Engine/InvokeFormatter.tests.ps1 +++ b/Tests/Engine/InvokeFormatter.tests.ps1 @@ -37,7 +37,7 @@ function foo { } It "Should expand unary operators when not being used as a single negative argument" { - Invoke-Formatter '$foo.bar(-$a+$b+$c)' -Settings CodeFormatting | Should -Be '$foo.bar(- $a + $b + $c)' + Invoke-Formatter '$foo.bar(-$a+$b+$c)' -Settings CodeFormatting | Should -Be '$foo.bar(-$a + $b + $c)' } }