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 #7841] Fix an error for Style/TrailingCommaInBlockArgs cop #7843

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
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