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 #6751] Prevent Style/OneLineConditional from breaking on retry and break keywords #6756

Merged
merged 3 commits into from
Feb 17, 2019

Conversation

Drenmi
Copy link
Collaborator

@Drenmi Drenmi commented Feb 11, 2019

This cop would break on code like:

if true then retry else 7 end
if true then break else 7 end

This was happening because the retry node was expected to be decorated with a decorator that includes the MethodDispatchNode extension.

This change adds a RetryNode- and a BreakNode decorator.

It also fixes an issue where superfluous parentheses were added when the keyword has no arguments:

Before:

true ? (retry) : 7

After:

true ? retry : 7

Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.

# available to all `retry` nodes within RuboCop.
class RetryNode < Node
include MethodDispatchNode
end
Copy link
Member

Choose a reason for hiding this comment

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

This looks like a wonderful approach 🌟

However issue #6751 also reports on raise and break. raise seems to work without errors, but break seems to be an error.

raise (no offenses)

% cat raise.rb
if true then raise else 7 end
% rubocop -a --only Style/OneLineConditional raise.rb
Inspecting 1 file
C

Offenses:

raise.rb:1:1: C: [Corrected] Style/OneLineConditional: Favor the ternary operator (?:) over if/then/else/end constructs.
if true then raise else 7 end
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected, 1 offense corrected
% g diff raise.rb
diff --git a/raise.rb b/raise.rb
index 519c3b0..69ac23b 100644
--- a/raise.rb
+++ b/raise.rb
@@ -1 +1 @@
-if true then raise else 7 end
+true ? raise : 7

break (offense detected)

% cat break.rb
if true then break else 7 end
% rubocop -a --only Style/OneLineConditional break.rb
Inspecting 1 file


0 files inspected, no offenses detected
undefined method `prefix_not?' for s(:break):RuboCop::AST::Node
/Users/koic/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.64.0/lib/rubocop/cop/style/one_line_conditional.rb:92:in `keyword_with_cha
nged_precedence?'
/Users/koic/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.64.0/lib/rubocop/cop/style/one_line_conditional.rb:80:in `requires_parenth
eses?'

So I think that it would be better to also make BreakNode. WDYT?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

So I think that it would be better to also make BreakNode. WDYT?

👍

expect_offense(<<-RUBY.strip_indent)
if true then retry else 7 end
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Favor the ternary operator (`?:`) over `if/then/else/end` constructs.
RUBY
Copy link
Member

Choose a reason for hiding this comment

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

It seems that retry is surrounded by parentheses when using auto-correct.

-if true then retry else 7 end
+true ? (retry) : 7

I think that parentheses are unnecessary if possible :-)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think that parentheses are unnecessary if possible :-)

Thanks for a great review! I will have a look. 🙂

@Drenmi
Copy link
Collaborator Author

Drenmi commented Feb 12, 2019

@koic Thanks for a stellar review! ⭐️ I have addressed all your comments in the PR. I will be on holiday from today until Sunday, so feel free to merge if you're okay with the additions. 🙇

CHANGELOG.md Outdated
@@ -5,6 +5,7 @@
### Bug fixes

* [#6748](https://github.com/rubocop-hq/rubocop/issues/6748): Fix `Style/RaiseArgs` auto-correction breaking in contexts that require parentheses. ([@drenmi][])
* [#6751](https://github.com/rubocop-hq/rubocop/issues/6751): Prevent `Style/OneLineConditional` from breaking on `retry` keyword. ([@drenmi][])
Copy link
Member

Choose a reason for hiding this comment

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

I think it would be good to have a mention about break keyword too :-)

@Drenmi Drenmi changed the title [Fix #6751] Prevent Style/OneLineConditional from breaking on retry keyword [Fix #6751] Prevent Style/OneLineConditional from breaking on retry and break keywords Feb 17, 2019
…retry keyword

This cop would break on code like:

```
if true then retry else 7 end
```

This was happening because the `retry` node was expected to be decorated
with a decorator that includes the `MethodDispatchNode` extension.

This change adds a `RetryNode` decorator.
To make this polymorphic with other method dispatch nodes, we need
to do some custom destructuring.

Before:

```
defined_node.node_parts
```

After:

```
defined_node.node_parts
```
@koic koic merged commit 333d650 into rubocop:master Feb 17, 2019
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.

None yet

2 participants