Class: RuboCop::Cop::Legion::ConstantSafety::BareDataDefine

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/legion/constant_safety/bare_data_define.rb

Overview

Detects bare ‘Data.define` inside `module Legion` namespaces where it resolves to `Legion::Data.define` instead of the stdlib `Data.define`.

Examples:

# bad (inside module Legion)
module Legion
  Point = Data.define(:x, :y)
end

# good
module Legion
  Point = ::Data.define(:x, :y)
end

Constant Summary collapse

MSG =
'Inside `module Legion`, bare `Data.define` resolves to `Legion::Data.define`. ' \
'Use `::Data.define`.'
RESTRICT_ON_SEND =
%i[define].freeze

Instance Method Summary collapse

Instance Method Details

#bare_data_define?(node) ⇒ Object



29
30
31
# File 'lib/rubocop/cop/legion/constant_safety/bare_data_define.rb', line 29

def_node_matcher :bare_data_define?, <<~PATTERN
  (send (const nil? :Data) :define ...)
PATTERN

#on_send(node) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/rubocop/cop/legion/constant_safety/bare_data_define.rb', line 33

def on_send(node)
  return unless bare_data_define?(node)
  return unless inside_legion_namespace?(node)

  receiver = node.receiver
  add_offense(receiver) do |corrector|
    corrector.replace(receiver.source_range, '::Data')
  end
end