diff --git a/changelog/fix_space_inside_block_braces_with_numbered_arguments.md b/changelog/fix_space_inside_block_braces_with_numbered_arguments.md new file mode 100644 index 00000000000..e539c9617f0 --- /dev/null +++ b/changelog/fix_space_inside_block_braces_with_numbered_arguments.md @@ -0,0 +1 @@ +* [#10736](https://github.com/rubocop/rubocop/pull/10736): Fix Layout/SpaceInsideBlockBraces for blocks with numbered arguments. ([@gsamokovarov][]) diff --git a/lib/rubocop/cop/layout/space_inside_block_braces.rb b/lib/rubocop/cop/layout/space_inside_block_braces.rb index 8a0d9a8fc1e..54c18d2c6f5 100644 --- a/lib/rubocop/cop/layout/space_inside_block_braces.rb +++ b/lib/rubocop/cop/layout/space_inside_block_braces.rb @@ -98,6 +98,8 @@ def on_block(node) check_inside(node, left_brace, right_brace) end + alias on_numblock on_block + private def check_inside(node, left_brace, right_brace) @@ -126,7 +128,7 @@ def adjacent_braces(left_brace, right_brace) end def braces_with_contents_inside(node, inner) - args_delimiter = node.arguments.loc.begin # Can be ( | or nil. + args_delimiter = node.arguments.loc.begin if node.block_type? # Can be ( | or nil. check_left_brace(inner, node.loc.begin, args_delimiter) check_right_brace(inner, node.loc.begin, node.loc.end, node.single_line?) diff --git a/spec/rubocop/cop/layout/space_inside_block_braces_spec.rb b/spec/rubocop/cop/layout/space_inside_block_braces_spec.rb index 10c22194f8d..735fbc30392 100644 --- a/spec/rubocop/cop/layout/space_inside_block_braces_spec.rb +++ b/spec/rubocop/cop/layout/space_inside_block_braces_spec.rb @@ -90,6 +90,20 @@ end end + context 'Ruby >= 2.7', :ruby27 do + it 'registers an offense for numblocks without inner space' do + expect_offense(<<~RUBY) + [1, 2, 3].each {_1 * 2} + ^ Space missing inside {. + ^ Space missing inside }. + RUBY + + expect_correction(<<~RUBY) + [1, 2, 3].each { _1 * 2 } + RUBY + end + end + it 'accepts braces surrounded by spaces' do expect_no_offenses('each { puts }') end