Class: RuboCop::Cop::Performance::FluentdPluginLogStringInterpolation

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/performance/plugin_log_string_interpolation.rb

Overview

Examples:

FluentdPluginLogStringInterpolation: (default)


# bad
log.debug "#{EVALUATE_LONG_MESSAGE}"

# good
log.debug { "#{EVALUATE_LONG_MESSAGE}" }

Constant Summary collapse

RESTRICT_ON_SEND =
%i[trace debug info warn error fatal].freeze

Instance Method Summary collapse

Instance Method Details

#log_message_string_interpolation?(node) ⇒ Object



19
20
21
# File 'lib/rubocop/cop/performance/plugin_log_string_interpolation.rb', line 19

def_node_matcher :log_message_string_interpolation?, <<~PATTERN
  (send (send nil? :log) _ (dstr ...))
PATTERN

#on_send(node) ⇒ Object Also known as: on_csend



23
24
25
26
27
28
29
30
31
32
# File 'lib/rubocop/cop/performance/plugin_log_string_interpolation.rb', line 23

def on_send(node)
  # match log.debug("#{...}") and so on
  return unless log_message_string_interpolation?(node)

  method = node.children[1].to_s
  msg = %Q(Use log.#{method} { "..." } instead of log.#{method}("..."))
  add_offense(node, message: msg)
  # mark do not match on_send further more
  ignore_node(node)
end