Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'rubocop/cop/api/base.rb')
-rw-r--r--rubocop/cop/api/base.rb53
1 files changed, 53 insertions, 0 deletions
diff --git a/rubocop/cop/api/base.rb b/rubocop/cop/api/base.rb
new file mode 100644
index 00000000000..85b19e9a833
--- /dev/null
+++ b/rubocop/cop/api/base.rb
@@ -0,0 +1,53 @@
+# frozen_string_literal: true
+
+module RuboCop
+ module Cop
+ module API
+ class Base < RuboCop::Cop::Cop
+ # This cop checks that APIs subclass API::Base.
+ #
+ # @example
+ #
+ # # bad
+ # module API
+ # class Projects < Grape::API
+ # end
+ # end
+ #
+ # module API
+ # class Projects < Grape::API::Instance
+ # end
+ # end
+ #
+ # # good
+ # module API
+ # class Projects < ::API::Base
+ # end
+ # end
+ MSG = 'Inherit from ::API::Base instead of Grape::API::Instance or Grape::API. ' \
+ 'For more details check https://gitlab.com/gitlab-org/gitlab/-/issues/215230.'
+
+ def_node_matcher :grape_api, '(const (const {nil? (cbase)} :Grape) :API)'
+ def_node_matcher :grape_api_definition, <<~PATTERN
+ (class
+ (const _ _)
+ {#grape_api (const #grape_api :Instance)}
+ ...
+ )
+ PATTERN
+
+ def on_class(node)
+ grape_api_definition(node) do
+ add_offense(node.children[1])
+ end
+ end
+
+ def autocorrect(node)
+ lambda do |corrector|
+ corrector.replace(node, '::API::Base')
+ end
+ end
+ end
+ end
+ end
+end