Skip to content

Add new Style/SingleLineDoEndBlock cop#12227

Merged
bbatsov merged 1 commit intorubocop:masterfrom
koic:add_new_style_single_line_do_end
Sep 29, 2023
Merged

Add new Style/SingleLineDoEndBlock cop#12227
bbatsov merged 1 commit intorubocop:masterfrom
koic:add_new_style_single_line_do_end

Conversation

@koic
Copy link
Member

@koic koic commented Sep 29, 2023

This PR adds new Style/SingleLineDoEndBlock cop that checks for single-line do...end blocks.

# bad
foo do |arg| bar(arg) end

# good
foo do |arg|
  bar(arg)
end

# bad
->(arg) do bar(arg) end

# good
->(arg) { bar(arg) }

In practice a single line do...end is autocorrected when EnforcedStyle: semantic in Style/BlockDelimiters. It can also be detected by this cop if it is written by handcraft. So I decided to introduce this new cop instead of an autocorrect extension of Style/BlockDelimiters.


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • 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.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think there's a style guide entry that you can refer here.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure! I've opened rubocop/ruby-style-guide#922 to add a new rule for this one.

Copy link
Collaborator

Choose a reason for hiding this comment

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

You might also add a note about why the auto-correct doesn't replace do/end with {}, as perhaps this won't be clear for some people.

Copy link
Member Author

Choose a reason for hiding this comment

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

Indeed, I've added an explain for that.

koic added a commit to koic/ruby-style-guide that referenced this pull request Sep 29, 2023
Follow up rubocop/rubocop#12227 (comment)

This PR adds new `Single-line `do`...`end` block` rule.

Use multi-line `do`...`end` block instead of single-line `do`...`end` block.

```ruby
# bad
foo do |arg| bar(arg) end

# good
foo do |arg|
  bar(arg)
end

# bad
->(arg) do bar(arg) end

# good
->(arg) { bar(arg) }
```
koic added a commit to koic/ruby-style-guide that referenced this pull request Sep 29, 2023
Follow up rubocop/rubocop#12227 (comment)

This PR adds new `Single-line `do`...`end` block` rule.

Use multi-line `do`...`end` block instead of single-line `do`...`end` block.

```ruby
# bad
foo do |arg| bar(arg) end

# good
foo do |arg|
  bar(arg)
end

# bad
->(arg) do bar(arg) end

# good
->(arg) { bar(arg) }
```
koic added a commit to koic/ruby-style-guide that referenced this pull request Sep 29, 2023
Follow up rubocop/rubocop#12227 (comment)

This PR adds new "Single-line `do`...`end` block" rule.

Use multi-line `do`...`end` block instead of single-line `do`...`end` block.

```ruby
# bad
foo do |arg| bar(arg) end

# good
foo do |arg|
  bar(arg)
end

# bad
->(arg) do bar(arg) end

# good
->(arg) { bar(arg) }
```
@koic koic force-pushed the add_new_style_single_line_do_end branch from d3fcf5b to 74a9b8f Compare September 29, 2023 12:48
@koic koic changed the title Add new Style/SingleLineDoEnd cop Add new Style/SingleLineDoEndBlock cop Sep 29, 2023
This PR adds new `Style/SingleLineDoEndBlock` cop
that checks for single-line `do`...`end` blocks.

```ruby
# bad
foo do |arg| bar(arg) end

# good
foo do |arg|
  bar(arg)
end

# bad
->(arg) do bar(arg) end

# good
->(arg) { bar(arg) }
```

In practice a single line `do`...`end` is autocorrected when `EnforcedStyle: semantic` in `Style/BlockDelimiters`.
It can also be detected by this cop if it is written by handcraft.
So I decided to introduce this new cop instead of an autocorrect extension of `Style/BlockDelimiters`.
@koic koic force-pushed the add_new_style_single_line_do_end branch from 74a9b8f to 8ad7335 Compare September 29, 2023 12:59
@bbatsov bbatsov merged commit 08f475d into rubocop:master Sep 29, 2023
@bbatsov
Copy link
Collaborator

bbatsov commented Sep 29, 2023

Thanks!

@koic koic deleted the add_new_style_single_line_do_end branch September 29, 2023 14:01
bbatsov pushed a commit to rubocop/ruby-style-guide that referenced this pull request Nov 5, 2023
Follow up rubocop/rubocop#12227 (comment)

This PR adds new "Single-line `do`...`end` block" rule.

Use multi-line `do`...`end` block instead of single-line `do`...`end` block.

```ruby
# bad
foo do |arg| bar(arg) end

# good
foo do |arg|
  bar(arg)
end

# bad
->(arg) do bar(arg) end

# good
->(arg) { bar(arg) }
```
kachick added a commit to kachick/striuct that referenced this pull request Dec 4, 2023
kachick added a commit to kachick/striuct that referenced this pull request Dec 4, 2023
* Update rubocop requirement from ~> 1.56.4 to ~> 1.58.0

Updates the requirements on [rubocop](https://github.com/rubocop/rubocop) to permit the latest version.
- [Release notes](https://github.com/rubocop/rubocop/releases)
- [Changelog](https://github.com/rubocop/rubocop/blob/master/CHANGELOG.md)
- [Commits](rubocop/rubocop@v1.56.4...v1.58.0)

---
updated-dependencies:
- dependency-name: rubocop
  dependency-type: direct:development
...

Signed-off-by: dependabot[bot] <[email protected]>

* Suppress `Style/SingleLineDoEndBlock` in tests

rubocop/rubocop#12227

---------

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Kenichi Kamiya <[email protected]>
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.

2 participants