Class: RuboCop::Cop::Legion::HelperMigration::DirectJson

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

Overview

Detects direct calls to ‘Legion::JSON.load` and `Legion::JSON.dump` and suggests using the `json_load` / `json_dump` helpers instead.

Examples:

# bad
Legion::JSON.load(str)
Legion::JSON.dump(obj)

# good
json_load(str)
json_dump(obj)

Constant Summary collapse

MSG =
'Use `%<helper>s` instead of `Legion::JSON.%<method>s`. ' \
'Include the appropriate JSON helper mixin.'
RESTRICT_ON_SEND =
%i[load dump].freeze
HELPER_MAP =
{
  load: 'json_load',
  dump: 'json_dump'
}.freeze

Instance Method Summary collapse

Instance Method Details

#legion_json_call?(node) ⇒ Object



32
33
34
# File 'lib/rubocop/cop/legion/helper_migration/direct_json.rb', line 32

def_node_matcher :legion_json_call?, <<~PATTERN
  (send (const (const nil? :Legion) :JSON) {:load :dump} ...)
PATTERN

#on_send(node) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rubocop/cop/legion/helper_migration/direct_json.rb', line 36

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

  method_name = node.method_name
  helper = HELPER_MAP[method_name]
  message = format(MSG, helper: helper, method: method_name)

  add_offense(node, message: message) do |corrector|
    args_source = node.arguments.map(&:source).join(', ')
    corrector.replace(node, "#{helper}(#{args_source})")
  end
end