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

54 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
2021-01-03 14:25:43 +05:30
class Base < RuboCop::Cop::Cop
# 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
add_offense(node.children[1])
end
end
2021-01-03 14:25:43 +05:30
def autocorrect(node)
lambda do |corrector|
corrector.replace(node, '::API::Base')
end
end
2020-07-28 23:09:34 +05:30
end
end
end
end