From 5c87552ca6c8472acbb13065346a5d57686f3698 Mon Sep 17 00:00:00 2001 From: Daniel Vandersluis Date: Mon, 27 Jan 2025 15:51:21 -0500 Subject: [PATCH] Use node groups in node patterns to replace unions of types `rubocop-ast` defines some node groups (https://github.com/rubocop/rubocop-ast/blob/85bfe84/lib/rubocop/ast/node.rb#L89-L116) that can be used in place of a union of node types. rubocop-ast v1.38.0 is needed for `any_block`. --- lib/rubocop/cop/rspec/be_eq.rb | 2 +- lib/rubocop/cop/rspec/be_eql.rb | 2 +- lib/rubocop/cop/rspec/described_class.rb | 2 +- lib/rubocop/cop/rspec/described_class_module_wrapping.rb | 2 +- lib/rubocop/cop/rspec/hook_argument.rb | 4 ++-- lib/rubocop/cop/rspec/hooks_before_examples.rb | 2 +- lib/rubocop/cop/rspec/no_expectation_example.rb | 2 +- lib/rubocop/cop/rspec/pending_without_reason.rb | 5 ++--- lib/rubocop/cop/rspec/predicate_matcher.rb | 2 +- lib/rubocop/cop/rspec/redundant_around.rb | 2 +- lib/rubocop/rspec/language.rb | 7 +++---- rubocop-rspec.gemspec | 1 + 12 files changed, 16 insertions(+), 17 deletions(-) diff --git a/lib/rubocop/cop/rspec/be_eq.rb b/lib/rubocop/cop/rspec/be_eq.rb index 4616762be..dfdcd7c5a 100644 --- a/lib/rubocop/cop/rspec/be_eq.rb +++ b/lib/rubocop/cop/rspec/be_eq.rb @@ -31,7 +31,7 @@ class BeEq < Base # @!method eq_type_with_identity?(node) def_node_matcher :eq_type_with_identity?, <<~PATTERN - (send nil? :eq {true false nil}) + (send nil? :eq {boolean nil}) PATTERN def on_send(node) diff --git a/lib/rubocop/cop/rspec/be_eql.rb b/lib/rubocop/cop/rspec/be_eql.rb index be9628471..f49a69231 100644 --- a/lib/rubocop/cop/rspec/be_eql.rb +++ b/lib/rubocop/cop/rspec/be_eql.rb @@ -45,7 +45,7 @@ class BeEql < Base # @!method eql_type_with_identity(node) def_node_matcher :eql_type_with_identity, <<~PATTERN - (send _ :to $(send nil? :eql {true false int float sym nil})) + (send _ :to $(send nil? :eql {boolean int float sym nil})) PATTERN def on_send(node) diff --git a/lib/rubocop/cop/rspec/described_class.rb b/lib/rubocop/cop/rspec/described_class.rb index 389e675c5..29a35cc68 100644 --- a/lib/rubocop/cop/rspec/described_class.rb +++ b/lib/rubocop/cop/rspec/described_class.rb @@ -83,7 +83,7 @@ class DescribedClass < Base # rubocop:disable Metrics/ClassLength # @!method rspec_block?(node) def_node_matcher :rspec_block?, - '({block numblock} (send #rspec? #ALL.all ...) ...)' + '(any_block (send #rspec? #ALL.all ...) ...)' # @!method scope_changing_syntax?(node) def_node_matcher :scope_changing_syntax?, '{def class module}' diff --git a/lib/rubocop/cop/rspec/described_class_module_wrapping.rb b/lib/rubocop/cop/rspec/described_class_module_wrapping.rb index 263bec673..9ea0a786d 100644 --- a/lib/rubocop/cop/rspec/described_class_module_wrapping.rb +++ b/lib/rubocop/cop/rspec/described_class_module_wrapping.rb @@ -24,7 +24,7 @@ class DescribedClassModuleWrapping < Base # @!method include_rspec_blocks?(node) def_node_search :include_rspec_blocks?, <<~PATTERN - ({block numblock} (send #explicit_rspec? #ExampleGroups.all ...) ...) + (any_block (send #explicit_rspec? #ExampleGroups.all ...) ...) PATTERN def on_module(node) diff --git a/lib/rubocop/cop/rspec/hook_argument.rb b/lib/rubocop/cop/rspec/hook_argument.rb index 36b2823aa..654dd036f 100644 --- a/lib/rubocop/cop/rspec/hook_argument.rb +++ b/lib/rubocop/cop/rspec/hook_argument.rb @@ -67,12 +67,12 @@ class HookArgument < Base # @!method scoped_hook(node) def_node_matcher :scoped_hook, <<~PATTERN - ({block numblock} $(send _ #Hooks.all (sym ${:each :example})) ...) + (any_block $(send _ #Hooks.all (sym ${:each :example})) ...) PATTERN # @!method unscoped_hook(node) def_node_matcher :unscoped_hook, <<~PATTERN - ({block numblock} $(send _ #Hooks.all) ...) + (any_block $(send _ #Hooks.all) ...) PATTERN def on_block(node) diff --git a/lib/rubocop/cop/rspec/hooks_before_examples.rb b/lib/rubocop/cop/rspec/hooks_before_examples.rb index 93fb96fa9..e8aeeebb5 100644 --- a/lib/rubocop/cop/rspec/hooks_before_examples.rb +++ b/lib/rubocop/cop/rspec/hooks_before_examples.rb @@ -30,7 +30,7 @@ class HooksBeforeExamples < Base # @!method example_or_group?(node) def_node_matcher :example_or_group?, <<~PATTERN { - ({block numblock} { + (any_block { (send #rspec? #ExampleGroups.all ...) (send nil? #Examples.all ...) } ...) diff --git a/lib/rubocop/cop/rspec/no_expectation_example.rb b/lib/rubocop/cop/rspec/no_expectation_example.rb index 5018a33cc..a800636df 100644 --- a/lib/rubocop/cop/rspec/no_expectation_example.rb +++ b/lib/rubocop/cop/rspec/no_expectation_example.rb @@ -65,7 +65,7 @@ class NoExpectationExample < Base # @param [RuboCop::AST::Node] node # @return [Boolean] def_node_matcher :regular_or_focused_example?, <<~PATTERN - ({block numblock} (send nil? {#Examples.regular #Examples.focused} ...) ...) + (any_block (send nil? {#Examples.regular #Examples.focused} ...) ...) PATTERN # @!method includes_expectation?(node) diff --git a/lib/rubocop/cop/rspec/pending_without_reason.rb b/lib/rubocop/cop/rspec/pending_without_reason.rb index 5f72bcd1f..eea8edaaa 100644 --- a/lib/rubocop/cop/rspec/pending_without_reason.rb +++ b/lib/rubocop/cop/rspec/pending_without_reason.rb @@ -63,8 +63,7 @@ class PendingWithoutReason < Base def_node_matcher :skipped_in_example?, <<~PATTERN { (send nil? ${#Examples.skipped #Examples.pending}) - (block (send nil? ${#Examples.skipped}) ...) - (numblock (send nil? ${#Examples.skipped}) ...) + (any_block (send nil? ${#Examples.skipped}) ...) } PATTERN @@ -75,7 +74,7 @@ class PendingWithoutReason < Base # @!method skipped_by_example_method_with_block?(node) def_node_matcher :skipped_by_example_method_with_block?, <<~PATTERN - ({block numblock} (send nil? ${#Examples.skipped #Examples.pending} ...) ...) + (any_block (send nil? ${#Examples.skipped #Examples.pending} ...) ...) PATTERN # @!method metadata_without_reason?(node) diff --git a/lib/rubocop/cop/rspec/predicate_matcher.rb b/lib/rubocop/cop/rspec/predicate_matcher.rb index a9b51e262..28ed70eb0 100644 --- a/lib/rubocop/cop/rspec/predicate_matcher.rb +++ b/lib/rubocop/cop/rspec/predicate_matcher.rb @@ -37,7 +37,7 @@ def check_inflected(node) # @!method be_bool?(node) def_node_matcher :be_bool?, <<~PATTERN - (send nil? {:be :eq :eql :equal} {true false}) + (send nil? {:be :eq :eql :equal} boolean) PATTERN # @!method be_boolthy?(node) diff --git a/lib/rubocop/cop/rspec/redundant_around.rb b/lib/rubocop/cop/rspec/redundant_around.rb index f943d2b66..7522c9ce0 100644 --- a/lib/rubocop/cop/rspec/redundant_around.rb +++ b/lib/rubocop/cop/rspec/redundant_around.rb @@ -41,7 +41,7 @@ def on_send(node) # @!method match_redundant_around_hook_block?(node) def_node_matcher :match_redundant_around_hook_block?, <<~PATTERN - ({block numblock} (send _ :around ...) ... (send _ :run)) + (any_block (send _ :around ...) ... (send _ :run)) PATTERN # @!method match_redundant_around_hook_send?(node) diff --git a/lib/rubocop/rspec/language.rb b/lib/rubocop/rspec/language.rb index dff05435c..6238106a9 100644 --- a/lib/rubocop/rspec/language.rb +++ b/lib/rubocop/rspec/language.rb @@ -26,7 +26,7 @@ class << self # @!method example_group?(node) def_node_matcher :example_group?, <<~PATTERN - ({block numblock} (send #rspec? #ExampleGroups.all ...) ...) + (any_block (send #rspec? #ExampleGroups.all ...) ...) PATTERN # @!method shared_group?(node) @@ -35,7 +35,7 @@ class << self # @!method spec_group?(node) def_node_matcher :spec_group?, <<~PATTERN - ({block numblock} (send #rspec? + (any_block (send #rspec? {#SharedGroups.all #ExampleGroups.all} ...) ...) PATTERN @@ -51,8 +51,7 @@ class << self # @!method hook?(node) def_node_matcher :hook?, <<~PATTERN { - (numblock (send nil? #Hooks.all ...) ...) - (block (send nil? #Hooks.all ...) ...) + (any_block (send nil? #Hooks.all ...) ...) } PATTERN diff --git a/rubocop-rspec.gemspec b/rubocop-rspec.gemspec index 6b1ada365..799e523cd 100644 --- a/rubocop-rspec.gemspec +++ b/rubocop-rspec.gemspec @@ -38,4 +38,5 @@ Gem::Specification.new do |spec| } spec.add_dependency 'rubocop', '~> 1.61' + spec.add_dependency 'rubocop-ast', '>= 1.38.0', '< 2.0' end