Skip to content

Commit

Permalink
Merge pull request #10749 from gsamokovarov/numblock-delimiters
Browse files Browse the repository at this point in the history
Fix Style/BlockDelimiters for blocks with numbered arguments
  • Loading branch information
koic committed Jun 26, 2022
2 parents 9e286a0 + 199c4b7 commit ae3dec3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
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

0 comments on commit ae3dec3

Please sign in to comment.