Sha256: 17f7002a78409ddf2dbbbb316131d987ae60bbfe65914e656088fc6d1eea08c7

Contents?: true

Size: 1.28 KB

Versions: 45

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Metrics
      # This cop checks for methods with too many parameters.
      # The maximum number of parameters is configurable.
      # Keyword arguments can optionally be excluded from the total count.
      class ParameterLists < Cop
        include ConfigurableMax

        MSG = 'Avoid parameter lists longer than %<max>d parameters. ' \
              '[%<count>d/%<max>d]'

        def on_args(node)
          count = args_count(node)
          return unless count > max_params

          return if argument_to_lambda_or_proc?(node)

          add_offense(node) do
            self.max = count
          end
        end

        private

        def_node_matcher :argument_to_lambda_or_proc?, <<~PATTERN
          ^lambda_or_proc?
        PATTERN

        def message(node)
          format(MSG, max: max_params, count: args_count(node))
        end

        def args_count(node)
          if count_keyword_args?
            node.children.size
          else
            node.children.count { |a| !%i[kwoptarg kwarg].include?(a.type) }
          end
        end

        def max_params
          cop_config['Max']
        end

        def count_keyword_args?
          cop_config['CountKeywordArgs']
        end
      end
    end
  end
end

Version data entries

45 entries across 34 versions & 5 rubygems

Version Path
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/metrics/parameter_lists.rb
rubocop-0.88.0 lib/rubocop/cop/metrics/parameter_lists.rb
rubocop-0.87.1 lib/rubocop/cop/metrics/parameter_lists.rb
rubocop-0.87.0 lib/rubocop/cop/metrics/parameter_lists.rb
rubocop-0.86.0 lib/rubocop/cop/metrics/parameter_lists.rb
files.com-1.0.1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.85.1/lib/rubocop/cop/metrics/parameter_lists.rb
rbhint-0.85.1.rc2 lib/rubocop/cop/metrics/parameter_lists.rb
rbhint-0.85.1.rc1 lib/rubocop/cop/metrics/parameter_lists.rb
rubocop-0.85.1 lib/rubocop/cop/metrics/parameter_lists.rb
rbhint-0.8.5.rc1 lib/rubocop/cop/metrics/parameter_lists.rb
rubocop-0.85.0 lib/rubocop/cop/metrics/parameter_lists.rb
rubocop-0.84.0 lib/rubocop/cop/metrics/parameter_lists.rb
rubocop-0.83.0 lib/rubocop/cop/metrics/parameter_lists.rb
rubocop-0.82.0 lib/rubocop/cop/metrics/parameter_lists.rb
rubocop-0.81.0 lib/rubocop/cop/metrics/parameter_lists.rb
rubocop-0.80.1 lib/rubocop/cop/metrics/parameter_lists.rb
rubocop-0.80.0 lib/rubocop/cop/metrics/parameter_lists.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/rubocop-0.79.0/lib/rubocop/cop/metrics/parameter_lists.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/metrics/parameter_lists.rb
rubocop-0.79.0 lib/rubocop/cop/metrics/parameter_lists.rb