Class: RuboCop::Cop::Operandi::DeprecatedAccessors

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/operandi/rubocop/cop/operandi/deprecated_accessors.rb

Overview

Detects deprecated arguments and outputs accessor calls and suggests using arg and output instead.

This cop checks calls inside service classes that inherit from Operandi::Base or any configured base service classes.

Examples:

# bad
class User::Create < ApplicationService
  step :process

  private

  def process
    arguments[:name]
    outputs[:result]
  end
end

# good
class User::Create < ApplicationService
  step :process

  private

  def process
    arg[:name]
    output[:result]
  end
end

Constant Summary collapse

MSG_ARGUMENTS =
"Use `arg` instead of deprecated `arguments`."
MSG_OUTPUTS =
"Use `output` instead of deprecated `outputs`."
RESTRICT_ON_SEND =
[:arguments, :outputs].freeze
REPLACEMENTS =
{
  arguments: :arg,
  outputs: :output,
}.freeze
DEFAULT_BASE_CLASSES =
["ApplicationService"].freeze

Instance Method Summary collapse

Instance Method Details

#after_class(_node) ⇒ Object



60
61
62
# File 'lib/operandi/rubocop/cop/operandi/deprecated_accessors.rb', line 60

def after_class(_node)
  @in_service_class = false
end

#on_class(node) ⇒ Object



56
57
58
# File 'lib/operandi/rubocop/cop/operandi/deprecated_accessors.rb', line 56

def on_class(node)
  @in_service_class = service_class?(node)
end

#on_send(node) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/operandi/rubocop/cop/operandi/deprecated_accessors.rb', line 64

def on_send(node)
  return unless @in_service_class
  return unless RESTRICT_ON_SEND.include?(node.method_name)
  return if node.receiver && !self_receiver?(node)

  message = node.method_name == :arguments ? MSG_ARGUMENTS : MSG_OUTPUTS
  replacement = REPLACEMENTS[node.method_name]

  add_offense(node, message: message) do |corrector|
    if node.receiver
      corrector.replace(node, "self.#{replacement}")
    else
      corrector.replace(node, replacement.to_s)
    end
  end
end