Class: RuboCop::Cop::Legion::HelperMigration::DirectCache

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

Overview

Detects direct calls to ‘Legion::Cache` methods and suggests using the `cache_*` helpers instead.

Examples:

# bad
Legion::Cache.get('key')
Legion::Cache.set('key', value)
Legion::Cache.delete('key')
Legion::Cache.fetch('key') { compute }
Legion::Cache.connected?

# good
cache_get('key')
cache_set('key', value)
cache_delete('key')
cache_fetch('key') { compute }
cache_connected?

Constant Summary collapse

MSG =
'Use `%<helper>s` instead of `Legion::Cache.%<method>s`. ' \
'Include the appropriate cache helper mixin.'
RESTRICT_ON_SEND =
%i[get set delete fetch connected?].freeze
HELPER_MAP =
{
  get: 'cache_get',
  set: 'cache_set',
  delete: 'cache_delete',
  fetch: 'cache_fetch',
  connected?: 'cache_connected?'
}.freeze

Instance Method Summary collapse

Instance Method Details

#legion_cache_call?(node) ⇒ Object



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

def_node_matcher :legion_cache_call?, <<~PATTERN
  (send (const (const nil? :Legion) :Cache) {:get :set :delete :fetch :connected?} ...)
PATTERN

#on_send(node) ⇒ Object



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

def on_send(node)
  return unless legion_cache_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|
    if node.arguments.empty?
      corrector.replace(node, helper)
    else
      args_source = node.arguments.map(&:source).join(', ')
      corrector.replace(node, "#{helper}(#{args_source})")
    end
  end
end