From b6c9b2ac675315973ef301ce569c70e698007d7b Mon Sep 17 00:00:00 2001 From: Genadi Samokovarov Date: Wed, 22 Jun 2022 11:33:00 +0300 Subject: [PATCH] Fix Layout/SpaceInsideBlockBraces for blocks with numbered arguments The `Layout/SpaceInsideBlockBraces` was not being applied for blocks with numbered arguments as they have AST node type of `numblock` and the cop only ran for `block` nodes. At Dext we caught spacing errors during code review and wandered why the cop didn't run at all. The piece of code was in the lines of ```ruby Tax.where(condition).partition {_1.by_something} ``` Not running Layout/SpaceInsideBlockBraces for `numblock` nodes was the cause. --- ..._inside_block_braces_with_numbered_arguments.md | 1 + .../cop/layout/space_inside_block_braces.rb | 4 +++- .../cop/layout/space_inside_block_braces_spec.rb | 14 ++++++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 changelog/fix_space_inside_block_braces_with_numbered_arguments.md 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