Skip to content

Commit ef447c1

Browse files
authored
Merge pull request #1907 from rubocop/reduced-private-scope
Don't always define node patterns in private scope
2 parents cfb3a48 + ce09cb2 commit ef447c1

File tree

85 files changed

+717
-764
lines changed

Some content is hidden

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

85 files changed

+717
-764
lines changed

lib/rubocop/cop/rspec/around_block.rb

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,21 @@ 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+
3449
def on_block(node)
3550
hook_block(node) do |(example_proxy)|
3651
if example_proxy.nil?
@@ -49,21 +64,6 @@ def on_numblock(node)
4964

5065
private
5166

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: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,16 @@ 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+
2631
def on_send(node)
2732
be_without_args(node) do |matcher|
2833
add_offense(matcher.loc.selector)
2934
end
3035
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
3836
end
3937
end
4038
end

lib/rubocop/cop/rspec/be_empty.rb

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,6 @@ 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-
3222
# @!method expect_array_matcher?(node)
3323
def_node_matcher :expect_array_matcher?, <<~PATTERN
3424
(send
@@ -41,6 +31,14 @@ def on_send(node)
4131
_?
4232
)
4333
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
4442
end
4543
end
4644
end

lib/rubocop/cop/rspec/be_eq.rb

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,18 @@ 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+
3237
def on_send(node)
3338
return unless eq_type_with_identity?(node)
3439

3540
add_offense(node.loc.selector) do |corrector|
3641
corrector.replace(node.loc.selector, 'be')
3742
end
3843
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
4644
end
4745
end
4846
end

lib/rubocop/cop/rspec/be_eql.rb

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,18 @@ 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+
4651
def on_send(node)
4752
eql_type_with_identity(node) do |eql|
4853
add_offense(eql.loc.selector) do |corrector|
4954
corrector.replace(eql.loc.selector, 'be')
5055
end
5156
end
5257
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
6058
end
6159
end
6260
end

lib/rubocop/cop/rspec/be_nil.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ 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+
3545
def on_send(node)
3646
case style
3747
when :be
@@ -43,16 +53,6 @@ def on_send(node)
4353

4454
private
4555

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: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ 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+
2934
def on_send(node)
3035
before_or_after_all(node) do |hook|
3136
add_offense(
@@ -34,13 +39,6 @@ def on_send(node)
3439
)
3540
end
3641
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
4442
end
4543
end
4644
end

lib/rubocop/cop/rspec/change_by_zero.rb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,6 @@ 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-
8270
# @!method expect_change_with_arguments(node)
8371
def_node_matcher :expect_change_with_arguments, <<~PATTERN
8472
(send
@@ -101,6 +89,18 @@ def on_send(node)
10189
$(send nil? CHANGE_METHODS ...)
10290
PATTERN
10391

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,6 +29,15 @@ 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+
3241
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
3342
context_method(node) do |context|
3443
add_offense(context) do |corrector|
@@ -39,15 +48,6 @@ def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
3948

4049
private
4150

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

lib/rubocop/cop/rspec/context_wording.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ class ContextWording < Base
6060

6161
MSG = 'Context description should match %<patterns>s.'
6262

63+
# @!method context_wording(node)
64+
def_node_matcher :context_wording, <<~PATTERN
65+
(block (send #rspec? { :context :shared_context } $({str dstr xstr} ...) ...) ...)
66+
PATTERN
67+
6368
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
6469
context_wording(node) do |context|
6570
if bad_pattern?(context)
@@ -71,11 +76,6 @@ def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
7176

7277
private
7378

74-
# @!method context_wording(node)
75-
def_node_matcher :context_wording, <<~PATTERN
76-
(block (send #rspec? { :context :shared_context } $({str dstr xstr} ...) ...) ...)
77-
PATTERN
78-
7979
def allowed_patterns
8080
super + prefix_regexes
8181
end

0 commit comments

Comments
 (0)