From 00f5c5c41b3283507163131f704cb3a77eb3bd26 Mon Sep 17 00:00:00 2001 From: Ted Johansson Date: Mon, 11 Feb 2019 11:36:24 +0800 Subject: [PATCH] [Fix #6755] Prevent Style/TrailingCommaInArgument from breaking when 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. --- CHANGELOG.md | 4 ++++ lib/rubocop/cop/mixin/trailing_comma.rb | 2 +- .../style/trailing_comma_in_arguments_spec.rb | 17 +++++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 473e3227383..d6bdc21ed0f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ ## 0.64.0 (2019-02-10) +### Bug fixes + +* [#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][]) + ### New features * [#6704](https://github.com/rubocop-hq/rubocop/pull/6704): Add new `Rails/ReflectionClassName` cop. ([@Bhacaz][]) diff --git a/lib/rubocop/cop/mixin/trailing_comma.rb b/lib/rubocop/cop/mixin/trailing_comma.rb index 4f8819dc983..f9754b07cef 100644 --- a/lib/rubocop/cop/mixin/trailing_comma.rb +++ b/lib/rubocop/cop/mixin/trailing_comma.rb @@ -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, diff --git a/spec/rubocop/cop/style/trailing_comma_in_arguments_spec.rb b/spec/rubocop/cop/style/trailing_comma_in_arguments_spec.rb index a781a60f50d..77e0b30776b 100644 --- a/spec/rubocop/cop/style/trailing_comma_in_arguments_spec.rb +++ b/spec/rubocop/cop/style/trailing_comma_in_arguments_spec.rb @@ -416,6 +416,23 @@ ) 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.bar( + baz: 1, + )&.fetch(:qux) + RUBY + end end context 'when EnforcedStyleForMultiline is consistent_comma' do