Skip to content

Commit

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

This PR fixes a false positive for `Rails/ContentTag`
when using using the `tag` method with 3 or more arguments.
  • Loading branch information
koic committed Sep 28, 2021
1 parent dfd8112 commit 162deaa
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
1 change: 1 addition & 0 deletions changelog/fix_a_false_positive_for_rails_content_tag.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#556](https://github.com/rubocop/rubocop-rails/issues/556): Fix a false positive for `Rails/ContentTag` when using using the `tag` method with 3 or more arguments. ([@koic][])
1 change: 1 addition & 0 deletions lib/rubocop/cop/rails/content_tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def on_new_investigation

def on_send(node)
return unless node.receiver.nil?
return if node.arguments.count >= 3

first_argument = node.first_argument
return if !first_argument ||
Expand Down
15 changes: 10 additions & 5 deletions spec/rubocop/cop/rails/content_tag_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,19 @@
RUBY
end

it 'corrects an offense with all arguments' do
expect_offense(<<~RUBY)
# Prevents `ArgumentError` reported by https://github.com/rubocop/rubocop-rails/issues/556.
# See: https://api.rubyonrails.org/v6.1.4/classes/ActionView/Helpers/TagHelper.html#method-i-tag-label-Legacy+syntax
it 'does not register an offense with all arguments' do
expect_no_offenses(<<~RUBY)
tag(:br, {class: ["strong", "highlight"]}, true, false)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `tag.br` instead of `tag(:br)`.
RUBY
end

expect_correction(<<~RUBY)
tag.br({class: ["strong", "highlight"]}, true, false)
# Prevents `ArgumentError` reported by https://github.com/rubocop/rubocop-rails/issues/556.
# See: https://api.rubyonrails.org/v6.1.4/classes/ActionView/Helpers/TagHelper.html#method-i-tag-label-Legacy+syntax
it 'does not register an offense with three arguments' do
expect_no_offenses(<<~RUBY)
tag(:br, {class: ["strong", "highlight"]}, true)
RUBY
end

Expand Down

0 comments on commit 162deaa

Please sign in to comment.