Skip to content

Commit

Permalink
[Fix #6755] Prevent Style/TrailingCommaInArgument from breaking when …
Browse files Browse the repository at this point in the history
…a safe method call is chained on the offending method

This cop would error out on code like:

```
foo.bar(
  baz: 1,
)&.fetch(:qux)
```

This was happening because the cop wasn't taking `csend` (safe navigation)
node types into consideration.
  • Loading branch information
Drenmi committed Feb 19, 2019
1 parent 1f51e4c commit e81e7a2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* [#6763](https://github.com/rubocop-hq/rubocop/pull/6763): Fix false positives in range literals for `Style/MethodCallWithArgsParentheses` `omit_parentheses`. ([@gsamokovarov][])
* [#6748](https://github.com/rubocop-hq/rubocop/issues/6748): Fix `Style/RaiseArgs` auto-correction breaking in contexts that require parentheses. ([@drenmi][])
* [#6751](https://github.com/rubocop-hq/rubocop/issues/6751): Prevent `Style/OneLineConditional` from breaking on `retry` and `break` keywords. ([@drenmi][])
* [#6755](https://github.com/rubocop-hq/rubocop/issues/6755): Prevent `Style/TrailingCommaInArgument` from breaking when a safe method call is chained on the offending method. ([@drenmi][], [@hoshinotsuyoshi][])

### Changes

Expand Down
4 changes: 2 additions & 2 deletions lib/rubocop/cop/mixin/trailing_comma.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def multiline?(node)
end

def method_name_and_arguments_on_same_line?(node)
node.send_type? &&
%i[send csend].include?(node.type) &&
node.loc.selector.line == node.arguments.last.last_line &&
node.last_line == node.arguments.last.last_line
end
Expand All @@ -104,7 +104,7 @@ def allowed_multiline_argument?(node)
end

def elements(node)
return node.children unless node.send_type?
return node.children unless %i[csend send].include?(node.type)

node.arguments.flat_map do |argument|
# For each argument, if it is a multi-line hash without braces,
Expand Down
25 changes: 25 additions & 0 deletions spec/rubocop/cop/style/trailing_comma_in_arguments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,31 @@
)
RUBY
end

it 'does not break when a method call is chaned on the offending one' do
expect_no_offenses(<<-RUBY.strip_indent)
foo.bar(
baz: 1,
).fetch(:qux)
RUBY
end

it 'does not break when a safe method call is chained on the ' \
'offending one', :ruby23 do
expect_no_offenses(<<-RUBY.strip_indent)
foo
&.do_something(:bar, :baz)
RUBY
end

it 'does not break when a safe method call is chained on the ' \
'offending one', :ruby23 do
expect_no_offenses(<<-RUBY.strip_indent)
foo.bar(
baz: 1,
)&.fetch(:qux)
RUBY
end
end

context 'when EnforcedStyleForMultiline is consistent_comma' do
Expand Down

0 comments on commit e81e7a2

Please # to comment.