Sha256: e995283c41a491db8ca2a39f47847b91f6dd4132ab01e152b35a8a5a4aab80a4

Contents?: true

Size: 1.33 KB

Versions: 3

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

module CustomCops
  module NxtCore
    module Rails
      # The NxtCore logger requires an include in most cases. Ensures it is present.
      #
      # @safety
      #   This cop's autocorrection is unsafe because
      #   `logger` may refer to an instance variable or other method of the class.
      class MissingLoggerInclude < RuboCop::Cop::Base
        extend RuboCop::Cop::AutoCorrector

        RESTRICT_ON_SEND = [:logger].freeze

        MSG = 'Missing `include Nxt::Loggable` for a class which uses the logger.'

        # NodePattern docs: https://github.com/rubocop/rubocop-ast/blob/master/docs/modules/ROOT/pages/node_pattern.adoc
        def_node_matcher :includes_nxt_loggable?, <<~PATTERN
          (begin <(send nil? :include (const (const nil? :Nxt) :Loggable)) ...>)
        PATTERN

        def on_send(node)
          return unless node.receiver.nil?

          class_node = node.ancestors.find(&:class_type?)
          return unless class_node

          class_definition_node = class_node.each_child_node(:begin).first
          return if includes_nxt_loggable?(class_definition_node)

          add_offense(class_node, message: MSG) do |corrector|
            corrector.insert_before(class_definition_node.child_nodes.first, "include Nxt::Loggable\n")
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
nxt_cop-2.3.0 custom_cops/nxt_core/rails/missing_logger_include.rb
nxt_cop-2.2.0 custom_cops/nxt_core/rails/missing_logger_include.rb
nxt_cop-2.1.0 custom_cops/nxt_core/rails/missing_logger_include.rb