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` methods and suggests using the `json_*` helpers instead.

Examples:

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

# good
json_load(str)
json_dump(obj)
json_parse(str)
json_generate(obj)
json_pretty_generate(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 parse generate pretty_generate].freeze
HELPER_MAP =
{
  load: 'json_load',
  dump: 'json_dump',
  parse: 'json_parse',
  generate: 'json_generate',
  pretty_generate: 'json_pretty_generate'
}.freeze

Instance Method Summary collapse

Instance Method Details

#legion_json_call?(node) ⇒ Object



41
42
43
# File 'lib/rubocop/cop/legion/helper_migration/direct_json.rb', line 41

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

#on_send(node) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rubocop/cop/legion/helper_migration/direct_json.rb', line 45

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