File tree Expand file tree Collapse file tree 8 files changed +71
-50
lines changed Expand file tree Collapse file tree 8 files changed +71
-50
lines changed Original file line number Diff line number Diff line change @@ -65,8 +65,9 @@ class DescribedClass < Cop
6565 (block (send (const nil? {:Class :Module :Struct}) :new ...) ...)
6666 PATTERN
6767
68- def_node_matcher :rspec_block? ,
69- RuboCop ::RSpec ::Language ::ALL . block_pattern
68+ def_runtime_node_matcher :rspec_block? do
69+ all_selectors . block_pattern
70+ end
7071
7172 def_node_matcher :scope_changing_syntax? , '{def class module}'
7273
Original file line number Diff line number Diff line change @@ -47,7 +47,9 @@ class Dialect < Cop
4747
4848 MSG = 'Prefer `%<prefer>s` over `%<current>s`.'
4949
50- def_node_matcher :rspec_method? , ALL . send_pattern
50+ def_runtime_node_matcher :rspec_method? do
51+ all_selectors . send_pattern
52+ end
5153
5254 def on_send ( node )
5355 return unless rspec_method? ( node )
Original file line number Diff line number Diff line change @@ -28,16 +28,13 @@ class EmptyHook < Cop
2828
2929 MSG = 'Empty hook detected.'
3030
31- def_node_matcher :empty_hook? , <<~PATTERN
32- (block $#{ Hooks ::ALL . send_pattern } _ nil?)
33- PATTERN
34-
3531 def on_block ( node )
36- empty_hook? ( node ) do |hook |
37- add_offense ( hook ) do |corrector |
38- range = range_with_surrounding_space ( range : node . loc . expression )
39- corrector . remove ( range )
40- end
32+ return unless hook? ( node )
33+ return unless node . body . nil?
34+
35+ add_offense ( node . send_node ) do |corrector |
36+ range = range_with_surrounding_space ( range : node . loc . expression )
37+ corrector . remove ( range )
4138 end
4239 end
4340 end
Original file line number Diff line number Diff line change @@ -65,13 +65,15 @@ class HookArgument < Cop
6565 'argument for RSpec hooks.'
6666 EXPLICIT_MSG = 'Use `%<scope>p` for RSpec hooks.'
6767
68- HOOKS = Hooks :: ALL . node_pattern_union . freeze
69-
70- def_node_matcher :scoped_hook , <<-PATTERN
71- (block $(send _ #{ HOOKS } (sym ${:each :example})) ...)
72- PATTERN
68+ def_runtime_node_matcher :scoped_hook do
69+ <<-PATTERN
70+ (block $(send _ #{ hook_selectors . node_pattern_union } (sym ${:each :example})) ...)
71+ PATTERN
72+ end
7373
74- def_node_matcher :unscoped_hook , "(block $(send _ #{ HOOKS } ) ...)"
74+ def_runtime_node_matcher :unscoped_hook do
75+ "(block $(send _ #{ hook_selectors . node_pattern_union } ) ...)"
76+ end
7577
7678 def on_block ( node )
7779 hook ( node ) do |method_send , scope_name |
Original file line number Diff line number Diff line change @@ -45,12 +45,14 @@ class NamedSubject < Cop
4545 MSG = 'Name your test subject if you need ' \
4646 'to reference it explicitly.'
4747
48- def_node_matcher :rspec_block? , <<-PATTERN
49- {
50- #{ Examples ::ALL . block_pattern }
51- #{ Hooks ::ALL . block_pattern }
52- }
53- PATTERN
48+ def_runtime_node_matcher :rspec_block? do
49+ <<-PATTERN
50+ {
51+ #{ Examples ::ALL . block_pattern }
52+ #{ hook_selectors . block_pattern }
53+ }
54+ PATTERN
55+ end
5456
5557 def_node_matcher :shared_example? , <<-PATTERN
5658 #{ SharedGroups ::EXAMPLES . block_pattern }
Original file line number Diff line number Diff line change @@ -61,9 +61,14 @@ class SharedContext < Cop
6161
6262 examples = ( Examples ::ALL + Includes ::EXAMPLES )
6363 def_node_search :examples? , examples . send_pattern
64-
65- context = ( Hooks ::ALL + Helpers ::ALL + Includes ::CONTEXT + Subject ::ALL )
66- def_node_search :context? , context . send_pattern
64+ def_runtime_node_search :context? do
65+ (
66+ hook_selectors +
67+ Helpers ::ALL +
68+ Includes ::CONTEXT +
69+ Subject ::ALL
70+ ) . send_pattern
71+ end
6772
6873 def_node_matcher :shared_context , SharedGroups ::CONTEXT . block_pattern
6974 def_node_matcher :shared_example , SharedGroups ::EXAMPLES . block_pattern
Original file line number Diff line number Diff line change @@ -91,17 +91,15 @@ module Examples
9191 end
9292
9393 module Hooks
94- ALL = SelectorSet . new (
95- %i[
96- prepend_before
97- before
98- append_before
99- around
100- prepend_after
101- after
102- append_after
103- ]
104- )
94+ BUILT_IN_METHOD_NAMES = %i[
95+ prepend_before
96+ before
97+ append_before
98+ around
99+ prepend_after
100+ after
101+ append_after
102+ ] . freeze
105103
106104 module Scopes
107105 ALL = SelectorSet . new (
@@ -131,16 +129,6 @@ module Expectations
131129 module Runners
132130 ALL = SelectorSet . new ( %i[ to to_not not_to ] )
133131 end
134-
135- ALL =
136- ExampleGroups ::ALL +
137- SharedGroups ::ALL +
138- Examples ::ALL +
139- Hooks ::ALL +
140- Helpers ::ALL +
141- Subject ::ALL +
142- Expectations ::ALL +
143- Runners ::ALL
144132 end
145133 end
146134end
Original file line number Diff line number Diff line change @@ -20,11 +20,35 @@ module NodePattern
2020
2121 def_node_matcher :example? , Examples ::ALL . block_pattern
2222
23- def_node_matcher :hook? , Hooks ::ALL . block_pattern
24-
2523 def_node_matcher :let? , Helpers ::ALL . block_or_block_pass_pattern
2624
2725 def_node_matcher :subject? , Subject ::ALL . block_pattern
26+
27+ def_runtime_node_matcher :hook? do
28+ hook_selectors . block_pattern
29+ end
30+
31+ def hook_selectors
32+ custom_method_names = config
33+ . for_all_cops
34+ . fetch ( 'RSpec' , { } )
35+ . fetch ( 'Language' , { } )
36+ . fetch ( 'Hooks' , [ ] )
37+ . map ( &:to_sym )
38+
39+ SelectorSet . new ( Hooks ::BUILT_IN_METHOD_NAMES + custom_method_names )
40+ end
41+
42+ def all_selectors
43+ ExampleGroups ::ALL +
44+ SharedGroups ::ALL +
45+ Examples ::ALL +
46+ hook_selectors +
47+ Helpers ::ALL +
48+ Subject ::ALL +
49+ Expectations ::ALL +
50+ Runners ::ALL
51+ end
2852 end
2953 end
3054 end
You can’t perform that action at this time.
0 commit comments