Skip to content

Commit 089491f

Browse files
committed
Move node patterns into private scope
Private code should be private. I think this makes it easier to read and understand the code.
1 parent 5d87427 commit 089491f

File tree

86 files changed

+769
-717
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+769
-717
lines changed

.rubocop.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ Lint/InterpolationCheck:
4747
Lint/RedundantCopDisableDirective:
4848
Enabled: false
4949

50+
Lint/UselessAccessModifier:
51+
MethodCreatingMethods:
52+
- def_node_matcher
53+
- def_node_search
54+
5055
Metrics/BlockLength:
5156
Exclude:
5257
- rubocop-rspec.gemspec

lib/rubocop/cop/rspec/around_block.rb

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,6 @@ class AroundBlock < Base
3131
MSG_UNUSED_ARG = 'You should call `%<arg>s.call` ' \
3232
'or `%<arg>s.run`.'
3333

34-
# @!method hook_block(node)
35-
def_node_matcher :hook_block, <<~PATTERN
36-
(block (send nil? :around sym ?) (args $...) ...)
37-
PATTERN
38-
39-
# @!method hook_numblock(node)
40-
def_node_matcher :hook_numblock, <<~PATTERN
41-
(numblock (send nil? :around sym ?) ...)
42-
PATTERN
43-
44-
# @!method find_arg_usage(node)
45-
def_node_search :find_arg_usage, <<~PATTERN
46-
{(send $... {:call :run}) (send _ _ $...) (yield $...) (block-pass $...)}
47-
PATTERN
48-
4934
def on_block(node)
5035
hook_block(node) do |(example_proxy)|
5136
if example_proxy.nil?
@@ -64,6 +49,21 @@ def on_numblock(node)
6449

6550
private
6651

52+
# @!method hook_block(node)
53+
def_node_matcher :hook_block, <<~PATTERN
54+
(block (send nil? :around sym ?) (args $...) ...)
55+
PATTERN
56+
57+
# @!method hook_numblock(node)
58+
def_node_matcher :hook_numblock, <<~PATTERN
59+
(numblock (send nil? :around sym ?) ...)
60+
PATTERN
61+
62+
# @!method find_arg_usage(node)
63+
def_node_search :find_arg_usage, <<~PATTERN
64+
{(send $... {:call :run}) (send _ _ $...) (yield $...) (block-pass $...)}
65+
PATTERN
66+
6767
def add_no_arg_offense(node)
6868
add_offense(node, message: MSG_NO_ARG)
6969
end

lib/rubocop/cop/rspec/be.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,18 @@ class Be < Base
2323

2424
RESTRICT_ON_SEND = Runners.all
2525

