debian-mirror-gitlab/rubocop/cop/api/base.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

52 lines
1.3 KiB
Ruby
Raw Normal View History

2020-07-28 23:09:34 +05:30
# frozen_string_literal: true
module RuboCop
module Cop
module API
2022-10-11 01:57:18 +05:30
class Base < RuboCop::Cop::Base
extend RuboCop::Cop::AutoCorrector
2021-01-03 14:25:43 +05:30
# This cop checks that APIs subclass API::Base.
2020-07-28 23:09:34 +05:30
#
# @example
#
# # bad
# module API
# class Projects < Grape::API
# end
# end
#
# module API
# class Projects < Grape::API::Instance
# end
# end
2021-01-03 14:25:43 +05:30
#
# # 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.'
2020-07-28 23:09:34 +05:30
2021-01-03 14:25:43 +05:30
def_node_matcher :grape_api, '(const (const {nil? (cbase)} :Grape) :API)'
2020-07-28 23:09:34 +05:30
def_node_matcher :grape_api_definition, <<~PATTERN
(class
(const _ _)
2021-01-03 14:25:43 +05:30
{#grape_api (const #grape_api :Instance)}
2020-07-28 23:09:34 +05:30
...
)
PATTERN
def on_class(node)
grape_api_definition(node) do
2022-10-11 01:57:18 +05:30
add_offense(node.children[1]) do |corrector|
corrector.replace(node.children[1], '::API::Base')
end
2021-01-03 14:25:43 +05:30
end
end
2020-07-28 23:09:34 +05:30
end
end
end
end