Sha256: 3c6b279e5e8240c8a2391a613c7d436cb69c4f325e4032daa2775cd0d8bf51ef
Contents?: true
Size: 1.92 KB
Versions: 46
Compression:
Stored size: 1.92 KB
Contents
# frozen_string_literal: true module RuboCop module Cop # @api private # # This module handles measurement and reporting of complexity in methods. module MethodComplexity include IgnoredMethods include Metrics::Utils::RepeatedCsendDiscount extend NodePattern::Macros extend ExcludeLimit exclude_limit 'Max' # Ensure cops that include `MethodComplexity` have the config # `attr_accessor`s that `ignored_method?` needs. def self.included(base) base.extend(IgnoredMethods::Config) end def on_def(node) return if ignored_method?(node.method_name) check_complexity(node, node.method_name) end alias on_defs on_def def on_block(node) define_method?(node) do |name| return if ignored_method?(name) check_complexity(node, name) end end private # @!method define_method?(node) def_node_matcher :define_method?, <<~PATTERN (block (send nil? :define_method ({sym str} $_)) args _) PATTERN def check_complexity(node, method_name) # Accepts empty methods always. return unless node.body max = cop_config['Max'] reset_repeated_csend complexity, abc_vector = complexity(node.body) return unless complexity > max msg = format(self.class::MSG, method: method_name, complexity: complexity, abc_vector: abc_vector, max: max) add_offense(node, message: msg) { self.max = complexity.ceil } end def complexity(body) body.each_node(:lvasgn, *self.class::COUNTED_NODES).reduce(1) do |score, node| if node.lvasgn_type? reset_on_lvasgn(node) next score end score + complexity_score_for(node) end end end end end
Version data entries
46 entries across 44 versions & 6 rubygems