Skip to content

Commit 6cc63e5

Browse files
committed
Use AST arguments instead of runtime matchers
1 parent fcf813d commit 6cc63e5

18 files changed

+100
-98
lines changed

lib/rubocop-rspec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
require_relative 'rubocop/rspec/top_level_describe'
1313
require_relative 'rubocop/rspec/wording'
1414
require_relative 'rubocop/rspec/language'
15-
require_relative 'rubocop/rspec/language/runtime_macros'
1615
require_relative 'rubocop/rspec/language/node_pattern'
1716
require_relative 'rubocop/rspec/top_level_group'
1817
require_relative 'rubocop/rspec/concept'

lib/rubocop/cop/rspec/base.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ module RSpec
2020
class Base < ::RuboCop::Cop::Base
2121
include RuboCop::RSpec::Language
2222
include RuboCop::RSpec::Language::NodePattern
23-
extend RuboCop::RSpec::Language::RuntimeMacros
2423

2524
DEFAULT_CONFIGURATION =
2625
RuboCop::RSpec::CONFIG.fetch('AllCops').fetch('RSpec')

lib/rubocop/cop/rspec/described_class.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,6 @@ class DescribedClass < Base
6565
(block (send (const nil? {:Class :Module :Struct}) :new ...) ...)
6666
PATTERN
6767

68-
def_runtime_node_matcher :rspec_block? do
69-
all_selectors.block_pattern
70-
end
71-
7268
def_node_matcher :scope_changing_syntax?, '{def class module}'
7369

7470
def_node_matcher :described_constant, <<-PATTERN
@@ -132,7 +128,7 @@ def scope_change?(node)
132128
end
133129

134130
def skippable_block?(node)
135-
node.block_type? && !rspec_block?(node) && cop_config['SkipBlocks']
131+
node.block_type? && !block_pattern?(node, rspec_all) && cop_config['SkipBlocks']
136132
end
137133

138134
def offensive?(node)

lib/rubocop/cop/rspec/dialect.rb

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,14 @@ class Dialect < Base
4747

4848
MSG = 'Prefer `%<prefer>s` over `%<current>s`.'
4949

50-
def_runtime_node_matcher :rspec_method? do
51-
all_selectors.send_pattern
52-
end
53-
5450
def on_send(node)
55-
return unless rspec_method?(node)
56-
return unless preferred_methods[node.method_name]
51+
return unless send_pattern?(node, rspec_all)
52+
53+
method_name = node.method_name
54+
return unless preferred_methods[method_name]
5755

58-
msg = format(MSG, prefer: preferred_method(node.method_name),
59-
current: node.method_name)
56+
msg = format(MSG, prefer: preferred_method(method_name),
57+
current: method_name)
6058

6159
add_offense(node, message: msg) do |corrector|
6260
current = node.loc.selector

lib/rubocop/cop/rspec/empty_hook.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ class EmptyHook < Base
2929
MSG = 'Empty hook detected.'
3030

3131
def on_block(node)
32-
return unless hook?(node)
33-
return unless node.body.nil?
32+
return unless block_pattern?(node, rspec_hooks)
33+
return if node.body
3434

3535
add_offense(node.send_node) do |corrector|
3636
range = range_with_surrounding_space(range: node.loc.expression)

lib/rubocop/cop/rspec/empty_line_after_hook.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class EmptyLineAfterHook < Base
4040
MSG = 'Add an empty line after `%<hook>s`.'
4141

4242
def on_block(node)
43-
return unless hook?(node)
43+
return unless block_pattern?(node, rspec_hooks)
4444

4545
missing_separating_line_offense(node) do |method|
4646
format(MSG, hook: method)

lib/rubocop/cop/rspec/expect_in_hook.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class ExpectInHook < Base
2626
def_node_search :expectation, Expectations::ALL.send_pattern
2727

2828
def on_block(node)
29-
return unless hook?(node)
29+
return unless block_pattern?(node, rspec_hooks)
3030
return if node.body.nil?
3131

3232
expectation(node.body) do |expect|

lib/rubocop/cop/rspec/expect_output.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ def on_gvasgn(node)
4242
def inside_example_scope?(node)
4343
return false if node.nil? || example_group?(node)
4444
return true if example?(node)
45-
return RuboCop::RSpec::Hook.new(node).example? if hook?(node)
45+
if block_pattern?(node, rspec_hooks)
46+
return RuboCop::RSpec::Hook.new(node).example?
47+
end
4648

4749
inside_example_scope?(node.parent)
4850
end

lib/rubocop/cop/rspec/hook_argument.rb

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,11 @@ class HookArgument < Base
6464
IMPLICIT_MSG = 'Omit the default `%<scope>p` argument for RSpec hooks.'
6565
EXPLICIT_MSG = 'Use `%<scope>p` for RSpec hooks.'
6666

67-
def_node_matcher :hook?, hook_selectors.node_pattern_union
67+
def_node_matcher :scoped_block, <<-PATTERN
68+
(block $(send _ %1 (sym ${:each :example})) ...)
69+
PATTERN
6870

69-
def_runtime_node_matcher :scoped_hook do
70-
<<-PATTERN
71-
(block $(send _ #hook? (sym ${:each :example})) ...)
72-
PATTERN
73-
end
74-
75-
def_runtime_node_matcher :unscoped_hook do
76-
"(block $(send _ #hook?) ...)"
77-
end
71+
def_node_matcher :unscoped_block, '(block $(send _ %1) ...)'
7872

7973
def on_block(node)
8074
hook(node) do |method_send, scope_name|
@@ -116,7 +110,8 @@ def implicit_style?
116110
end
117111

118112
def hook(node, &block)
119-
scoped_hook(node, &block) || unscoped_hook(node, &block)
113+
scoped_block(node, rspec_hooks, &block) ||
114+
unscoped_block(node, rspec_hooks, &block)
120115
end
121116

122117
def argument_range(send_node)

lib/rubocop/cop/rspec/hooks_before_examples.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def check_hooks(node)
5353

5454
node.each_child_node do |child|
5555
next if child.sibling_index < first_example.sibling_index
56-
next unless hook?(child)
56+
next unless block_pattern?(child, rspec_hooks)
5757

5858
msg = format(MSG, hook: child.method_name)
5959
add_offense(child, message: msg) do |corrector|

0 commit comments

Comments
 (0)