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

Semantic block delimiter should allow braces on procedural one-liners #6393

Closed
davearonson opened this issue Oct 18, 2018 · 4 comments
Closed

Comments

@davearonson
Copy link
Contributor

I've seen lots of Ruby code that uses braces around one-line blocks even when the code is not using the result of the block, i.e., when it's "procedural" rather than "functional". It's often appeared in code that was otherwise great, and I've never seen anybody object to this.

Except Rubocop. :-)

The choices of block delimiter style seem to be:

  • line_count_based: One-liners have braces; everything else has do-end, even if functional.
  • semantic: Functional has braces; everything else has do-end, even if one-liner.
  • braces_for_chaining: Chained functional blocks, and one-liners, have braces; everything else has do-end, even if functional (when not chained).

There does not seem to be a choice that means "functional blocks (whether chained or not), and one-liners, have braces, even if procedural, and everything else has do-end." That is my preferred style.

What I'd like to see is that semantic allows procedural one-liners to have braces. This may be controlled by another configuration option, or always.

Again, I'll try to Make It So myself, ideally this month to count towards a Hacktoberfest shirt. :-)

@davearonson
Copy link
Contributor Author

I've started working on this. In the interests of keeping semantic mode purely semantic, at least as an option, I'm taking the tack of making "allow braces on procedural oneliners" an additional config option.

davearonson added a commit to davearonson/rubocop that referenced this issue Oct 30, 2018
option to fine-tune behavior of Style/BlockDelimiters
@searls
Copy link

searls commented Dec 3, 2018

Heads up that I roundaboutly forked and implemented this behavior in the standard gem here: https://github.com/testdouble/standard/blob/master/lib/standard/cop/semantic_blocks.rb

Would love for this (and any other) cops standard shakes loose to wind up upstream in rubocop, though. Feel free to crib from this and add to your rubocop PR

@jherdman
Copy link

This would be really great. Here are examples that the semantic style that should probably use { }:

git_source(:github) { |repo| "https://github.com/#{repo}.git" }
# a factory_bot factory:

factory :user do
  name { |n| "James#{n}" }
end

@searls
Copy link

searls commented Feb 19, 2019

@jherdman Yes, I believe that's how StandardRB behaves currently

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

No branches or pull requests

3 participants