diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-08-19 12:08:42 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-08-19 12:08:42 +0300 |
commit | b76ae638462ab0f673e5915986070518dd3f9ad3 (patch) | |
tree | bdab0533383b52873be0ec0eb4d3c66598ff8b91 /rubocop/cop/graphql/old_types.rb | |
parent | 434373eabe7b4be9593d18a585fb763f1e5f1a6f (diff) |
Add latest changes from gitlab-org/gitlab@14-2-stable-eev14.2.0-rc42
Diffstat (limited to 'rubocop/cop/graphql/old_types.rb')
-rw-r--r-- | rubocop/cop/graphql/old_types.rb | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/rubocop/cop/graphql/old_types.rb b/rubocop/cop/graphql/old_types.rb new file mode 100644 index 00000000000..2df594c7016 --- /dev/null +++ b/rubocop/cop/graphql/old_types.rb @@ -0,0 +1,44 @@ +# 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 |