forked from rubocop/rubocop-rails
/
content_tag.rb
68 lines (55 loc) · 1.87 KB
/
content_tag.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# frozen_string_literal: true
module RuboCop
module Cop
module Rails
# This cop checks that `tag` is used instead of `content_tag`
# because `content_tag` is legacy syntax.
#
# NOTE: Allow `content_tag` when the first argument is a variable because
# `content_tag(name)` is simpler rather than `tag.public_send(name)`.
#
# @example
# # bad
# content_tag(:p, 'Hello world!')
# content_tag(:br)
#
# # good
# tag.p('Hello world!')
# tag.br
# content_tag(name, 'Hello world!')
class ContentTag < Cop
include RangeHelp
extend TargetRailsVersion
minimum_target_rails_version 5.1
MSG = 'Use `tag` instead of `content_tag`.'
RESTRICT_ON_SEND = %i[content_tag].freeze
def on_send(node)
first_argument = node.first_argument
return unless first_argument
return if first_argument.variable? || first_argument.send_type? || first_argument.const_type?
add_offense(node)
end
def autocorrect(node)
lambda do |corrector|
if method_name?(node.first_argument)
range = correction_range(node)
rest_args = node.arguments.drop(1)
replacement = "tag.#{node.first_argument.value.to_s.underscore}(#{rest_args.map(&:source).join(', ')})"
corrector.replace(range, replacement)
else
corrector.replace(node.loc.selector, 'tag')
end
end
end
private
def method_name?(node)
return false unless node.str_type? || node.sym_type?
/^[a-zA-Z_][a-zA-Z_\-0-9]*$/.match?(node.value)
end
def correction_range(node)
range_between(node.loc.selector.begin_pos, node.loc.expression.end_pos)
end
end
end
end
end