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

Fix Style/BlockDelimiters for blocks with numbered arguments #10749

Merged
merged 1 commit into from Jun 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/fix_block_delimiters_with_numbered_arguments.md
@@ -0,0 +1 @@
* [#10749](https://github.com/rubocop/rubocop/pull/10749): Fix Style/BlockDelimiters for blocks with numbered arguments. ([@gsamokovarov][])
4 changes: 3 additions & 1 deletion lib/rubocop/cop/style/block_delimiters.rb
Expand Up @@ -184,6 +184,8 @@ def on_block(node)
end
end

alias on_numblock on_block

private

def autocorrect(corrector, node)
Expand Down Expand Up @@ -300,7 +302,7 @@ def with_block?(node)

def get_blocks(node, &block)
case node.type
when :block
when :block, :numblock
yield node
when :send
get_blocks(node.receiver, &block) if node.receiver
Expand Down
29 changes: 29 additions & 0 deletions spec/rubocop/cop/style/block_delimiters_spec.rb
Expand Up @@ -27,6 +27,35 @@
}, 1
RUBY
end

context 'Ruby >= 2.7', :ruby27 do
it 'registers an offense for a single line numblock with do-end' do
expect_offense(<<~RUBY)
each do _1 end
^^ Prefer `{...}` over `do...end` for single-line blocks.
RUBY
end

it 'accepts a single line numblock with braces' do
expect_no_offenses('each { _1 }')
end

it 'accepts a multi-line numblock with do-end' do
expect_no_offenses(<<~RUBY)
each do
_1
end
RUBY
end

it 'accepts a multi-line numblock that needs braces to be valid ruby' do
expect_no_offenses(<<~RUBY)
puts [1, 2, 3].map {
_1 * _1
}, 1
RUBY
end
end
end

context 'EnforcedStyle: semantic' do
Expand Down