Skip to content

Commit

Permalink
Use node groups in node patterns to replace unions of types
Browse files Browse the repository at this point in the history
`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`.
  • Loading branch information
dvandersluis committed Jan 27, 2025
1 parent 6ddeea4 commit 5c87552
Show file tree
Hide file tree
Showing 12 changed files with 16 additions and 17 deletions.
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/be_eq.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/be_eql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/described_class.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}'
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/described_class_module_wrapping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions lib/rubocop/cop/rspec/hook_argument.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/hooks_before_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 ...)
} ...)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/no_expectation_example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions lib/rubocop/cop/rspec/pending_without_reason.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/predicate_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/redundant_around.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 3 additions & 4 deletions lib/rubocop/rspec/language.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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

Expand Down
1 change: 1 addition & 0 deletions rubocop-rspec.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 5c87552

Please sign in to comment.