Class: RuboCop::Cop::Lint::FluentdPluginLogScope

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
IgnoredNode
Defined in:
lib/rubocop/cop/lint/plugin_log_scope.rb

Overview

Examples:

Lint/FluentdPluginLogLevel: (default)


# bad
$log.trace("...")
$log.debug("...")
$log.info("...")
$log.warn("...")
$log.error("...")
$log.fatal("...")

# good
log.trace("...")
log.debug("...")
log.info("...")
log.warn("...")
log.error("...")
log.fatal("...")

Lint/FluentdPluginLogLevel: block


# bad
$log.trace("...")
$log.debug("...")

# good
$log.trace { "..." }
$log.debug { "..." }

Constant Summary collapse

MSG =
'Use plugin scope `log` instead of global scope `$log`.'
RESTRICT_ON_BLOCK =

Detect only supported log level RESTRICT_ON_SEND = %i[trace debug info warn error fatal].freeze

i[trace debug info warn error fatal].freeze
LOG_LEVELS =
{'trace' => 0,
'debug' => 1,
'info' => 2,
'warn' => 3,
'error' => 4,
'fatal' => 5}

Instance Method Summary collapse

Instance Method Details

#block_log_level_method(node) ⇒ Object



144
145
146
147
# File 'lib/rubocop/cop/lint/plugin_log_scope.rb', line 144

def block_log_level_method(node)
  send_node = node.children.first
  send_node.children.last
end

#global_log_method?(node) ⇒ Object



53
54
55
# File 'lib/rubocop/cop/lint/plugin_log_scope.rb', line 53

def_node_matcher :global_reciever_method?, "(send gvar $_ $(...))\n"

#global_log_reciever?(node) ⇒ Boolean



154
155
156
# File 'lib/rubocop/cop/lint/plugin_log_scope.rb', line 154

def global_log_reciever?(node)
  node.name == :$log
end

#global_reciever_block_method?(node) ⇒ Object



58
59
60
# File 'lib/rubocop/cop/lint/plugin_log_scope.rb', line 58

def_node_matcher :global_reciever_block_method?, "(block (send gvar $_) _ $(...))\n"

#local_log_method?(node) ⇒ Object



63
64
65
# File 'lib/rubocop/cop/lint/plugin_log_scope.rb', line 63

def_node_matcher :local_log_method?, "(send (send nil? :log) $_ $(...))\n"

#log_level_method(node) ⇒ Object



140
141
142
# File 'lib/rubocop/cop/lint/plugin_log_scope.rb', line 140

def log_level_method(node)
  node.children[1]
end

#on_block(node) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/rubocop/cop/lint/plugin_log_scope.rb', line 123

def on_block(node)
  expression = global_reciever_block_method?(node)
  return unless expression

  # $log.method { ... }
  send_node = node.children.first
  if send_global_log_node?(send_node)
    add_offense(node) do |corrector|
      source_code = "log.#{block_log_level_method(node)}"
      # $log.xxx => log.xxx
      corrector.replace(node.children.first, source_code)
    end
    # mark do not match on_send further more
    ignore_node(node)
  end
end

#on_send(node) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/rubocop/cop/lint/plugin_log_scope.rb', line 67

def on_send(node)
  return if part_of_ignored_node?(node)
  global_expression = global_reciever_method?(node)
  local_expression = local_log_method?(node)
  return unless global_expression or local_expression
  if global_expression and send_global_log_node?(node)
    expression = global_expression
    method = expression.first
    return unless i[trace debug info warn error fatal].freeze.include?(method)
    # $log.method(...)
    message = 'Use plugin scope `log` instead of global scope `$log`.'
    add_offense(node, message: MSG) do |corrector|
      literal = expression.last
      assume_level = cop_config['AssumeConfigLogLevel'] || 'info'
      threshould = LOG_LEVELS[assume_level]
      if LOG_LEVELS[method.to_s] >= threshould
        # no need to delay evaluation
        source_code = "log.#{method} #{literal.source}"
      else
        source_code = "log.#{method} { #{literal.source} }"
      end
      # $log.xxx => log.xxx
      corrector.replace(node, source_code)
    end
  elsif local_expression and send_local_log_node?(node)
    # log.method "#{expansion}"
    expression = local_expression
    method = expression.first
    return unless i[trace debug info warn error fatal].freeze.include?(method)

    assume_level = cop_config['AssumeConfigLogLevel'] || 'info'
    threshould = LOG_LEVELS[assume_level]

    if LOG_LEVELS[method.to_s] >= threshould
      # no need to apply offense because surely log will be emitted
      return
    end
    message = "Use block not to evaluate too long message"
    method = expression.first
    assume_level = cop_config['AssumeConfigLogLevel'] || 'info'
    threshould = LOG_LEVELS[assume_level]

    if LOG_LEVELS[method.to_s] >= threshould
      # no need to apply offense because surely log will be emitted
      return
    end
    add_offense(node, message: message) do |corrector|
      method = expression.first
      literal = expression.last
      source_code = "log.#{method} { #{literal.source} }"
      # $log.xxx => log.xxx
      corrector.replace(node, source_code)
    end
  end
end

#send_global_log_node?(node) ⇒ Boolean



149
150
151
152
# File 'lib/rubocop/cop/lint/plugin_log_scope.rb', line 149

def send_global_log_node?(node)
  node.class == RuboCop::AST::SendNode and
    global_log_reciever?(node.children.first)
end

#send_local_log_node?(node) ⇒ Boolean



158
159
160
161
# File 'lib/rubocop/cop/lint/plugin_log_scope.rb', line 158

def send_local_log_node?(node)
  node.class == RuboCop::AST::SendNode and
    node.children.last.class == RuboCop::AST::DstrNode
end