Class: RuboCop::Cop::GraphQL::UnnecessaryFieldAlias

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
GraphQL::NodePattern
Defined in:
lib/rubocop/cop/graphql/unnecessary_field_alias.rb

Overview

This cop prevents defining an unnecessary alias, method, or resolver_method.

Examples:

# good

field :name, String, "Name of the user", null: true, alias: :real_name
field :name, String, "Name of the user", null: true, method: :real_name
field :name, String, "Name of the user", null: true, resolver_method: :real_name
field :name, String, "Name of the user", null: true, hash_key: :real_name

# bad

field :name, "Name of the user" String, null: true, alias: :name
field :name, String, "Name of the user", null: true, method: :name
field :name, String, "Name of the user", null: true, resolver_method: :name
field :name, String, "Name of the user", null: true, hash_key: :name

Constant Summary collapse

MSG =
"Unnecessary :%<kwarg>s configured"
RESTRICT_ON_SEND =
%i[field].freeze

Instance Method Summary collapse

Methods included from GraphQL::NodePattern

#argument?, #field?, #field_definition?, #field_definition_with_body?

Instance Method Details

#on_send(node) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rubocop/cop/graphql/unnecessary_field_alias.rb', line 30

def on_send(node)
  return unless field_definition?(node)

  field = RuboCop::GraphQL::Field.new(node)

  if (unnecessary_kwarg = validate_kwargs(field))
    message = format(self.class::MSG, kwarg: unnecessary_kwarg)
    add_offense(node, message: message) do |corrector|
      kwarg_node = node.last_argument.pairs.find do |pair|
        pair.key.value == unnecessary_kwarg.to_sym
      end
      corrector.remove_preceding(kwarg_node, 2)
      corrector.remove(kwarg_node)
    end
  end
end