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 false negative for Layout/SpaceAroundBlockParameters #6797

Merged
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.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* [#6777](https://github.com/rubocop-hq/rubocop/issues/6777): Fix a false positive for `Style/TrivialAccessors` when using trivial reader/writer methods at the top level. ([@koic][])
* [#6799](https://github.com/rubocop-hq/rubocop/pull/6799): Fix errors for `Style/ConditionalAssignment`, `Style/IdenticalConditionalBranches`, `Lint/ElseLayout`, and `Layout/IndentationWidth` with empty braces. ([@pocke][])
* [#6802](https://github.com/rubocop-hq/rubocop/pull/6802): Fix auto-correction for `Style/SymbolArray` with array contains interpolation when `EnforcedStyle` is `brackets`. ([@pocke][])
* [#6797](https://github.com/rubocop-hq/rubocop/pull/6797): Fix false negative for Layout/SpaceAroundBlockParameters on block parameter with parens. ([@pocke][])

### Changes

Expand Down
20 changes: 13 additions & 7 deletions lib/rubocop/cop/layout/space_around_block_parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,22 @@ def last_end_pos_inside_pipes(pos)
end

def check_each_arg(args)
args.children.butfirst.each do |arg|
expr = arg.source_range
check_no_space(
range_with_surrounding_space(range: expr, side: :left).begin_pos,
expr.begin_pos - 1,
'Extra space before'
)
args.children.each do |arg|
check_arg(arg)
end
end

def check_arg(arg)
arg.children.each { |a| check_arg(a) } if arg.mlhs_type?

expr = arg.source_range
check_no_space(
range_with_surrounding_space(range: expr, side: :left).begin_pos,
expr.begin_pos - 1,
'Extra space before'
)
end

def check_space(space_begin_pos, space_end_pos, range, msg)
return if space_begin_pos != space_end_pos

Expand Down
19 changes: 19 additions & 0 deletions spec/rubocop/cop/layout/space_around_block_parameters_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@
RUBY
end

it 'registers an offense for space with parens' do
expect_offense(<<-RUBY.strip_indent)
{}.each { |a, (x, y), z| puts x }
^ Extra space before block parameter detected.
^ Extra space before block parameter detected.
^ Extra space before block parameter detected.
RUBY
end

context 'trailing comma' do
it 'registers an offense for space after the last comma' do
expect_offense(<<-RUBY.strip_indent)
Expand Down Expand Up @@ -215,6 +224,16 @@
RUBY
end

it 'registers an offense for space with parens at middle' do
expect_offense(<<-RUBY.strip_indent)
{}.each { |(x, y), z| puts x }
^^^^^^^ Space before first block parameter missing.
^ Extra space before block parameter detected.
^ Extra space before block parameter detected.
^ Space after last block parameter missing.
RUBY
end

context 'trailing comma' do
it 'accepts space after the last comma' do
expect_no_offenses('{}.each { | x, | puts x }')
Expand Down