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

ensure_string_detail.rb « api « cop « rubocop - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bc999525055c95a49fa3918017fe58886bdab3f9 (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
# frozen_string_literal: true

require_relative '../../code_reuse_helpers'

module RuboCop
  module Cop
    module API
      class EnsureStringDetail < RuboCop::Cop::Base
        include CodeReuseHelpers

        # This cop checks that API detail entries use Strings
        #
        # https://gitlab.com/gitlab-org/gitlab/-/issues/379037
        #
        # @example
        #
        # # bad
        # detail ['Foo bar baz bat', 'http://example.com']
        #
        # # good
        # detail 'Foo bar baz bat. http://example.com'
        #
        # end
        #
        MSG = 'Only String objects are permitted in API detail field.'

        def_node_matcher :detail_in_desc, <<~PATTERN
          (block
            (send nil? :desc ...)
            _args
            `(send nil? :detail $_ ...)
          )
        PATTERN

        RESTRICT_ON_SEND = %i[detail].freeze

        def on_send(node)
          return unless in_api?(node)

          parent = node.each_ancestor(:block).first
          detail_arg = detail_in_desc(parent)

          return unless detail_arg
          return if [:str, :dstr].include?(detail_arg.type)

          add_offense(node)
        end
      end
    end
  end
end