Class: RuboCop::Cop::Rails::ToFormattedS

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

Overview

Checks for consistent uses of ‘to_fs` or `to_formatted_s`, depending on the cop’s configuration.

Examples:

EnforcedStyle: to_fs (default)


# bad
time.to_formatted_s(:db)

# good
time.to_fs(:db)

EnforcedStyle: to_formatted_s


# bad
time.to_fs(:db)

# good
time.to_formatted_s(:db)

Constant Summary collapse

MSG =
'Use `%<prefer>s` instead.'
RESTRICT_ON_SEND =
%i[to_formatted_s to_fs].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



35
36
37
38
39
40
41
# File 'lib/rubocop/cop/rails/to_formatted_s.rb', line 35

def on_send(node)
  return if node.method?(style)

  add_offense(node.loc.selector, message: format(MSG, prefer: style)) do |corrector|
    corrector.replace(node.loc.selector, style)
  end
end