Skip to content

Formatter adds spaces around a hyphen, but only if it is followed by a variable #847

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

Closed
exchange12rocks opened this issue Jan 12, 2018 · 5 comments · Fixed by #949
Closed
Assignees

Comments

@exchange12rocks
Copy link

Unformatted code:

$Var = 6
(Get-Date).AddDays(-6)
(Get-Date).AddDays(-$Var)

Formatted code:

$Var = 6
(Get-Date).AddDays(-6)
(Get-Date).AddDays( - $Var)

As you can see, the formatter adds spaces around a hyphen which is followed by a variable but does not add them if a hyphen is followed by a number. I think, the behavior should be consistent.

@JamesWTruher
Copy link
Contributor

JamesWTruher commented Jan 22, 2018

this is really a reflection of the rule, right? The formatter seems a red-herring

PS> Invoke-ScriptAnalyzer -Settings CodeFormatting -ScriptDefinition '$var = 6; (get-date).adddays(-$var)' | ft RuleName,Message            

RuleName                  Message                                                    
--------                  -------                                                    
PSUseConsistentWhitespace Use space before and after binary and assignment operators.

@AWahlqvist
Copy link

@bergmeister hmm, I'm not really sure but shouldn't unary operators (which this is, right?) be excluded from that rule?

This doesn't trigger the rule for example:

Invoke-ScriptAnalyzer -Settings CodeFormatting -ScriptDefinition '$var = 6; (get-date).adddays(--$var)'

So it doesn't seem logical that it triggers for a single hyphen either.

@bergmeister
Copy link
Collaborator

bergmeister commented Mar 26, 2018

@AWahlqvist Hmm, that's a good idea. I would need to first look at the whitespaceAroundOperator and PSUseConsistentWhitespace rule implementation to see how it would fit in, so there is a chance that this could get fixed but I can't guarantee it at the moment.

@msftrncs
Copy link

I'm still experiencing this behavior, where - is spaced out, but -not and ! are not. However, I think -not should be spaced, and so should not be ignored.

@msftrncs
Copy link

I need to clarify, I was testing just -$a with no parenthesis.

Consider: -not$a and (-not$a), or even (-split$a). These should at least get a space following the operator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants