summaryrefslogtreecommitdiff
path: root/rubocop/cop/scalability/file_uploads.rb
blob: 83017217e32a009f8aa6d8137559abe659f6d63f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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
      #
      class FileUploads < RuboCop::Cop::Cop
        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)

          add_offense(find_file_param(node), location: :expression)
        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