Skip to content

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

Closed
@davearonson

Description

@davearonson
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. :-)

Activity

davearonson

davearonson commented on Oct 27, 2018

@davearonson
ContributorAuthor

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.

searls

searls commented on Dec 3, 2018

@searls

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

jherdman commented on Feb 19, 2019

@jherdman

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

searls commented on Feb 19, 2019

@searls

@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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @jherdman@searls@davearonson

        Issue actions

          Semantic block delimiter should allow braces on procedural one-liners · Issue #6393 · rubocop/rubocop