Skip to content

Commit efe0ec9

Browse files
committed
Tweak offense message for Rails/ContentTag
Follow up to rubocop#526.
1 parent 6163f9a commit efe0ec9

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

lib/rubocop/cop/rails/content_tag.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class ContentTag < Base
2525

2626
minimum_target_rails_version 5.1
2727

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

3131
def on_new_investigation
@@ -38,8 +38,11 @@ def on_send(node)
3838
allowed_argument?(first_argument) ||
3939
corrected_ancestor?(node)
4040

41-
add_offense(node) do |corrector|
42-
autocorrect(corrector, node)
41+
preferred_method = node.first_argument.value.to_s.underscore
42+
message = format(MSG, preferred_method: preferred_method, current_argument: first_argument.source)
43+
44+
add_offense(node, message: message) do |corrector|
45+
autocorrect(corrector, node, preferred_method)
4346

4447
@corrected_nodes ||= Set.new.compare_by_identity
4548
@corrected_nodes.add(node)
@@ -60,11 +63,11 @@ def allowed_argument?(argument)
6063
allowed_name?(argument)
6164
end
6265

63-
def autocorrect(corrector, node)
66+
def autocorrect(corrector, node, preferred_method)
6467
range = correction_range(node)
6568

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

6972
corrector.replace(range, replacement)
7073
end

spec/rubocop/cop/rails/content_tag_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
it 'corrects an offense with only tag name' do
6868
expect_offense(<<~RUBY)
6969
tag(:br)
70-
^^^^^^^^ Use `tag.something` instead of `tag(:something)`.
70+
^^^^^^^^ Use `tag.br` instead of `tag(:br)`.
7171
RUBY
7272

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

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

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

112112
expect_correction(<<~RUBY)

0 commit comments

Comments
 (0)