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

[Fix #3511] Style/TernaryParentheses false positive #3513

Merged
merged 1 commit into from
Sep 20, 2016
Merged

[Fix #3511] Style/TernaryParentheses false positive #3513

merged 1 commit into from
Sep 20, 2016

Conversation

dreyks
Copy link
Contributor

@dreyks dreyks commented Sep 19, 2016

Fix false positive for a ternary with ranges

Fixes #3511

  • Wrote [good commit messages][1].
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Used the same coding conventions as the rest of the project.
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • All tests are passing.
  • The new code doesn't generate RuboCop offenses.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.

@bbatsov
Copy link
Collaborator

bbatsov commented Sep 19, 2016

The fix looks good, but it also needs a changelog entry.

@dreyks
Copy link
Contributor Author

dreyks commented Sep 19, 2016

done

@@ -1,6 +1,7 @@
# Change log

## master (unreleased)
[#3513](https://github.com/bbatsov/rubocop/pull/3513): Fix false positive in `Style/TernaryParentheses` for a ternary with ranges. ([@dreyks][])
Copy link
Collaborator

Choose a reason for hiding this comment

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

See the changelog for older releases. There should be a Bugs fixed section here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup, fixed this.

@@ -91,7 +91,7 @@ def redundant_parentheses_enabled?
end

def parenthesized?(node)
node.source =~ /^\(.*\)$/
node.type == :begin
Copy link
Collaborator

Choose a reason for hiding this comment

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

There's a predicate method on Node for this:

node.begin_type?

Also, I'm not entirely sure why this works. 😅 With the changed implementation, does the method name still make sense?

Copy link
Contributor Author

@dreyks dreyks Sep 19, 2016

Choose a reason for hiding this comment

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

I'm new to this whole AST thing but empirically I realized that a braced expression has a :begin root node, while non-braced has anything but it... I will change to begin_type?

And all the previous tests pass, so it seems that I didn't break anything >.<

Copy link
Collaborator

Choose a reason for hiding this comment

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

Okay. Makes sense to me. Nice job fixing this. 😀

@dreyks
Copy link
Contributor Author

dreyks commented Sep 19, 2016

💚

@bbatsov
Copy link
Collaborator

bbatsov commented Sep 19, 2016

I'm new to this whole AST thing but empirically I realized that a braced expression has a :begin root node, while non-braced has anything but it... I will change to begin_type?

And all the previous tests pass, so it seems that I didn't break anything >.<

There's one small caveat. Something like x; y will also introduce a begin node, so ideally you should still check if this begin node starts with a ( and perhaps add a test for the x; y scenario. (begin nodes simply indicate there are more than 1 expression in a scope).

@dreyks
Copy link
Contributor Author

dreyks commented Sep 19, 2016

I've just checked x; y generates a :send root node

@dreyks
Copy link
Contributor Author

dreyks commented Sep 19, 2016

I've added a couple of tests. What else should I check for?

@bbatsov
Copy link
Collaborator

bbatsov commented Sep 19, 2016

I've just checked x; y generates a :send root node

[2] pry(main)> Parser::CurrentRuby.parse("x; y")
=> s(:begin,
  s(:send, nil, :x),
  s(:send, nil, :y))

Doesn't look like a send root node to me.


context 'with several expressions on one line' do
it_behaves_like 'code without offense',
'foo; bar ? a: b'
Copy link
Collaborator

Choose a reason for hiding this comment

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

a :

@bbatsov
Copy link
Collaborator

bbatsov commented Sep 19, 2016

Anyways, I recalled that in the context of a ? this won't matter as ? binds stronger than ;.

'(foo..bar).include?(baz) ? a : b'
end

context 'with several expressions on one line' do
Copy link
Collaborator

Choose a reason for hiding this comment

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

Guess I misled you and we can drop this example.

@dreyks
Copy link
Contributor Author

dreyks commented Sep 19, 2016

💚

@bbatsov bbatsov merged commit 51ed214 into rubocop:master Sep 20, 2016
@bbatsov
Copy link
Collaborator

bbatsov commented Sep 20, 2016

👍

@reitermarkus
Copy link
Contributor

Great to see this fixed, just ran into this with this nice line:

        "cellar" => (cellar = bottle_spec.cellar).is_a?(Symbol) ? cellar.inspect : cellar,

mikezter added a commit to mikezter/rubocop that referenced this pull request Sep 28, 2016
* bbatsov/master: (80 commits)
  [Fix rubocop#3540] Make `Style/GuardClause` register offense for instance & singleton methods
  [Fix rubocop#3436] issue related to Rails/SaveBang when returning non-bang call from the parent method
  Allow `#to_yml` to produce single-quoted strings
  Add support for StyleGuideBaseURL and update rules
  Add spec for the existing style guide URL implementation
  Fix the changelog
  Edited regular expression for normal case to fix issues rubocop#3514 and rubocop#3516 (rubocop#3524)
  Add a rake task for generation a new cop (rubocop#3533)
  [Fix rubocop#3510] Various bug fixes for SafeNavigation (rubocop#3517)
  [Fix rubocop#3512] Change error message of `Lint/UnneededSplatExpansion` for array (rubocop#3526)
  Fix false positive in `Lint/AssignmentInCondition` (rubocop#3520) (rubocop#3529)
  Rename a mismatched filename (rubocop#3523)
  Fix a broken changelog entry
  [Fix rubocop#3511] Style/TernaryParentheses false positive (rubocop#3513)
  Fix the release notes for 0.43
  Cut 0.43.0
  [Fix rubocop#3462] Don't flag single-line methods
  Fix false negatives in `Rails/NotNullColumn` cop (rubocop#3508)
  Remove unused doubled methods (rubocop#3509)
  [Fix rubocop#3485] Make OneLineConditional cop ignore empty else (rubocop#3487)
  ...
Neodelf pushed a commit to Neodelf/rubocop that referenced this pull request Oct 15, 2016
@dreyks dreyks deleted the fix_ternary branch October 25, 2018 11:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants