Skip to content

Commit

Permalink
Merge pull request #437 from koic/fix_false_positive_for_rails_conten…
Browse files Browse the repository at this point in the history
…t_tag

[Fix #436] Fix a false positive for `Rails/ContentTag`
  • Loading branch information
koic authored Feb 15, 2021
2 parents b3766c9 + 1bd8ba4 commit e71791a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

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_argument?(first_argument)

add_offense(node) do |corrector|
autocorrect(corrector, node)
Expand All @@ -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)
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 e71791a

Please sign in to comment.