Class: RuboCop::Cop::Legion::HelperMigration::DirectLogging

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

Overview

Detects direct calls to ‘Legion::Logging.*` and suggests using the `log.*` helper instead (via `Helpers::Lex` or `Legion::Logging::Helper`).

Examples:

# bad
Legion::Logging.info('hello')
Legion::Logging.warn(msg)

# good
log.info('hello')
log.warn(msg)

Constant Summary collapse

MSG =
'Use `log.%<method>s(msg)` instead of `Legion::Logging.%<method>s`. ' \
'Include `Helpers::Lex` or `Legion::Logging::Helper`.'
RESTRICT_ON_SEND =
%i[info warn error debug fatal].freeze

Instance Method Summary collapse

Instance Method Details

#legion_logging_call?(node) ⇒ Object



27
28
29
# File 'lib/rubocop/cop/legion/helper_migration/direct_logging.rb', line 27

def_node_matcher :legion_logging_call?, <<~PATTERN
  (send (const (const nil? :Legion) :Logging) {:info :warn :error :debug :fatal} ...)
PATTERN

#on_send(node) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rubocop/cop/legion/helper_migration/direct_logging.rb', line 31

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

  method_name = node.method_name
  message = format(MSG, method: method_name)

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