Class: RuboCop::Cop::Legion::ConstantSafety::BareJson

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

Overview

Detects bare ‘JSON` method calls inside `module Legion` namespaces where `JSON` resolves to `Legion::JSON` instead of the stdlib.

Examples:

# bad (inside module Legion)
module Legion
  JSON.parse(raw)
end

# good
module Legion
  ::JSON.parse(raw)
end

Constant Summary collapse

MSG =
'Inside `module Legion`, bare `JSON` resolves to `Legion::JSON`. ' \
'Use `::JSON` for stdlib.'
RESTRICT_ON_SEND =
%i[parse generate pretty_generate dump load fast_generate].freeze

Instance Method Summary collapse

Instance Method Details

#bare_json_send?(node) ⇒ Object



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

def_node_matcher :bare_json_send?, <<~PATTERN
  (send (const nil? :JSON) _ ...)
PATTERN

#on_send(node) ⇒ Object



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

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

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