Skip to content

Commit

Permalink
Merge pull request #7843 from koic/fix_error_for_style_trailing_comma…
Browse files Browse the repository at this point in the history
…_in_block_args

[Fix #7841] Fix an error for `Style/TrailingCommaInBlockArgs` cop
  • Loading branch information
koic committed Apr 4, 2020
2 parents 2ef0e72 + 6169ce3 commit 28f18ac
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@

* [#7842](https://github.com/rubocop-hq/rubocop/issues/7842): Fix a false positive for `Lint/RaiseException` when raising Exception with explicit namespace. ([@koic][])
* [#7834](https://github.com/rubocop-hq/rubocop/issues/7834): Fix `Lint/UriRegexp` to register offense with array arguments. ([@tejasbubane][])
* [#7841](https://github.com/rubocop-hq/rubocop/issues/7841): Fix an error for `Style/TrailingCommaInBlockArgs` when lambda literal (`->`) has multiple arguments. ([@koic][])

## 0.81.0 (2020-04-01)

Expand Down
3 changes: 3 additions & 0 deletions lib/rubocop/cop/style/trailing_comma_in_block_args.rb
Expand Up @@ -44,6 +44,9 @@ class TrailingCommaInBlockArgs < Cop
MSG = 'Useless trailing comma present in block arguments.'

def on_block(node)
# lambda literal (`->`) never has block arguments.
return if node.send_node.lambda_literal?

return unless useless_trailing_comma?(node)

add_offense(node, location: last_comma(node).pos)
Expand Down
28 changes: 28 additions & 0 deletions spec/rubocop/cop/style/trailing_comma_in_block_args_spec.rb
Expand Up @@ -139,4 +139,32 @@
RUBY
end
end

context 'when `->` has multiple arguments' do
it 'does not registers an offense' do
expect_no_offenses(<<~RUBY)
-> (foo, bar) { do_something(foo, bar) }
RUBY
end
end

context 'when `lambda` has multiple arguments' do
it 'does not register an offense when more than one argument is ' \
'present with no trailing comma' do
expect_no_offenses(<<~RUBY)
lambda { |foo, bar| do_something(foo, bar) }
RUBY
end

it "registers an offense and corrects when a trailing comma isn't needed" do
expect_offense(<<~RUBY)
lambda { |foo, bar,| do_something(foo, bar) }
^ Useless trailing comma present in block arguments.
RUBY

expect_correction(<<~RUBY)
lambda { |foo, bar| do_something(foo, bar) }
RUBY
end
end
end

0 comments on commit 28f18ac

Please sign in to comment.