Sha256: 6754c5ec263766e4be0dcc01097653a2068b9c610b5cbf69b43ae9de45b12fd2

Contents?: true

Size: 1.86 KB

Versions: 146

Compression:

Stored size: 1.86 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 AllowedMethods
      include AllowedPattern
      include Metrics::Utils::RepeatedCsendDiscount
      extend NodePattern::Macros
      extend ExcludeLimit

      exclude_limit 'Max'

      def on_def(node)
        return if allowed_method?(node.method_name) || matches_allowed_pattern?(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 allowed_method?(name) || matches_allowed_pattern?(name)

          check_complexity(node, name)
        end
      end

      alias on_numblock on_block

      private

      # @!method define_method?(node)
      def_node_matcher :define_method?, <<~PATTERN
        ({block numblock}
         (send nil? :define_method ({sym str} $_)) _ _)
      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)
        score = 1
        body.each_node(:lvasgn, *self.class::COUNTED_NODES) do |node|
          if node.lvasgn_type?
            reset_on_lvasgn(node)
          else
            score += complexity_score_for(node)
          end
        end
        score
      end
    end
  end
end

Version data entries

146 entries across 145 versions & 14 rubygems

Version Path
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/mixin/method_complexity.rb
bison-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.62.1/lib/rubocop/cop/mixin/method_complexity.rb
rubocop-1.62.1 lib/rubocop/cop/mixin/method_complexity.rb
rubocop-1.62.0 lib/rubocop/cop/mixin/method_complexity.rb
rubocop-1.61.0 lib/rubocop/cop/mixin/method_complexity.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.2/lib/rubocop/cop/mixin/method_complexity.rb
rubocop-1.60.2 lib/rubocop/cop/mixin/method_complexity.rb
study_line-0.2.7 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/mixin/method_complexity.rb
study_line-0.2.6 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/mixin/method_complexity.rb
study_line-0.2.5 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/mixin/method_complexity.rb
study_line-0.2.4 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/mixin/method_complexity.rb
study_line-0.2.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/mixin/method_complexity.rb
study_line-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/mixin/method_complexity.rb
rubocop-1.60.1 lib/rubocop/cop/mixin/method_complexity.rb
study_line-0.2.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/mixin/method_complexity.rb
study_line-0.2.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/mixin/method_complexity.rb
rubocop-1.60.0 lib/rubocop/cop/mixin/method_complexity.rb
harbr-0.2.10 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/mixin/method_complexity.rb
harbr-0.2.9 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/mixin/method_complexity.rb
harbr-0.2.8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/mixin/method_complexity.rb