Sha256: dc5f5bb531fc5c11148b223ba7aa6f1f0859a01ae6bf229cff54ede68b35af9c

Contents?: true

Size: 1.46 KB

Versions: 15

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

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

      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

      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

15 entries across 15 versions & 3 rubygems

Version Path
rubocop-0.88.0 lib/rubocop/cop/mixin/method_complexity.rb
rbhint-0.87.1.rc1 lib/rubocop/cop/mixin/method_complexity.rb
rubocop-0.87.1 lib/rubocop/cop/mixin/method_complexity.rb
rubocop-0.87.0 lib/rubocop/cop/mixin/method_complexity.rb
rubocop-0.86.0 lib/rubocop/cop/mixin/method_complexity.rb
files.com-1.0.1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.85.1/lib/rubocop/cop/mixin/method_complexity.rb
rbhint-0.85.1.rc2 lib/rubocop/cop/mixin/method_complexity.rb
rbhint-0.85.1.rc1 lib/rubocop/cop/mixin/method_complexity.rb
rubocop-0.85.1 lib/rubocop/cop/mixin/method_complexity.rb
rbhint-0.8.5.rc1 lib/rubocop/cop/mixin/method_complexity.rb
rubocop-0.85.0 lib/rubocop/cop/mixin/method_complexity.rb
rubocop-0.84.0 lib/rubocop/cop/mixin/method_complexity.rb
rubocop-0.83.0 lib/rubocop/cop/mixin/method_complexity.rb
rubocop-0.82.0 lib/rubocop/cop/mixin/method_complexity.rb
rubocop-0.81.0 lib/rubocop/cop/mixin/method_complexity.rb