debian-mirror-gitlab/rubocop/cop/scalability/file_uploads.rb

62 lines
1.6 KiB
Ruby
Raw Normal View History

2019-12-04 20:38:33 +05:30
# frozen_string_literal: true
module RuboCop
module Cop
module Scalability
# This cop checks for `File` params in API
#
# @example
#
# # bad
# params do
# requires :file, type: File
# end
#
# params do
# optional :file, type: File
# end
#
# # good
# params do
# requires :file, type: ::API::Validations::Types::WorkhorseFile
# end
#
# params do
# optional :file, type: ::API::Validations::Types::WorkhorseFile
# end
#
2022-10-11 01:57:18 +05:30
class FileUploads < RuboCop::Cop::Base
2019-12-04 20:38:33 +05:30
MSG = 'Do not upload files without workhorse acceleration. Please refer to https://docs.gitlab.com/ee/development/uploads.html'
def_node_search :file_type_params?, <<~PATTERN
(send nil? {:requires :optional} (sym _) (hash <(pair (sym :type)(const nil? :File)) ...>))
PATTERN
def_node_search :file_types_params?, <<~PATTERN
(send nil? {:requires :optional} (sym _) (hash <(pair (sym :types)(array <(const nil? :File) ...>)) ...>))
PATTERN
def be_file_param_usage?(node)
file_type_params?(node) || file_types_params?(node)
end
def on_send(node)
return unless be_file_param_usage?(node)
2022-10-11 01:57:18 +05:30
add_offense(find_file_param(node))
2019-12-04 20:38:33 +05:30
end
private
def find_file_param(node)
node.each_descendant.find { |children| file_node_pattern.match(children) }
end
def file_node_pattern
@file_node_pattern ||= RuboCop::NodePattern.new("(const nil? :File)")
end
end
end
end
end