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

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

module RuboCop
  module Cop
    module Gitlab
      # Cop that encourages usage of inherit=false for 2nd argument when using const_get.
      #
      # See https://gitlab.com/gitlab-org/gitlab/issues/27678
      class ConstGetInheritFalse < RuboCop::Cop::Base
        extend RuboCop::Cop::AutoCorrector

        MSG = 'Use inherit=false when using const_get.'

        def_node_matcher :const_get?, <<~PATTERN
        (send _ :const_get ...)
        PATTERN

        def on_send(node)
          return unless const_get?(node)
          return if second_argument(node)&.false_type?

          add_offense(node.loc.selector) do |corrector|
            if arg = second_argument(node)
              corrector.replace(arg.source_range, 'false')
            else
              first_argument = node.arguments[0]
              corrector.insert_after(first_argument.source_range, ', false')
            end
          end
        end

        private

        def second_argument(node)
          node.arguments[1]
        end
      end
    end
  end
end