Skip to content

Commit

Permalink
[Fix #436] Fix a false positive for Rails/ContentTag
Browse files Browse the repository at this point in the history
Fixes #436.

This PR fixes a false positive for `Rails/ContentTag`
when the first argument is a splat argument.
  • Loading branch information
koic committed Feb 8, 2021
1 parent d6868a6 commit 33718bb
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change log

### Bug fixes

* [#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][])

## master (unreleased)

### Bug fixes
Expand Down
8 changes: 5 additions & 3 deletions lib/rubocop/cop/rails/content_tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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_type?(first_argument)

add_offense(node) do |corrector|
autocorrect(corrector, node)
Expand All @@ -41,6 +39,10 @@ def on_send(node)

private

def allowed_type?(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)
Expand Down
6 changes: 6 additions & 0 deletions spec/rubocop/cop/rails/content_tag_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 33718bb

Please sign in to comment.