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

Style/TernaryParentheses auto-correct generates invalid Ruby #4084

Closed
Drenmi opened this issue Mar 2, 2017 · 0 comments · Fixed by #4123
Closed

Style/TernaryParentheses auto-correct generates invalid Ruby #4084

Drenmi opened this issue Mar 2, 2017 · 0 comments · Fixed by #4123
Labels

Comments

@Drenmi
Copy link
Collaborator

Drenmi commented Mar 2, 2017

If you are using parentheses, Ruby let's you place the question mark operator of a ternary like this:

(foo)? bar : baz

(Note the lack of space between the condition and the operator.)

Style/TernaryParentheses needs to keep this in mind when auto-correcting, as currently it will generate invalid Ruby code by just removing the closing paren.

(This offense is separately checked by Style/SpaceAroundOperators, but we can't count on it being always enabled and first.)


Expected behavior

RuboCop should auto-correct to:

foo ? bar : baz

Actual behavior

RuboCop auto-corrects to:

foo? ? bar : baz

Steps to reproduce the problem

Run bundle exec rubocop --only Style/TernaryParentheses -a on:

(foo)? bar : baz

RuboCop version

Include the output of rubocop -V. Here's an example:

$ rubocop -V
0.47.1 (using Parser 2.3.3.1, running on ruby 2.3.3 x86_64-darwin15)
@bbatsov bbatsov added the bug label Mar 7, 2017
bbatsov pushed a commit that referenced this issue Mar 15, 2017
The space between ternary condition and question mark is not required if
the condition is parenthesized. Auto-correct would remove the
parentheses and generate invalid Ruby code.

```ruby
(foo)? bar : baz # valid Ruby code
foo? bar : baz   # invalid auto-correct before
foo ? bar : baz  # after the fix
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants