Skip to content

Commit 8fe4946

Browse files
authored
Merge pull request #6757 from Drenmi/bugfix/trailing-comma-in-arguments-cop
[Fix #6755] Prevent Style/TrailingCommaInArgument from breaking when a safe method call is chained on the offending method
2 parents 1f51e4c + e81e7a2 commit 8fe4946

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* [#6763](https://github.com/rubocop-hq/rubocop/pull/6763): Fix false positives in range literals for `Style/MethodCallWithArgsParentheses` `omit_parentheses`. ([@gsamokovarov][])
1313
* [#6748](https://github.com/rubocop-hq/rubocop/issues/6748): Fix `Style/RaiseArgs` auto-correction breaking in contexts that require parentheses. ([@drenmi][])
1414
* [#6751](https://github.com/rubocop-hq/rubocop/issues/6751): Prevent `Style/OneLineConditional` from breaking on `retry` and `break` keywords. ([@drenmi][])
15+
* [#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][])
1516

1617
### Changes
1718

lib/rubocop/cop/mixin/trailing_comma.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def multiline?(node)
9191
end
9292

9393
def method_name_and_arguments_on_same_line?(node)
94-
node.send_type? &&
94+
%i[send csend].include?(node.type) &&
9595
node.loc.selector.line == node.arguments.last.last_line &&
9696
node.last_line == node.arguments.last.last_line
9797
end
@@ -104,7 +104,7 @@ def allowed_multiline_argument?(node)
104104
end
105105

106106
def elements(node)
107-
return node.children unless node.send_type?
107+
return node.children unless %i[csend send].include?(node.type)
108108

109109
node.arguments.flat_map do |argument|
110110
# For each argument, if it is a multi-line hash without braces,

spec/rubocop/cop/style/trailing_comma_in_arguments_spec.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,31 @@
416416
)
417417
RUBY
418418
end
419+
420+
it 'does not break when a method call is chaned on the offending one' do
421+
expect_no_offenses(<<-RUBY.strip_indent)
422+
foo.bar(
423+
baz: 1,
424+
).fetch(:qux)
425+
RUBY
426+
end
427+
428+
it 'does not break when a safe method call is chained on the ' \
429+
'offending one', :ruby23 do
430+
expect_no_offenses(<<-RUBY.strip_indent)
431+
foo
432+
&.do_something(:bar, :baz)
433+
RUBY
434+
end
435+
436+
it 'does not break when a safe method call is chained on the ' \
437+
'offending one', :ruby23 do
438+
expect_no_offenses(<<-RUBY.strip_indent)
439+
foo.bar(
440+
baz: 1,
441+
)&.fetch(:qux)
442+
RUBY
443+
end
419444
end
420445

421446
context 'when EnforcedStyleForMultiline is consistent_comma' do

0 commit comments

Comments
 (0)