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

New braces_for_chaining style for Style/BlockDelimiters. #2329

Merged

Conversation

panthomakos
Copy link
Contributor

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:

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.

@panthomakos panthomakos force-pushed the enforce-multi-line-brace-chaining branch from 48bb062 to de2bfc7 Compare October 20, 2015 04:49
@panthomakos panthomakos changed the title EnforceMultiLineBraceChaining for BlockDelimiters. EnforceMultiLineBraceChaining for BlockDelimiters Oct 20, 2015
@panthomakos panthomakos force-pushed the enforce-multi-line-brace-chaining branch from de2bfc7 to 04ac153 Compare October 20, 2015 16:12
@jonas054
Copy link
Collaborator

This is already taken care of if you configure

Style/BlockDelimiters:
  EnforcedStyle: semantic

@panthomakos
Copy link
Contributor Author

@jonas054 This is a little bit different because semantic requires {} on assignment as well:

Style/BlockDelimiters:
  EnforcedStyle: semantic

# good
variable = array.map { |i|
  i*2
}

This PR allows you to enforce do-end for multi-line blocks except when chaining methods:

Style/BlockDelimiters:
  EnforcedStyle: line_count_based
  EnforceMultiLineBraceChaining: true

# good
variable = array.map do |i|
  i * 2
end

# good
array.map{ |i|
  i * 2
}.map(&:to_sym)

@@ -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][])
Copy link
Collaborator

Choose a reason for hiding this comment

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

it's -> its

@jonas054
Copy link
Collaborator

I see. Then I think it's OK. But since the new parameter only makes sense for the line_count_based style, I think it's better to implement it as a third style, braces_for_chaining.

@panthomakos panthomakos force-pushed the enforce-multi-line-brace-chaining branch from 04ac153 to 7b6616e Compare October 26, 2015 16:11
@panthomakos panthomakos changed the title EnforceMultiLineBraceChaining for BlockDelimiters New braces_for_chaining style for Style/BlockDelimiters. Oct 26, 2015
@panthomakos panthomakos force-pushed the enforce-multi-line-brace-chaining branch 2 times, most recently from ecb7739 to 97cebf1 Compare October 26, 2015 16:14
@panthomakos
Copy link
Contributor Author

@jonas054 PTAL I've updated with your comments and implemented using the braces_for_chaining style.

@@ -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 }
Copy link
Collaborator

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.

@jonas054
Copy link
Collaborator

I just had one more comment. And the Travis build is failing.

@panthomakos panthomakos force-pushed the enforce-multi-line-brace-chaining branch from 97cebf1 to d9ba4c5 Compare October 27, 2015 13:32
@panthomakos
Copy link
Contributor Author

@jonas054 The failures were related to RuboCop rules. I have resolved all of them except for Class has too many lines. [167/147] Do you have a recommended way to resolve in this case? I don't see an obvious way to reduce the class size. I'm happy to refactor, but some guidance on direction would be helpful.

@panthomakos panthomakos force-pushed the enforce-multi-line-brace-chaining branch from d9ba4c5 to dfd0140 Compare October 27, 2015 13:36
@jonas054
Copy link
Collaborator

How to best refactor could be a long discussion, so for me it's OK if you just run ./bin/rubocop --auto-gen-config and add/commit the updated .rubocop_todo.yml. We can reduce the class length later IMO.

@panthomakos panthomakos force-pushed the enforce-multi-line-brace-chaining branch from dfd0140 to cf34a75 Compare October 27, 2015 16:51
@panthomakos
Copy link
Contributor Author

@jonas054 Ran the --auto-gen-config and that has resolved the class length issue.

@jonas054
Copy link
Collaborator

👍 Looks good now.

@bbatsov
Copy link
Collaborator

bbatsov commented Oct 28, 2015

Drop the . from the title of the commit message and rebase on top of the current master.

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
@panthomakos panthomakos force-pushed the enforce-multi-line-brace-chaining branch from cf34a75 to b23c144 Compare October 28, 2015 13:06
@panthomakos
Copy link
Contributor Author

@bbatsov I always forget the period!

Period removed and branch rebased on master.

bbatsov added a commit that referenced this pull request Oct 28, 2015
…aining

New braces_for_chaining style for Style/BlockDelimiters.
@bbatsov bbatsov merged commit b0866cd into rubocop:master Oct 28, 2015
@bbatsov
Copy link
Collaborator

bbatsov commented Oct 28, 2015

👍 Thanks!

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.

3 participants