Class: RuboCop::Cop::GraphQL::ResolverMethodLength

Inherits:
Base
  • Object
show all
Includes:
CodeLength
Defined in:
lib/rubocop/cop/graphql/resolver_method_length.rb

Overview

Checks if the length of a resolver method exceeds some maximum value. Comment lines can optionally be ignored.

The maximum allowed length is configurable using the Max option.

Constant Summary collapse

MSG =
"ResolverMethod has too many lines. [%<total>d/%<max>d]"

Instance Method Summary collapse

Instance Method Details

#field_definition(node) ⇒ Object



16
17
18
# File 'lib/rubocop/cop/graphql/resolver_method_length.rb', line 16

def_node_matcher :field_definition, <<~PATTERN
  (send nil? :field (sym $...) ...)
PATTERN

#on_def(node) ⇒ Object Also known as: on_defs



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rubocop/cop/graphql/resolver_method_length.rb', line 20

def on_def(node)
  excluded_methods = cop_config["ExcludedMethods"]
  return if excluded_methods.include?(String(node.method_name))

  if field_is_defined?(node)
    return if node.line_count <= max_length

    calculator = build_code_length_calculator(node)
    length = calculator.calculate
    return if length <= max_length

    add_offense(node, message: message(length))
  end
end