Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
9af1b899
Commit
9af1b899
authored
Jul 20, 2021
by
Brett Walker
Committed by
Matthias Käppler
Jul 20, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
New cop to enforce using new GraphQL types
instead of old ones
parent
c6282573
Changes
4
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
541 additions
and
0 deletions
+541
-0
.rubocop.yml
.rubocop.yml
+9
-0
.rubocop_manual_todo.yml
.rubocop_manual_todo.yml
+404
-0
rubocop/cop/graphql/old_types.rb
rubocop/cop/graphql/old_types.rb
+44
-0
spec/rubocop/cop/graphql/old_types_spec.rb
spec/rubocop/cop/graphql/old_types_spec.rb
+84
-0
No files found.
.rubocop.yml
View file @
9af1b899
...
...
@@ -442,6 +442,15 @@ Graphql/JSONType:
-
'
spec/**/*.rb'
-
'
ee/spec/**/*.rb'
Graphql/OldTypes
:
Enabled
:
true
Include
:
-
'
app/graphql/**/*'
-
'
ee/app/graphql/**/*'
Exclude
:
-
'
spec/**/*.rb'
-
'
ee/spec/**/*.rb'
RSpec/EnvAssignment
:
Enable
:
true
Include
:
...
...
.rubocop_manual_todo.yml
View file @
9af1b899
This diff is collapsed.
Click to expand it.
rubocop/cop/graphql/old_types.rb
0 → 100644
View file @
9af1b899
# 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
spec/rubocop/cop/graphql/old_types_spec.rb
0 → 100644
View file @
9af1b899
# 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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment