Class: RuboCop::Cop::Operandi::DslOrder

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

Overview

Enforces a consistent order for DSL declarations in service classes.

The expected order is: configargstepoutput

Examples:

# bad
class MyService < ApplicationService
  step :process
  arg :name, type: String
  output :result, type: Hash
  config raise_on_error: true
end

# good
class MyService < ApplicationService
  config raise_on_error: true

  arg :name, type: String

  step :process

  output :result, type: Hash
end

Constant Summary collapse

MSG =
"`%<current>s` should come before `%<previous>s`. " \
"Expected order: config → arg → step → output."
DSL_METHODS =
[:config, :arg, :step, :output].freeze
DSL_ORDER =
{ config: 0, arg: 1, step: 2, output: 3 }.freeze

Instance Method Summary collapse

Instance Method Details

#after_class(_node) ⇒ Object



54
55
56
57
58
# File 'lib/operandi/rubocop/cop/operandi/dsl_order.rb', line 54

def after_class(_node)
  return unless @dsl_calls&.any?

  check_order
end

#on_class(node) ⇒ Object



42
43
44
45
# File 'lib/operandi/rubocop/cop/operandi/dsl_order.rb', line 42

def on_class(node)
  @dsl_calls = []
  @class_node = node
end

#on_send(node) ⇒ Object



47
48
49
50
51
52
# File 'lib/operandi/rubocop/cop/operandi/dsl_order.rb', line 47

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

  @dsl_calls ||= []
  @dsl_calls << { method: node.method_name, node: node }
end