-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
New braces_for_chaining style for Style/BlockDelimiters. #2329
New braces_for_chaining style for Style/BlockDelimiters. #2329
Conversation
48bb062
to
de2bfc7
Compare
de2bfc7
to
04ac153
Compare
This is already taken care of if you configure Style/BlockDelimiters:
EnforcedStyle: semantic |
@jonas054 This is a little bit different because
This PR allows you to enforce
|
@@ -17,6 +17,7 @@ | |||
* [#2277](https://github.com/bbatsov/rubocop/pull/2277): New cop `Style/FirstHashElementLineBreak` checks for a line break before the first element in a multi-line hash. ([@panthomakos][]) | |||
* [#2277](https://github.com/bbatsov/rubocop/pull/2277): New cop `Style/FirstMethodArgumentLineBreak` checks for a line break before the first argument in a multi-line method call. ([@panthomakos][]) | |||
* [#2277](https://github.com/bbatsov/rubocop/pull/2277): New cop `Style/FirstMethodParameterLineBreak` checks for a line break before the first parameter in a multi-line method parameter definition. ([@panthomakos][]) | |||
* [#2329](https://github.com/bbatsov/rubocop/pull/2329): New option `EnforceMultiLineBraceChaining` for `Style/BlockDelimiters` cop enforces braces on a multi-line block if it's return value is being chained with another method. ([@panthomakos][]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's -> its
I see. Then I think it's OK. But since the new parameter only makes sense for the |
04ac153
to
7b6616e
Compare
ecb7739
to
97cebf1
Compare
@jonas054 PTAL I've updated with your comments and implemented using the |
@@ -28,7 +28,7 @@ def on_block(node) | |||
if proper_block_style?(node) | |||
correct_style_detected | |||
else | |||
add_offense(node, :begin) { opposite_style_detected } | |||
add_offense(node, :begin) { unrecognized_style_detected } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, those ..._style_detected
methods don't do anything useful when there are more than two styles to choose from. So you can remove this block and the call to correct_style_detected
.
I just had one more comment. And the Travis build is failing. |
97cebf1
to
d9ba4c5
Compare
@jonas054 The failures were related to RuboCop rules. I have resolved all of them except for |
d9ba4c5
to
dfd0140
Compare
How to best refactor could be a long discussion, so for me it's OK if you just run |
dfd0140
to
cf34a75
Compare
@jonas054 Ran the |
👍 Looks good now. |
Drop the |
As per [the style-guide][style-guide], even though multi-line chaining is considered ugly and we should generally find ways to re-write the code, sometimes this simply isn't possible. Regardless, some code-bases would like to allow multi-line blocks to be used in method chains. We can also argue that multi-line chaining, when used, is better like this: ``` array.map { |x| x * 2 }.map(&:to_s) ``` than like this: ``` array.map do |x| x * 2 end.map(&:to_s) ``` This style, when selected, forces multi-line blocks to use braces if their return value is being chained with another method. I believe that this is a better option than the current default configuration which enforces the second of the two code examples. [style-guide]: https://github.com/bbatsov/ruby-style-guide
cf34a75
to
b23c144
Compare
@bbatsov I always forget the period! Period removed and branch rebased on master. |
…aining New braces_for_chaining style for Style/BlockDelimiters.
👍 Thanks! |
As per the style-guide, even though multi-line chaining
is considered ugly and we should generally find ways to re-write the
code, sometimes this simply isn't possible. Regardless, some code-bases
would like to allow multi-line blocks to be used in method chains.
We can also argue that multi-line chaining, when used, is better like
this:
than like this:
This style, when selected, forces multi-line blocks to use braces if
their return value is being chained with another method. I believe
that this is a better option than the current default configuration
which enforces the second of the two code examples.