From 162deaa62309682bbaea65e5922dfdbb8487e6f9 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Mon, 27 Sep 2021 12:34:59 +0900 Subject: [PATCH] [Fix #556] Fix a false positive for `Rails/ContentTag` Fixes #556. This PR fixes a false positive for `Rails/ContentTag` when using using the `tag` method with 3 or more arguments. --- .../fix_a_false_positive_for_rails_content_tag.md | 1 + lib/rubocop/cop/rails/content_tag.rb | 1 + spec/rubocop/cop/rails/content_tag_spec.rb | 15 ++++++++++----- 3 files changed, 12 insertions(+), 5 deletions(-) create mode 100644 changelog/fix_a_false_positive_for_rails_content_tag.md diff --git a/changelog/fix_a_false_positive_for_rails_content_tag.md b/changelog/fix_a_false_positive_for_rails_content_tag.md new file mode 100644 index 0000000000..f01cc3b728 --- /dev/null +++ b/changelog/fix_a_false_positive_for_rails_content_tag.md @@ -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][]) diff --git a/lib/rubocop/cop/rails/content_tag.rb b/lib/rubocop/cop/rails/content_tag.rb index 2eacc03055..87c69f3a41 100644 --- a/lib/rubocop/cop/rails/content_tag.rb +++ b/lib/rubocop/cop/rails/content_tag.rb @@ -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 || diff --git a/spec/rubocop/cop/rails/content_tag_spec.rb b/spec/rubocop/cop/rails/content_tag_spec.rb index 2d18c4f323..01bf7a2db4 100644 --- a/spec/rubocop/cop/rails/content_tag_spec.rb +++ b/spec/rubocop/cop/rails/content_tag_spec.rb @@ -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