class RuboCop::Yast::Builtins::Y2log
generic class for handling logging builtins
Constants
- ASGN_TYPES
- LOGGING_REPLACEMENENTS
- TYPES_WITHOUT_ARG
Attributes
added_includes[R]
Public Class Methods
new()
click to toggle source
# File lib/rubocop/yast/builtins/y2log.rb, line 37 def initialize @added_includes = [] end
Public Instance Methods
correction(node)
click to toggle source
# File lib/rubocop/yast/builtins/y2log.rb, line 41 def correction(node) _receiver, _method_name, format, *params = *node # we can replace only standard logging, not backtraces like # Builtins.y2milestone(-1, "foo") or unsafe code like # fmt = "%1: %2"; Builtins.y2milestone(fmt, foo, bar) if !((TYPES_WITHOUT_ARG.include?(format.type) && params.empty?) || (format.str_type?)) || ignore_node?(node) raise RuboCop::Cop::CorrectionNotPossible end correction_lambda(node) end
Private Instance Methods
add_include_to_node(node, corrector)
click to toggle source
add the Yast::LOgger statement include to this node
# File lib/rubocop/yast/builtins/y2log.rb, line 103 def add_include_to_node(node, corrector) if node.class_type? || node.module_type? # indent the include statement corrector.insert_after(class_logger_pos(node), logger_include_code(node.loc.keyword.column)) else # otherwise put it at the top corrector.insert_before(node.loc.expression, logger_include_code) end end
add_missing_logger(node, corrector)
click to toggle source
Add Yast::Logger include somewhere up in the tree
# File lib/rubocop/yast/builtins/y2log.rb, line 89 def add_missing_logger(node, corrector) target_node = parent_node_type(node, [:class, :module]) target_node = target_node.children.first if target_node.begin_type? # already added or already present return if added_includes.include?(target_node) || logger_included?(target_node) add_include_to_node(target_node, corrector) added_includes << target_node end
class_logger_pos(node)
click to toggle source
insert after “class Foo” or “class Foo < Bar” statement
# File lib/rubocop/yast/builtins/y2log.rb, line 115 def class_logger_pos(node) node.loc.operator ? node.children[1].loc.expression : node.loc.name end
correction_lambda(node)
click to toggle source
# File lib/rubocop/yast/builtins/y2log.rb, line 57 def correction_lambda(node) lambda do |corrector| add_missing_logger(node, corrector) corrector.replace(node.loc.expression, replacement(node)) end end
ignore_node?(node)
click to toggle source
# File lib/rubocop/yast/builtins/y2log.rb, line 78 def ignore_node?(node) target_node = parent_node_type(node, [:class, :module]) log_descendant = target_node.each_descendant.find do |n| n && ASGN_TYPES.include?(n.type) && n.loc.name.source == "log" end log_descendant end
logger_include_code(indent = nil)
click to toggle source
format the include statement
# File lib/rubocop/yast/builtins/y2log.rb, line 129 def logger_include_code(indent = nil) code = "include Yast::Logger\n" return code unless indent indent_str = "\n" + " " * (indent + 2) indent_str + code end
logger_included?(node)
click to toggle source
simple check for already present include
# File lib/rubocop/yast/builtins/y2log.rb, line 120 def logger_included?(node) return false if node.nil? || node.loc.nil? source = node.loc.expression.source # TODO: better check for the include call, this is a simple "grep"... !source.match(/include\s+(Yast::|)Logger/).nil? end
replacement(node)
click to toggle source
# File lib/rubocop/yast/builtins/y2log.rb, line 64 def replacement(node) _receiver, method_name, format, *params = *node method = LOGGING_REPLACEMENENTS[method_name] src_format = format.loc.expression.source if format.str_type? src_params = params.map { |p| p.loc.expression.source } "log.#{method} #{interpolate(src_format, src_params)}" else "log.#{method} #{src_format}" end end