Sha256: 364772a9fe5dd4af811105a26e103a1418137bf0d846205eeae3f291b3c9294f

Contents?: true

Size: 1.34 KB

Versions: 12

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    # This module handles measurement and reporting of complexity in methods.
    module MethodComplexity
      include ConfigurableMax
      extend NodePattern::Macros

      def on_def(node)
        check_complexity(node, node.method_name)
      end
      alias on_defs on_def

      def on_block(node)
        define_method?(node) do |name|
          check_complexity(node, name)
        end
      end

      private

      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']
        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) do
          self.max = complexity.ceil
        end
      end

      def complexity(body)
        body.each_node(*self.class::COUNTED_NODES).reduce(1) do |score, n|
          score + complexity_score_for(n)
        end
      end
    end
  end
end

Version data entries

12 entries across 10 versions & 3 rubygems

Version Path
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/mixin/method_complexity.rb
rubocop-0.80.1 lib/rubocop/cop/mixin/method_complexity.rb
rubocop-0.80.0 lib/rubocop/cop/mixin/method_complexity.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/rubocop-0.79.0/lib/rubocop/cop/mixin/method_complexity.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/mixin/method_complexity.rb
rubocop-0.79.0 lib/rubocop/cop/mixin/method_complexity.rb
rubocop-0.78.0 lib/rubocop/cop/mixin/method_complexity.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.76.0/lib/rubocop/cop/mixin/method_complexity.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.77.0/lib/rubocop/cop/mixin/method_complexity.rb
rubocop-0.77.0 lib/rubocop/cop/mixin/method_complexity.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/rubocop-0.76.0/lib/rubocop/cop/mixin/method_complexity.rb
rubocop-0.76.0 lib/rubocop/cop/mixin/method_complexity.rb