26-
# @!method be_without_args(node)
27-
def_node_matcher :be_without_args, <<~PATTERN
28-
(send _ #Runners.all $(send nil? :be))
29-
PATTERN
30-
3126
def on_send(node)
3227
be_without_args(node) do |matcher|
3328
add_offense(matcher.loc.selector)
3429
end
3530
end
31+
32+
private
33+
34+
# @!method be_without_args(node)
35+
def_node_matcher :be_without_args, <<~PATTERN
36+
(send _ #Runners.all $(send nil? :be))
37+
PATTERN
3638
end
3739
end
3840
end

lib/rubocop/cop/rspec/be_empty.rb

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ class BeEmpty < Base
1919
MSG = 'Use `be_empty` matchers for checking an empty array.'
2020
RESTRICT_ON_SEND = %i[contain_exactly match_array].freeze
2121

22+
def on_send(node)
23+
expect_array_matcher?(node.parent) do |expect|
24+
add_offense(expect) do |corrector|
25+
corrector.replace(expect, 'be_empty')
26+
end
27+
end
28+
end
29+
30+
private
31+
2232
# @!method expect_array_matcher?(node)
2333
def_node_matcher :expect_array_matcher?, <<~PATTERN
2434
(send
@@ -31,14 +41,6 @@ class BeEmpty < Base
3141
_?
3242
)
3343
PATTERN
34-
35-
def on_send(node)
36-
expect_array_matcher?(node.parent) do |expect|
37-
add_offense(expect) do |corrector|
38-
corrector.replace(expect, 'be_empty')
39-
end
40-
end
41-
end
4244
end
4345
end
4446
end

lib/rubocop/cop/rspec/be_eq.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,20 @@ class BeEq < Base
2929
MSG = 'Prefer `be` over `eq`.'
3030
RESTRICT_ON_SEND = %i[eq].freeze
3131

32-
# @!method eq_type_with_identity?(node)
33-
def_node_matcher :eq_type_with_identity?, <<~PATTERN
34-
(send nil? :eq {true false nil})
35-
PATTERN
36-
3732
def on_send(node)
3833
return unless eq_type_with_identity?(node)
3934

4035
add_offense(node.loc.selector) do |corrector|
4136
corrector.replace(node.loc.selector, 'be')
4237
end
4338
end
39+
40+
private
41+
42+
# @!method eq_type_with_identity?(node)
43+
def_node_matcher :eq_type_with_identity?, <<~PATTERN
44+
(send nil? :eq {true false nil})
45+
PATTERN
4446
end
4547
end
4648
end

lib/rubocop/cop/rspec/be_eql.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,20 @@ class BeEql < Base
4343
MSG = 'Prefer `be` over `eql`.'
4444
RESTRICT_ON_SEND = %i[to].freeze
4545

46-
# @!method eql_type_with_identity(node)
47-
def_node_matcher :eql_type_with_identity, <<~PATTERN
48-
(send _ :to $(send nil? :eql {true false int float sym nil}))
49-
PATTERN
50-
5146
def on_send(node)
5247
eql_type_with_identity(node) do |eql|
5348
add_offense(eql.loc.selector) do |corrector|
5449
corrector.replace(eql.loc.selector, 'be')
5550
end
5651
end
5752
end
53+
54+
private
55+
56+
# @!method eql_type_with_identity(node)
57+
def_node_matcher :eql_type_with_identity, <<~PATTERN
58+
(send _ :to $(send nil? :eql {true false int float sym nil}))
59+
PATTERN
5860
end
5961
end
6062
end

lib/rubocop/cop/rspec/be_nil.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,6 @@ class BeNil < Base
3232
BE_NIL_MSG = 'Prefer `be_nil` over `be(nil)`.'
3333
RESTRICT_ON_SEND = %i[be be_nil].freeze
3434

35-
# @!method be_nil_matcher?(node)
36-
def_node_matcher :be_nil_matcher?, <<~PATTERN
37-
(send nil? :be_nil)
38-
PATTERN
39-
40-
# @!method nil_value_expectation?(node)
41-
def_node_matcher :nil_value_expectation?, <<~PATTERN
42-
(send nil? :be nil)
43-
PATTERN
44-
4535
def on_send(node)
4636
case style
4737
when :be
@@ -53,6 +43,16 @@ def on_send(node)
5343

5444
private
5545

46+
# @!method be_nil_matcher?(node)
47+
def_node_matcher :be_nil_matcher?, <<~PATTERN
48+
(send nil? :be_nil)
49+
PATTERN
50+
51+
# @!method nil_value_expectation?(node)
52+
def_node_matcher :nil_value_expectation?, <<~PATTERN
53+
(send nil? :be nil)
54+
PATTERN
55+
5656
def check_be_style(node)
5757
return unless be_nil_matcher?(node)
5858

lib/rubocop/cop/rspec/before_after_all.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@ class BeforeAfterAll < Base
2626

2727
RESTRICT_ON_SEND = Set[:before, :after].freeze
2828

29-
# @!method before_or_after_all(node)
30-
def_node_matcher :before_or_after_all, <<~PATTERN
31-
$(send _ RESTRICT_ON_SEND (sym {:all :context}))
32-
PATTERN
33-
3429
def on_send(node)
3530
before_or_after_all(node) do |hook|
3631
add_offense(
@@ -39,6 +34,13 @@ def on_send(node)
3934
)
4035
end
4136
end
37+
38+
private
39+
40+
# @!method before_or_after_all(node)
41+
def_node_matcher :before_or_after_all, <<~PATTERN
42+
$(send _ RESTRICT_ON_SEND (sym {:all :context}))
43+
PATTERN
4244
end
4345
end
4446
end

lib/rubocop/cop/rspec/change_by_zero.rb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,18 @@ class ChangeByZero < Base
6767
CHANGE_METHODS = Set[:change, :a_block_changing, :changing].freeze
6868
RESTRICT_ON_SEND = CHANGE_METHODS.freeze
6969

70+
def on_send(node)
71+
expect_change_with_arguments(node.parent) do |change|
72+
register_offense(node.parent, change)
73+
end
74+
75+
expect_change_with_block(node.parent.parent) do |change|
76+
register_offense(node.parent.parent, change)
77+
end
78+
end
79+
80+
private
81+
7082
# @!method expect_change_with_arguments(node)
7183
def_node_matcher :expect_change_with_arguments, <<~PATTERN
7284
(send
@@ -89,18 +101,6 @@ class ChangeByZero < Base
89101
$(send nil? CHANGE_METHODS ...)
90102
PATTERN
91103

92-
def on_send(node)
93-
expect_change_with_arguments(node.parent) do |change|
94-
register_offense(node.parent, change)
95-
end
96-
97-
expect_change_with_block(node.parent.parent) do |change|
98-
register_offense(node.parent.parent, change)
99-
end
100-
end
101-
102-
private
103-
104104
# rubocop:disable Metrics/MethodLength
105105
def register_offense(node, change_node)
106106
if compound_expectations?(node)

lib/rubocop/cop/rspec/context_method.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,6 @@ class ContextMethod < Base
2929

3030
MSG = 'Use `describe` for testing methods.'
3131

32-
# @!method context_method(node)
33-
def_node_matcher :context_method, <<~PATTERN
34-
(block
35-
(send #rspec? :context
36-
${(str #method_name?) (dstr (str #method_name?) ...)}
37-
...)
38-
...)
39-
PATTERN
40-
4132
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
4233
context_method(node) do |context|
4334
add_offense(context) do |corrector|
@@ -48,6 +39,15 @@ def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
4839

4940
private
5041

42+
# @!method context_method(node)
43+
def_node_matcher :context_method, <<~PATTERN
44+
(block
45+
(send #rspec? :context
46+
${(str #method_name?) (dstr (str #method_name?) ...)}
47+
...)
48+
...)
49+
PATTERN
50+
5151
def method_name?(description)
5252
description.start_with?('.', '#')
5353
end

0 commit comments

Comments
 (0)