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

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

module RuboCop
  module Cop
    module Gitlab
      class PredicateMemoization < RuboCop::Cop::Base
        MSG = <<~EOL
          Avoid using `@value ||= query` inside predicate methods in order to
          properly memoize `false` or `nil` values.
          https://docs.gitlab.com/ee/development/utilities.html#strongmemoize
        EOL

        def on_def(node)
          return unless predicate_method?(node)

          select_offenses(node).each do |offense|
            add_offense(offense)
          end
        end

        private

        def predicate_method?(node)
          node.method_name.to_s.end_with?('?')
        end

        def or_ivar_assignment?(or_assignment)
          lhs = or_assignment.each_child_node.first

          lhs.ivasgn_type?
        end

        def select_offenses(node)
          node.each_descendant(:or_asgn).select do |or_assignment|
            or_ivar_assignment?(or_assignment)
          end
        end
      end
    end
  end
end