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

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

module RuboCop
  module Cop
    # Cop that blacklists the use of ".becomes(SomeConstant)".
    #
    # The use of becomes() will result in a new object being created, throwing
    # away any eager loaded assocations. This in turn can cause N+1 query
    # problems, even when a developer eager loaded all necessary associations.
    #
    # See https://gitlab.com/gitlab-org/gitlab/-/issues/23182 for more information.
    class AvoidBecomes < RuboCop::Cop::Base
      MSG = 'Avoid the use of becomes(SomeConstant), as this creates a ' \
        'new object and throws away any eager loaded associations. ' \
        'When creating URLs in views, just use the path helpers directly. ' \
        'For example, instead of `link_to(..., [group.becomes(Namespace), ...])` ' \
        'use `link_to(..., namespace_foo_path(group, ...))`. Most of the time there is no ' \
        'need to pass in namespace to the path helpers after implementaton of ' \
        'https://gitlab.com/gitlab-org/gitlab-foss/-/merge_requests/12566'

      def_node_matcher :becomes?, <<~PATTERN
        (send {send ivar lvar} :becomes ...)
      PATTERN

      def on_send(node)
        add_offense(node) if becomes?(node)
      end
    end
  end
end