diff --git a/CHANGELOG.md b/CHANGELOG.md index fbc4139bc3..b09c78521d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Bug fixes * [#421](https://github.com/rubocop-hq/rubocop-rails/issues/421): Fix incorrect auto-correct for `Rails/LinkToBlank` when using `target: '_blank'` with hash brackets for the option. ([@koic][]) +* [#436](https://github.com/rubocop-hq/rubocop-rails/issues/436): Fix a false positive for `Rails/ContentTag` when the first argument is a splat argument. ([@koic][]) ### Changes diff --git a/lib/rubocop/cop/rails/content_tag.rb b/lib/rubocop/cop/rails/content_tag.rb index 483a90ee72..9a8ad3aeb5 100644 --- a/lib/rubocop/cop/rails/content_tag.rb +++ b/lib/rubocop/cop/rails/content_tag.rb @@ -30,9 +30,7 @@ class ContentTag < Base def on_send(node) first_argument = node.first_argument - return unless first_argument - - return if first_argument.variable? || first_argument.send_type? || first_argument.const_type? + return if !first_argument || allowed_argument?(first_argument) add_offense(node) do |corrector| autocorrect(corrector, node) @@ -41,6 +39,10 @@ def on_send(node) private + def allowed_argument?(argument) + argument.variable? || argument.send_type? || argument.const_type? || argument.splat_type? + end + def autocorrect(corrector, node) if method_name?(node.first_argument) range = correction_range(node) diff --git a/spec/rubocop/cop/rails/content_tag_spec.rb b/spec/rubocop/cop/rails/content_tag_spec.rb index bb7210bbf7..bbb9f52eb7 100644 --- a/spec/rubocop/cop/rails/content_tag_spec.rb +++ b/spec/rubocop/cop/rails/content_tag_spec.rb @@ -165,6 +165,12 @@ content_tag($name, "Hello world!", class: ["strong", "highlight"]) RUBY end + + it 'does not register an offence when the first argument is a splat argument' do + expect_no_offenses(<<~RUBY) + content_tag(*args, &block) + RUBY + end end context 'when the first argument is a method' do