forked from rubocop/rubocop-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequire_dependency.rb
38 lines (33 loc) · 1.24 KB
/
require_dependency.rb
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
# frozen_string_literal: true
module RuboCop
module Cop
module Rails
# This cop checks for the usage of `require_dependency`.
#
# `require_dependency` is an obsolete method for Rails applications running in Zeitwerk mode.
# In Zeitwerk mode, the semantics should match Ruby's and no need to be defensive with load order,
# just refer to classes and modules normally.
# If the constant name is dynamic, camelize if needed, and constantize.
#
# Applications running in Zeitwerk mode should not use `require_dependency`.
#
# NOTE: This cop is disabled by default. Please enable it if you are using Zeitwerk mode.
#
# @example
# # bad
# require_dependency 'some_lib'
class RequireDependency < Base
extend TargetRailsVersion
minimum_target_rails_version 6.0
MSG = 'Do not use `require_dependency` with Zeitwerk mode.'
RESTRICT_ON_SEND = %i[require_dependency].freeze
def_node_matcher :require_dependency_call?, <<~PATTERN
(send {nil? (const _ :Kernel)} :require_dependency _)
PATTERN
def on_send(node)
require_dependency_call?(node) { add_offense(node) }
end
end
end
end
end