Skip to content

Commit

Permalink
Tweak offense message for Rails/ContentTag
Browse files Browse the repository at this point in the history
Follow up to #526.
  • Loading branch information
koic committed Aug 21, 2021
1 parent 6163f9a commit efe0ec9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
13 changes: 8 additions & 5 deletions lib/rubocop/cop/rails/content_tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ContentTag < Base

minimum_target_rails_version 5.1

MSG = 'Use `tag.something` instead of `tag(:something)`.'
MSG = 'Use `tag.%<preferred_method>s` instead of `tag(%<current_argument>s)`.'
RESTRICT_ON_SEND = %i[tag].freeze

def on_new_investigation
Expand All @@ -38,8 +38,11 @@ def on_send(node)
allowed_argument?(first_argument) ||
corrected_ancestor?(node)

add_offense(node) do |corrector|
autocorrect(corrector, node)
preferred_method = node.first_argument.value.to_s.underscore
message = format(MSG, preferred_method: preferred_method, current_argument: first_argument.source)

add_offense(node, message: message) do |corrector|
autocorrect(corrector, node, preferred_method)

@corrected_nodes ||= Set.new.compare_by_identity
@corrected_nodes.add(node)
Expand All @@ -60,11 +63,11 @@ def allowed_argument?(argument)
allowed_name?(argument)
end

def autocorrect(corrector, node)
def autocorrect(corrector, node, preferred_method)
range = correction_range(node)

rest_args = node.arguments.drop(1)
replacement = "tag.#{node.first_argument.value.to_s.underscore}(#{rest_args.map(&:source).join(', ')})"
replacement = "tag.#{preferred_method}(#{rest_args.map(&:source).join(', ')})"

corrector.replace(range, replacement)
end
Expand Down
8 changes: 4 additions & 4 deletions spec/rubocop/cop/rails/content_tag_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
it 'corrects an offense with only tag name' do
expect_offense(<<~RUBY)
tag(:br)
^^^^^^^^ Use `tag.something` instead of `tag(:something)`.
^^^^^^^^ Use `tag.br` instead of `tag(:br)`.
RUBY

expect_correction(<<~RUBY)
Expand All @@ -78,7 +78,7 @@
it 'corrects an offense with all arguments' do
expect_offense(<<~RUBY)
tag(:br, {class: ["strong", "highlight"]}, true, false)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `tag.something` instead of `tag(:something)`.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `tag.br` instead of `tag(:br)`.
RUBY

expect_correction(<<~RUBY)
Expand All @@ -89,7 +89,7 @@
it 'corrects an offense when first argument is non-identifier string' do
expect_offense(<<~RUBY)
tag('foo-bar', class: 'strong')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `tag.something` instead of `tag(:something)`.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `tag.foo_bar` instead of `tag('foo-bar')`.
RUBY

expect_correction(<<~RUBY)
Expand All @@ -106,7 +106,7 @@
it 'corrects an offense when first argument is string starts with hyphen' do
expect_offense(<<~RUBY)
tag('-foo', class: 'strong')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `tag.something` instead of `tag(:something)`.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `tag._foo` instead of `tag('-foo')`.
RUBY

expect_correction(<<~RUBY)
Expand Down

0 comments on commit efe0ec9

Please sign in to comment.