From b3278847ac1301c01fc5f1c68814e455a03a46fb Mon Sep 17 00:00:00 2001 From: Robert Fletcher Date: Sun, 14 Jun 2020 11:11:39 -0700 Subject: [PATCH] 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]: https://github.com/rubocop-hq/rubocop-rspec/pull/863 --- lib/rubocop/cop/rspec/describe_class.rb | 2 -- lib/rubocop/cop/rspec/instance_variable.rb | 4 ---- lib/rubocop/cop/rspec/leaky_constant_declaration.rb | 5 +---- lib/rubocop/rspec/language/node_pattern.rb | 4 ++++ 4 files changed, 5 insertions(+), 10 deletions(-) diff --git a/lib/rubocop/cop/rspec/describe_class.rb b/lib/rubocop/cop/rspec/describe_class.rb index 059689830..6bdf4ffb9 100644 --- a/lib/rubocop/cop/rspec/describe_class.rb +++ b/lib/rubocop/cop/rspec/describe_class.rb @@ -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) diff --git a/lib/rubocop/cop/rspec/instance_variable.rb b/lib/rubocop/cop/rspec/instance_variable.rb index 3dfec9859..9ff31abd0 100644 --- a/lib/rubocop/cop/rspec/instance_variable.rb +++ b/lib/rubocop/cop/rspec/instance_variable.rb @@ -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 diff --git a/lib/rubocop/cop/rspec/leaky_constant_declaration.rb b/lib/rubocop/cop/rspec/leaky_constant_declaration.rb index 155aef735..6431eefbe 100644 --- a/lib/rubocop/cop/rspec/leaky_constant_declaration.rb +++ b/lib/rubocop/cop/rspec/leaky_constant_declaration.rb @@ -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 diff --git a/lib/rubocop/rspec/language/node_pattern.rb b/lib/rubocop/rspec/language/node_pattern.rb index 665b7ad70..de4c1da27 100644 --- a/lib/rubocop/rspec/language/node_pattern.rb +++ b/lib/rubocop/rspec/language/node_pattern.rb @@ -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?])