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

Conversation

bergmeister
Copy link
Collaborator

@bergmeister bergmeister commented Mar 26, 2018

PR Summary

Fixes #847

When an expression like $foo.bar(-$Var) is present, then the UseConsistentWhiteSpace should not expand spaces around this expression.
This works by checking if the - operator is an unary operator, is preceded by an open parenthesis and followed by a variable.

PR Checklist

Note: Tick the boxes below that apply to this pull request by putting an x between the square brackets. Please mark anything not applicable to this PR NA.

  • PR has a meaningful title
    • Use the present tense and imperative mood when describing your changes
  • Summarized changes
  • User facing documentation needed
  • Change is not breaking
  • Make sure you've added a new test if existing tests do not effectively test the code changed
  • This PR is ready to merge and is not work in progress
    • If the PR is work in progress, please add the prefix WIP: to the beginning of the title and remove the prefix when the PR is ready

@bergmeister bergmeister self-assigned this Mar 26, 2018
@bergmeister bergmeister changed the title Formatter unary operator Tweak UseConsistenWhiteSpace formatting rule to exclude unary operator when being used as negative argument Mar 26, 2018
@bergmeister bergmeister changed the title Tweak UseConsistenWhiteSpace formatting rule to exclude unary operator when being used as negative argument Tweak UseConsistenWhiteSpace formatting rule to exclude first unary operator when being used in argument Mar 26, 2018
Copy link
Contributor

@JamesWTruher JamesWTruher left a comment

Choose a reason for hiding this comment

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

since this is definitely improving the current experience, this isn't a blocking comment or one that requires a change, but I think this can go a bit further.

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.

@bergmeister bergmeister changed the title Tweak UseConsistenWhiteSpace formatting rule to exclude first unary operator when being used in argument Tweak UseConsistentWhiteSpace formatting rule to exclude first unary operator when being used in argument Mar 28, 2018
@bergmeister bergmeister merged commit 00c4787 into PowerShell:development Mar 28, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Formatter adds spaces around a hyphen, but only if it is followed by a variable
2 participants