Class: RuboCop::Cop::Rails::ToSWithArgument

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

Overview

Identifies passing any argument to ‘#to_s`.

Examples:


# bad
obj.to_s(:delimited)

# good
obj.to_formatted_s(:delimited)

Constant Summary collapse

EXTENDED_FORMAT_TYPES =

These types are defined by the following files in ActiveSupport:

lib/active_support/core_ext/array/conversions.rb
lib/active_support/core_ext/date/conversions.rb
lib/active_support/core_ext/date_time/conversions.rb
lib/active_support/core_ext/numeric/conversions.rb
lib/active_support/core_ext/range/conversions.rb
lib/active_support/core_ext/time/conversions.rb
lib/active_support/time_with_zone.rb
Set.new(
  %i[
    currency
    db
    delimited
    human
    human_size
    inspect
    iso8601
    long
    long_ordinal
    nsec
    number
    percentage
    phone
    rfc822
    rounded
    short
    time
    usec
  ]
)
MSG =
'Use `to_formatted_s` instead.'
RESTRICT_ON_SEND =
%i[to_s].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_send(node) ⇒ Object Also known as: on_csend



61
62
63
64
65
66
67
# File 'lib/rubocop/cop/rails/to_s_with_argument.rb', line 61

def on_send(node)
  return unless rails_extended_to_s?(node)

  add_offense(node.loc.selector) do |corrector|
    corrector.replace(node.loc.selector, 'to_formatted_s')
  end
end