Skip to content

Commit

Permalink
[Fix rubocop#7841] Fix an error for Style/TrailingCommaInBlockArgs cop
Browse files Browse the repository at this point in the history
Fixes rubocop#7841.

This PR fixes an error for `Style/TrailingCommaInBlockArgs` cop
when lambda literal (`->`) has multiple arguments.
It will not be checked in the case of lambda literal because
lambda literal (`->`) never have a block arguments.

```console
# Valid syntax
% ruby -ce '-> (foo, bar) { do_something(foo, bar) }'
Syntax OK

# Syntax error
% ruby -ce '-> { |foo| do_something }'
-e:1: syntax error, unexpected '|'
-> { |foo| do_something }
      ^

# Also syntax error
% ruby -ce '-> (foo, bar,) { do_something(foo, bar) }'
-e:1: syntax error, unexpected ')'
-> (foo, bar,) { do_something(foo, bar) }
              ^
-e:1: syntax error, unexpected '}', expecting end-of-input
r,) { do_something(foo, bar) }
                              ^
```
  • Loading branch information
koic committed Apr 4, 2020
1 parent 2ef0e72 commit 6169ce3
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 6169ce3

Please sign in to comment.