Commit a05f26bf authored by Matthias Käppler's avatar Matthias Käppler

Merge branch 'bw-rubocop-graphql-types' into 'master'

New cop to enforce using new GraphQL types

See merge request gitlab-org/gitlab!66289
parents 3eabb6c3 9af1b899
...@@ -442,6 +442,15 @@ Graphql/JSONType: ...@@ -442,6 +442,15 @@ Graphql/JSONType:
- 'spec/**/*.rb' - 'spec/**/*.rb'
- 'ee/spec/**/*.rb' - 'ee/spec/**/*.rb'
Graphql/OldTypes:
Enabled: true
Include:
- 'app/graphql/**/*'
- 'ee/app/graphql/**/*'
Exclude:
- 'spec/**/*.rb'
- 'ee/spec/**/*.rb'
RSpec/EnvAssignment: RSpec/EnvAssignment:
Enable: true Enable: true
Include: Include:
......
This diff is collapsed.
# frozen_string_literal: true
# This cop checks for use of older GraphQL types in GraphQL fields
# and arguments.
# GraphQL::ID_TYPE, GraphQL::INT_TYPE, GraphQL::STRING_TYPE, GraphQL::BOOLEAN_TYPE
#
# @example
#
# # bad
# class AwfulClass
# field :some_field, GraphQL::STRING_TYPE
# end
#
# # good
# class GreatClass
# field :some_field, GraphQL::Types::String
# end
module RuboCop
module Cop
module Graphql
class OldTypes < RuboCop::Cop::Cop
MSG_ID = 'Avoid using GraphQL::ID_TYPE. Use GraphQL::Types::ID instead'
MSG_INT = 'Avoid using GraphQL::INT_TYPE. Use GraphQL::Types::Int instead'
MSG_STRING = 'Avoid using GraphQL::STRING_TYPE. Use GraphQL::Types::String instead'
MSG_BOOLEAN = 'Avoid using GraphQL::BOOLEAN_TYPE. Use GraphQL::Types::Boolean instead'
def_node_matcher :has_old_type?, <<~PATTERN
(send nil? {:field :argument}
(sym _)
(const (const nil? :GraphQL) ${:ID_TYPE :INT_TYPE :STRING_TYPE :BOOLEAN_TYPE})
(...)?)
PATTERN
def on_send(node)
old_constant = has_old_type?(node)
return unless old_constant
add_offense(node, location: :expression, message: "#{self.class}::MSG_#{old_constant[0..-6]}".constantize)
end
end
end
end
end
# frozen_string_literal: true
require 'fast_spec_helper'
require 'rspec-parameterized'
require_relative '../../../../rubocop/cop/graphql/old_types'
RSpec.describe RuboCop::Cop::Graphql::OldTypes do
using RSpec::Parameterized::TableSyntax
subject(:cop) { described_class.new }
where(:old_type, :message) do
'GraphQL::ID_TYPE' | 'Avoid using GraphQL::ID_TYPE. Use GraphQL::Types::ID instead'
'GraphQL::INT_TYPE' | 'Avoid using GraphQL::INT_TYPE. Use GraphQL::Types::Int instead'
'GraphQL::STRING_TYPE' | 'Avoid using GraphQL::STRING_TYPE. Use GraphQL::Types::String instead'
'GraphQL::BOOLEAN_TYPE' | 'Avoid using GraphQL::BOOLEAN_TYPE. Use GraphQL::Types::Boolean instead'
end
with_them do
context 'fields' do
it 'adds an offense when an old type is used' do
expect_offense(<<~RUBY)
class MyType
field :some_field, #{old_type}
^^^^^^^^^^^^^^^^^^^#{'^' * old_type.length} #{message}
end
RUBY
end
it "adds an offense when an old type is used with other keywords" do
expect_offense(<<~RUBY)
class MyType
field :some_field, #{old_type}, null: true, description: 'My description'
^^^^^^^^^^^^^^^^^^^#{'^' * old_type.length}^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{message}
end
RUBY
end
end
context 'arguments' do
it 'adds an offense when an old type is used' do
expect_offense(<<~RUBY)
class MyType
field :some_arg, #{old_type}
^^^^^^^^^^^^^^^^^#{'^' * old_type.length} #{message}
end
RUBY
end
it 'adds an offense when an old type is used with other keywords' do
expect_offense(<<~RUBY)
class MyType
argument :some_arg, #{old_type}, null: true, description: 'My description'
^^^^^^^^^^^^^^^^^^^^#{'^' * old_type.length}^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{message}
end
RUBY
end
end
end
it 'does not add an offense for other types in fields' do
expect_no_offenses(<<~RUBY.strip)
class MyType
field :some_field, GraphQL::Types::JSON
end
RUBY
end
it 'does not add an offense for other types in arguments' do
expect_no_offenses(<<~RUBY.strip)
class MyType
argument :some_arg, GraphQL::Types::JSON
end
RUBY
end
it 'does not add an offense for uses outside of field or argument' do
expect_no_offenses(<<~RUBY.strip)
class MyType
foo :some_field, GraphQL::ID_TYPE
end
RUBY
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