Skip to content

Commit

Permalink
Pull example group node matchers up
Browse files Browse the repository at this point in the history
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]: rubocop#863
  • Loading branch information
mockdeep committed Jun 14, 2020
1 parent 953fcf6 commit ba6f8ff
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 10 deletions.
2 changes: 0 additions & 2 deletions lib/rubocop/cop/rspec/describe_class.rb
Expand Up @@ -51,8 +51,6 @@ class DescribeClass < Cop
)
PATTERN

def_node_matcher :shared_group?, SharedGroups::ALL.block_pattern

def on_top_level_describe(node, (described_value, _))
return if shared_group?(root_node)
return if valid_describe?(node)
Expand Down
4 changes: 0 additions & 4 deletions lib/rubocop/cop/rspec/instance_variable.rb
Expand Up @@ -50,10 +50,6 @@ class InstanceVariable < Cop
MSG = 'Avoid instance variables – use let, ' \
'a method call, or a local variable (if possible).'

EXAMPLE_GROUP_METHODS = ExampleGroups::ALL + SharedGroups::ALL

def_node_matcher :spec_group?, EXAMPLE_GROUP_METHODS.block_pattern

def_node_matcher :dynamic_class?, <<-PATTERN
(block (send (const nil? :Class) :new ...) ...)
PATTERN
Expand Down
5 changes: 1 addition & 4 deletions lib/rubocop/cop/rspec/leaky_constant_declaration.rb
Expand Up @@ -119,11 +119,8 @@ def on_module(node)
private

def inside_describe_block?(node)
node.each_ancestor(:block).any?(&method(:in_example_or_shared_group?))
node.each_ancestor(:block).any?(&method(:spec_group?))
end

def_node_matcher :in_example_or_shared_group?,
(ExampleGroups::ALL + SharedGroups::ALL).block_pattern
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/rubocop/rspec/language/node_pattern.rb
Expand Up @@ -8,6 +8,10 @@ module NodePattern
extend RuboCop::NodePattern::Macros

def_node_matcher :example_group?, ExampleGroups::ALL.block_pattern
def_node_matcher :shared_group?, SharedGroups::ALL.block_pattern

spec_groups = ExampleGroups::ALL + SharedGroups::ALL
def_node_matcher :spec_group?, spec_groups.block_pattern

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

0 comments on commit ba6f8ff

Please sign in to comment.