Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement UnusedArgument cop #61

Merged
merged 10 commits into from
Dec 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,8 @@ GraphQL/OrderedFields:
VersionAdded: '0.80'
Description: 'Fields should be alphabetically sorted within groups'

GraphQL/UnusedArgument:
Enabled: true
Description: 'Arguments should either be listed explicitly or **rest should be in the resolve signature'


133 changes: 133 additions & 0 deletions lib/rubocop/cop/graphql/unused_argument.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
# frozen_string_literal: true

module RuboCop
module Cop
module GraphQL
# Arguments should either be listed explicitly or **rest should be in the resolve signature.
#
# @example
# # good
#
# class SomeResolver < Resolvers::Base
# argument :arg1, String, required: true
# argument :arg2, String, required: true
#
# def resolve(arg1:, arg2:); end
# end
#
# # good
#
# class SomeResolver < Resolvers::Base
# argument :arg1, String, required: true
# argument :arg2, String, required: true
#
# def resolve(arg1:, **rest); end
# end
#
# # good
#
# class SomeResolver < Resolvers::Base
# type SomeType, null: false
#
# argument :arg1, String, required: true
# argument :arg2, String, required: true
#
# def resolve(args); end
# end
#
# # bad
#
# class SomeResolver < Resolvers::Base
# type SomeType, null: false
#
# argument :arg1, String, required: true
# argument :arg2, String, required: true
#
# def resolve(arg1:); end
# end
#
# # bad
#
# class SomeResolver < Resolvers::Base
# type SomeType, null: false
#
# argument :arg1, String, required: true
# argument :arg2, String, required: true
#
# def resolve; end
# end
#
class UnusedArgument < Base
extend AutoCorrector

MSG = "Argument%<ending>s `%<unresolved_args>s` should be listed in the resolve signature."

def on_class(node)
resolve_method_node = find_resolve_method_node(node)
return if resolve_method_node.nil? ||
resolve_method_node.arguments.any? do |arg|
arg.arg_type? || arg.kwrestarg_type?
end

declared_arg_nodes = argument_declarations(node)
return unless declared_arg_nodes.any?
aishek marked this conversation as resolved.
Show resolved Hide resolved

unresolved_args = find_unresolved_args(resolve_method_node, declared_arg_nodes)
register_offense(resolve_method_node, unresolved_args) if unresolved_args.any?
end

private

def find_resolve_method_node(node)
resolve_method_nodes = resolve_method_definition(node)
resolve_method_nodes.to_a.last if resolve_method_nodes.any?
end

def find_unresolved_args(method_node, declared_arg_nodes)
resolve_method_kwargs = method_node.arguments.select(&:kwarg_type?).map do |node|
node.node_parts[0]
end.to_set
declared_args = declared_arg_nodes.map { |node| RuboCop::GraphQL::Argument.new(node) }
declared_args.map(&:name).uniq.reject do |declared_arg|
resolve_method_kwargs.include?(declared_arg)
end
end

def register_offense(node, unresolved_args)
unresolved_args_source = unresolved_args.map { |v| "#{v}:" }.join(", ")

message = format(
self.class::MSG,
unresolved_args: unresolved_args_source,
ending: unresolved_args.size == 1 ? "" : "s"
)

add_offense(node, message: message) do |corrector|
if node.arguments?
corrector.insert_after(arg_end(node.arguments.last), ", #{unresolved_args_source}")
else
corrector.insert_after(method_name(node), "(#{unresolved_args_source})")
end
end
end

def method_name(node)
node.loc.keyword.end.resize(node.method_name.to_s.size + 1)
end

def arg_end(node)
node.loc.expression.end
end

def_node_search :argument_declarations, <<~PATTERN
(send nil? :argument (:sym _) ...)
PATTERN

def_node_search :resolve_method_definition, <<~PATTERN
(def :resolve
(args ...) ...)
PATTERN
end
end
end
end
1 change: 1 addition & 0 deletions lib/rubocop/cop/graphql_cops.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
require_relative "graphql/object_description"
require_relative "graphql/ordered_arguments"
require_relative "graphql/ordered_fields"
require_relative "graphql/unused_argument"
130 changes: 130 additions & 0 deletions spec/rubocop/cop/graphql/unused_argument_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::GraphQL::UnusedArgument do
subject(:cop) { described_class.new(config) }

let(:config) { RuboCop::Config.new }

context "when all args are used" do
it "not registers an offense" do
expect_no_offenses(<<~RUBY)
class SomeResolver < Resolvers::Base
argument :arg1, String, required: true
argument :arg2, String, required: true

def resolve(arg1:, arg2:); end
end
RUBY
end
end

context "when first arg and splats arg are used" do
it "not registers an offense" do
expect_no_offenses(<<~RUBY)
class SomeResolver < Resolvers::Base
argument :arg1, String, required: true
argument :arg2, String, required: true

def resolve(arg1:, **rest); end
end
RUBY
end
end

context "when hash arg is used" do
it "not registers an offense" do
expect_no_offenses(<<~RUBY)
class SomeResolver < Resolvers::Base
argument :arg1, String, required: true
argument :arg2, String, required: true

def resolve(args); end
end
RUBY
end
end

context "when splats arg is used" do
it "not registers an offense" do
expect_no_offenses(<<~RUBY)
class SomeResolver < Resolvers::Base
argument :arg1, String, required: true
argument :arg2, String, required: true

def resolve(**args); end
end
RUBY
end
end

context "when not all args are used" do
it "registers an offense" do
expect_offense(<<~RUBY)
class SomeResolver < Resolvers::Base
argument :arg1, String, required: true
argument :arg2, String, required: true

def resolve(arg1:); end
^^^^^^^^^^^^^^^^^^^^^^^ Argument `arg2:` should be listed in the resolve signature.
end
RUBY

expect_correction(<<~RUBY)
class SomeResolver < Resolvers::Base
argument :arg1, String, required: true
argument :arg2, String, required: true

def resolve(arg1:, arg2:); end
end
RUBY
end
end

context "when not all args are used, but some args declared twice" do
it "registers an offense" do
expect_offense(<<~RUBY)
class SomeResolver < Resolvers::Base
argument :arg1, String, required: true
argument :arg2, String, required: true
argument :arg2, String, required: true

def resolve(arg1:); end
^^^^^^^^^^^^^^^^^^^^^^^ Argument `arg2:` should be listed in the resolve signature.
end
RUBY

expect_correction(<<~RUBY)
class SomeResolver < Resolvers::Base
argument :arg1, String, required: true
argument :arg2, String, required: true
argument :arg2, String, required: true

def resolve(arg1:, arg2:); end
end
RUBY
end
end

context "when args are not used at all" do
it "registers an offense" do
expect_offense(<<~RUBY)
class SomeResolver < Resolvers::Base
argument :arg1, String, required: true
argument :arg2, String, required: true

def resolve; end
^^^^^^^^^^^^^^^^ Arguments `arg1:, arg2:` should be listed in the resolve signature.
end
RUBY

expect_correction(<<~RUBY)
class SomeResolver < Resolvers::Base
argument :arg1, String, required: true
argument :arg2, String, required: true

def resolve(arg1:, arg2:); end
end
RUBY
end
end
end