Skip to content

Commit 6163f9a

Browse files
authored
Merge pull request rubocop#526 from tabuchi0919/fix_target_of_rails_content_tag
[Fix rubocop#260] Fix target of `Rails/ContentTag`
2 parents 8c49e97 + 3474d16 commit 6163f9a

File tree

5 files changed

+98
-124
lines changed

5 files changed

+98
-124
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
* [#520](https://github.com/rubocop/rubocop-rails/pull/520): Support auto-correction for `Rails/ScopeArgs`. ([@koic][])
99
* [#524](https://github.com/rubocop/rubocop-rails/pull/524): Add new `Rails/RedundantTravelBack` cop. ([@koic][])
1010

11+
## Changes
12+
13+
* [#260](https://github.com/rubocop/rubocop-rails/issues/260): Change target of `Rails/ContentTag` from `content_tag` method to `tag` method. ([@tabuchi0919][])
14+
1115
## 2.11.3 (2021-07-11)
1216

1317
### Bug fixes

config/default.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,14 @@ Rails/BulkChangeTable:
181181
- db/migrate/*.rb
182182

183183
Rails/ContentTag:
184-
Description: 'Use `tag` instead of `content_tag`.'
184+
Description: 'Use `tag.something` instead of `tag(:something)`.'
185185
Reference:
186+
- 'https://github.com/rubocop/rubocop-rails/issues/260'
186187
- 'https://github.com/rails/rails/issues/25195'
187188
- 'https://api.rubyonrails.org/classes/ActionView/Helpers/TagHelper.html#method-i-content_tag'
188189
Enabled: true
189190
VersionAdded: '2.6'
191+
VersionChanged: '2.12'
190192

191193
Rails/CreateTableWithTimestamps:
192194
Description: >-

docs/modules/ROOT/pages/cops_rails.adoc

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -822,31 +822,32 @@ end
822822
| Yes
823823
| Yes
824824
| 2.6
825-
| -
825+
| 2.12
826826
|===
827827

828-
This cop checks that `tag` is used instead of `content_tag`
829-
because `content_tag` is legacy syntax.
828+
This cop checks legacy syntax usage of `tag`
830829

831-
NOTE: Allow `content_tag` when the first argument is a variable because
832-
`content_tag(name)` is simpler rather than `tag.public_send(name)`.
830+
NOTE: Allow `tag` when the first argument is a variable because
831+
`tag(name)` is simpler rather than `tag.public_send(name)`.
832+
And this cop will be renamed to something like `LegacyTag` in the future. (e.g. RuboCop Rails 2.0)
833833

834834
=== Examples
835835

836836
[source,ruby]
837837
----
838838
# bad
839-
content_tag(:p, 'Hello world!')
840-
content_tag(:br)
839+
tag(:p)
840+
tag(:br, class: 'classname')
841841
842842
# good
843-
tag.p('Hello world!')
844-
tag.br
845-
content_tag(name, 'Hello world!')
843+
tag.p
844+
tag.br(class: 'classname')
845+
tag(name, class: 'classname')
846846
----
847847

848848
=== References
849849

850+
* https://github.com/rubocop/rubocop-rails/issues/260
850851
* https://github.com/rails/rails/issues/25195
851852
* https://api.rubyonrails.org/classes/ActionView/Helpers/TagHelper.html#method-i-content_tag
852853

lib/rubocop/cop/rails/content_tag.rb

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,30 @@
33
module RuboCop
44
module Cop
55
module Rails
6-
# This cop checks that `tag` is used instead of `content_tag`
7-
# because `content_tag` is legacy syntax.
6+
# This cop checks legacy syntax usage of `tag`
87
#
9-
# NOTE: Allow `content_tag` when the first argument is a variable because
10-
# `content_tag(name)` is simpler rather than `tag.public_send(name)`.
8+
# NOTE: Allow `tag` when the first argument is a variable because
9+
# `tag(name)` is simpler rather than `tag.public_send(name)`.
10+
# And this cop will be renamed to something like `LegacyTag` in the future. (e.g. RuboCop Rails 2.0)
1111
#
1212
# @example
1313
# # bad
14-
# content_tag(:p, 'Hello world!')
15-
# content_tag(:br)
14+
# tag(:p)
15+
# tag(:br, class: 'classname')
1616
#
1717
# # good
18-
# tag.p('Hello world!')
19-
# tag.br
20-
# content_tag(name, 'Hello world!')
18+
# tag.p
19+
# tag.br(class: 'classname')
20+
# tag(name, class: 'classname')
2121
class ContentTag < Base
2222
include RangeHelp
2323
extend AutoCorrector
2424
extend TargetRailsVersion
2525

2626
minimum_target_rails_version 5.1
2727

28-
MSG = 'Use `tag` instead of `content_tag`.'
29-
RESTRICT_ON_SEND = %i[content_tag].freeze
28+
MSG = 'Use `tag.something` instead of `tag(:something)`.'
29+
RESTRICT_ON_SEND = %i[tag].freeze
3030

3131
def on_new_investigation
3232
@corrected_nodes = nil
@@ -53,26 +53,26 @@ def corrected_ancestor?(node)
5353
end
5454

5555
def allowed_argument?(argument)
56-
argument.variable? || argument.send_type? || argument.const_type? || argument.splat_type?
56+
argument.variable? ||
57+
argument.send_type? ||
58+
argument.const_type? ||
59+
argument.splat_type? ||
60+
allowed_name?(argument)
5761
end
5862

5963
def autocorrect(corrector, node)
60-
if method_name?(node.first_argument)
61-
range = correction_range(node)
64+
range = correction_range(node)
6265

63-
rest_args = node.arguments.drop(1)
64-
replacement = "tag.#{node.first_argument.value.to_s.underscore}(#{rest_args.map(&:source).join(', ')})"
66+
rest_args = node.arguments.drop(1)
67+
replacement = "tag.#{node.first_argument.value.to_s.underscore}(#{rest_args.map(&:source).join(', ')})"
6568

66-
corrector.replace(range, replacement)
67-
else
68-
corrector.replace(node.loc.selector, 'tag')
69-
end
69+
corrector.replace(range, replacement)
7070
end
7171

72-
def method_name?(node)
73-
return false unless node.str_type? || node.sym_type?
72+
def allowed_name?(argument)
73+
return false unless argument.str_type? || argument.sym_type?
7474

75-
/^[a-zA-Z_][a-zA-Z_\-0-9]*$/.match?(node.value)
75+
!/^[a-zA-Z\-][a-zA-Z\-0-9]*$/.match?(argument.value)
7676
end
7777

7878
def correction_range(node)

0 commit comments

Comments
 (0)