-
-
Notifications
You must be signed in to change notification settings - Fork 269
/
remove_const.rb
39 lines (35 loc) · 949 Bytes
/
remove_const.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
39
# frozen_string_literal: true
module RuboCop
module Cop
module RSpec
# Checks that `remove_const` is not used in specs.
#
# @example
# # bad
# it 'does something' do
# Object.send(:remove_const, :SomeConstant)
# end
#
# before do
# SomeClass.send(:remove_const, :SomeConstant)
# end
#
class RemoveConst < Base
include RuboCop::RSpec::Language
MSG = 'Do not use remove_const in specs. ' \
'Consider using e.g. `stub_const`.'
RESTRICT_ON_SEND = %i[send __send__].freeze
# @!method remove_const(node)
def_node_matcher :remove_const, <<~PATTERN
(send _ {:send | :__send__} (sym :remove_const) _)
PATTERN
# Check for offenses
def on_send(node)
remove_const(node) do
add_offense(node)
end
end
end
end
end
end