Skip to content

Commit

Permalink
Fix Layout/SpaceInsideBlockBraces for blocks with numbered arguments
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
gsamokovarov committed Jun 22, 2022
1 parent dfc6e87 commit b6c9b2a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
@@ -0,0 +1 @@
* [#10736](https://github.com/rubocop/rubocop/pull/10736): Fix Layout/SpaceInsideBlockBraces for blocks with numbered arguments. ([@gsamokovarov][])
4 changes: 3 additions & 1 deletion lib/rubocop/cop/layout/space_inside_block_braces.rb
Expand Up @@ -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)
Expand Down Expand Up @@ -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?)
Expand Down
14 changes: 14 additions & 0 deletions spec/rubocop/cop/layout/space_inside_block_braces_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit b6c9b2a

Please sign in to comment.