Skip to content

Commit b327884

Browse files
committed
Pull example group node matchers up
This refactors to add `shared_group?` and `spec_group?` node matchers in `NodePattern` for use across cops. I wanted to check shared contexts in the [rule that I'm working on][1] and noticed that there were similar implementations in a handful of other places. I thought it was probably worth abstracting upwards. [1]: #863
1 parent e970b6a commit b327884

File tree

4 files changed

+5
-10
lines changed

4 files changed

+5
-10
lines changed

lib/rubocop/cop/rspec/describe_class.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ class DescribeClass < Cop
5151
)
5252
PATTERN
5353

54-
def_node_matcher :shared_group?, SharedGroups::ALL.block_pattern
55-
5654
def on_top_level_describe(node, (described_value, _))
5755
return if shared_group?(root_node)
5856
return if valid_describe?(node)

lib/rubocop/cop/rspec/instance_variable.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,6 @@ class InstanceVariable < Cop
5050
MSG = 'Avoid instance variables – use let, ' \
5151
'a method call, or a local variable (if possible).'
5252

53-
EXAMPLE_GROUP_METHODS = ExampleGroups::ALL + SharedGroups::ALL
54-
55-
def_node_matcher :spec_group?, EXAMPLE_GROUP_METHODS.block_pattern
56-
5753
def_node_matcher :dynamic_class?, <<-PATTERN
5854
(block (send (const nil? :Class) :new ...) ...)
5955
PATTERN

lib/rubocop/cop/rspec/leaky_constant_declaration.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,8 @@ def on_module(node)
119119
private
120120

121121
def inside_describe_block?(node)
122-
node.each_ancestor(:block).any?(&method(:in_example_or_shared_group?))
122+
node.each_ancestor(:block).any?(&method(:spec_group?))
123123
end
124-
125-
def_node_matcher :in_example_or_shared_group?,
126-
(ExampleGroups::ALL + SharedGroups::ALL).block_pattern
127124
end
128125
end
129126
end

lib/rubocop/rspec/language/node_pattern.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ module NodePattern
88
extend RuboCop::NodePattern::Macros
99

1010
def_node_matcher :example_group?, ExampleGroups::ALL.block_pattern
11+
def_node_matcher :shared_group?, SharedGroups::ALL.block_pattern
12+
13+
spec_groups = ExampleGroups::ALL + SharedGroups::ALL
14+
def_node_matcher :spec_group?, spec_groups.block_pattern
1115

1216
def_node_matcher :example_group_with_body?, <<-PATTERN
1317
(block #{ExampleGroups::ALL.send_pattern} args [!nil?])

0 commit comments

Comments
 (0)