Commit aa8a5940 authored by Mario de la Ossa's avatar Mario de la Ossa

RuboCop: Add cop for GraphQL::ID_TYPE

Let's use the more restrictive Types::GlobalIDType when possible
parent 21dfdd32
...@@ -371,6 +371,12 @@ Graphql/GIDExpectedType: ...@@ -371,6 +371,12 @@ Graphql/GIDExpectedType:
- 'spec/**/*.rb' - 'spec/**/*.rb'
- 'ee/spec/**/*.rb' - 'ee/spec/**/*.rb'
Graphql/IDType:
Enabled: true
Include:
- 'app/graphql/**/*'
- 'ee/app/graphql/**/*'
Graphql/JSONType: Graphql/JSONType:
Enabled: true Enabled: true
Include: Include:
......
...@@ -1245,3 +1245,44 @@ RSpec/TimecopFreeze: ...@@ -1245,3 +1245,44 @@ RSpec/TimecopFreeze:
- 'spec/support/shared_examples/workers/concerns/reenqueuer_shared_examples.rb' - 'spec/support/shared_examples/workers/concerns/reenqueuer_shared_examples.rb'
- 'spec/workers/concerns/reenqueuer_spec.rb' - 'spec/workers/concerns/reenqueuer_spec.rb'
- 'spec/workers/metrics/dashboard/prune_old_annotations_worker_spec.rb' - 'spec/workers/metrics/dashboard/prune_old_annotations_worker_spec.rb'
# Offense count: 43
Graphql/IDType:
Exclude:
- 'ee/app/graphql/ee/mutations/issues/update.rb'
- 'ee/app/graphql/ee/types/boards/board_issue_input_base_type.rb'
- 'ee/app/graphql/mutations/boards/lists/update_limit_metrics.rb'
- 'ee/app/graphql/mutations/instance_security_dashboard/remove_project.rb'
- 'ee/app/graphql/mutations/issues/set_epic.rb'
- 'ee/app/graphql/mutations/issues/set_iteration.rb'
- 'ee/app/graphql/mutations/iterations/update.rb'
- 'ee/app/graphql/resolvers/iterations_resolver.rb'
- 'app/graphql/mutations/boards/create.rb'
- 'app/graphql/mutations/boards/issues/issue_move_list.rb'
- 'app/graphql/mutations/boards/lists/update.rb'
- 'app/graphql/mutations/discussions/toggle_resolve.rb'
- 'app/graphql/mutations/issues/update.rb'
- 'app/graphql/mutations/merge_requests/set_milestone.rb'
- 'app/graphql/mutations/metrics/dashboard/annotations/delete.rb'
- 'app/graphql/mutations/notes/create/note.rb'
- 'app/graphql/mutations/notes/update/base.rb'
- 'app/graphql/mutations/snippets/destroy.rb'
- 'app/graphql/mutations/snippets/mark_as_spam.rb'
- 'app/graphql/mutations/snippets/update.rb'
- 'app/graphql/mutations/todos/mark_done.rb'
- 'app/graphql/mutations/todos/restore.rb'
- 'app/graphql/resolvers/board_lists_resolver.rb'
- 'app/graphql/resolvers/board_resolver.rb'
- 'app/graphql/resolvers/boards_resolver.rb'
- 'app/graphql/resolvers/design_management/design_at_version_resolver.rb'
- 'app/graphql/resolvers/design_management/design_resolver.rb'
- 'app/graphql/resolvers/design_management/designs_resolver.rb'
- 'app/graphql/resolvers/design_management/version/design_at_version_resolver.rb'
- 'app/graphql/resolvers/design_management/version_in_collection_resolver.rb'
- 'app/graphql/resolvers/design_management/version_resolver.rb'
- 'app/graphql/resolvers/design_management/versions_resolver.rb'
- 'app/graphql/resolvers/error_tracking/sentry_detailed_error_resolver.rb'
- 'app/graphql/resolvers/error_tracking/sentry_error_stack_trace_resolver.rb'
- 'app/graphql/resolvers/snippets_resolver.rb'
- 'app/graphql/resolvers/user_merge_requests_resolver.rb'
- 'app/graphql/resolvers/user_resolver.rb'
# frozen_string_literal: true
module RuboCop
module Cop
module Graphql
class IDType < RuboCop::Cop::Cop
MSG = 'Do not use GraphQL::ID_TYPE, use a specific GlobalIDType instead'
WHITELISTED_ARGUMENTS = %i[iid full_path project_path group_path].freeze
def_node_search :graphql_id_type?, <<~PATTERN
(send nil? :argument (_ #does_not_match?) (const (const nil? :GraphQL) :ID_TYPE) ...)
PATTERN
def on_send(node)
return unless graphql_id_type?(node)
add_offense(node)
end
private
def does_not_match?(arg)
!WHITELISTED_ARGUMENTS.include?(arg)
end
end
end
end
end
# frozen_string_literal: true
require 'fast_spec_helper'
require 'rubocop'
require_relative '../../../../rubocop/cop/graphql/id_type'
RSpec.describe RuboCop::Cop::Graphql::IDType, type: :rubocop do
include CopHelper
subject(:cop) { described_class.new }
it 'adds an offense when GraphQL::ID_TYPE is used as a param to #argument' do
inspect_source(<<~TYPE)
argument :some_arg, GraphQL::ID_TYPE, some: other, params: do_not_matter
TYPE
expect(cop.offenses.size).to eq 1
end
context 'whitelisted arguments' do
RuboCop::Cop::Graphql::IDType::WHITELISTED_ARGUMENTS.each do |arg|
it "does not add an offense for calls to #argument with #{arg} as argument name" do
expect_no_offenses(<<~TYPE.strip)
argument #{arg}, GraphQL::ID_TYPE, some: other, params: do_not_matter
TYPE
end
end
end
it 'does not add an offense for calls to #argument without GraphQL::ID_TYPE' do
expect_no_offenses(<<~TYPE.strip)
argument :some_arg, ::Types::GlobalIDType[::Awardable], some: other, params: do_not_matter
TYPE
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment