Class: RuboCop::Cop::Rails::ContentTag

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector, TargetRailsVersion
Includes:
RangeHelp
Defined in:
lib/rubocop/cop/rails/content_tag.rb

Overview

Checks legacy syntax usage of ‘tag`

NOTE: Allow ‘tag` when the first argument is a variable because `tag(name)` is simpler rather than `tag.public_send(name)`. And this cop will be renamed to something like `LegacyTag` in the future. (e.g. RuboCop Rails 3.0)

Examples:

# bad
tag(:p)
tag(:br, class: 'classname')

# good
tag.p
tag.br(class: 'classname')
tag(name, class: 'classname')

Constant Summary collapse

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

Constants included from TargetRailsVersion

TargetRailsVersion::TARGET_GEM_NAME, TargetRailsVersion::USES_REQUIRES_GEM_API

Instance Method Summary collapse

Methods included from TargetRailsVersion

minimum_target_rails_version, support_target_rails_version?

Instance Method Details

#on_new_investigationObject



31
32
33
# File 'lib/rubocop/cop/rails/content_tag.rb', line 31

def on_new_investigation
  @corrected_nodes = nil
end

#on_send(node) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rubocop/cop/rails/content_tag.rb', line 35

def on_send(node)
  return unless node.receiver.nil?
  return if node.arguments.count >= 3

  first_argument = node.first_argument
  return if !first_argument || allowed_argument?(first_argument) || corrected_ancestor?(node)

  preferred_method = node.first_argument.value.to_s.underscore
  message = format(MSG, preferred_method: preferred_method, current_argument: first_argument.source)

  register_offense(node, message, preferred_method)
end