Skip to content

Commit

Permalink
Prepare _pattern abstraction for retirement
Browse files Browse the repository at this point in the history
  • Loading branch information
pirj committed Feb 10, 2023
1 parent 3fede0f commit ed70c1a
Show file tree
Hide file tree
Showing 24 changed files with 108 additions and 101 deletions.
2 changes: 1 addition & 1 deletion .simplecov
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

SimpleCov.start do
enable_coverage :branch
minimum_coverage line: 99.86, branch: 95.33
minimum_coverage line: 99.83, branch: 95.33
add_filter '/spec/'
add_filter '/vendor/bundle/'
end
6 changes: 3 additions & 3 deletions docs/modules/ROOT/pages/upgrade_to_version_2.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def_node_matcher :shared_context,
# After
def_node_matcher :shared_context,
block_pattern('#SharedGroups.context')
'(block (send #rspec? #SharedGroups.context ...) ...)'
----

[source,ruby]
Expand All @@ -173,7 +173,7 @@ def_node_search :examples?,
# After
def_node_search :examples?,
send_pattern('{#Includes.examples #Examples.all}')
'(send nil? {#Includes.examples #Examples.all} ...)'
----

[source,ruby]
Expand All @@ -184,7 +184,7 @@ def_node_search :find_rspec_blocks,
# After
def_node_search :find_rspec_blocks,
block_pattern('#ExampleGroups.all')
'(block (send #rspec? #ExampleGroups.all ...) ...)'
----

If you were calling Language elements directly, you have to make the same adjustments:
Expand Down
3 changes: 2 additions & 1 deletion lib/rubocop/cop/rspec/described_class.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ class DescribedClass < Base
PATTERN

# @!method rspec_block?(node)
def_node_matcher :rspec_block?, block_pattern('#ALL.all')
def_node_matcher :rspec_block?,
'({block numblock} (send #rspec? #ALL.all ...) ...)'

# @!method scope_changing_syntax?(node)
def_node_matcher :scope_changing_syntax?, '{def class module}'
Expand Down
4 changes: 3 additions & 1 deletion lib/rubocop/cop/rspec/described_class_module_wrapping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ class DescribedClassModuleWrapping < Base
MSG = 'Avoid opening modules and defining specs within them.'

# @!method find_rspec_blocks(node)
def_node_search :find_rspec_blocks, block_pattern('#ExampleGroups.all')
def_node_search :find_rspec_blocks, <<~PATTERN
(block (send #explicit_rspec? #ExampleGroups.all ...) ...)
PATTERN

def on_module(node)
find_rspec_blocks(node) do
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/dialect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Dialect < Base
MSG = 'Prefer `%<prefer>s` over `%<current>s`.'

# @!method rspec_method?(node)
def_node_matcher :rspec_method?, send_pattern('#ALL.all')
def_node_matcher :rspec_method?, '(send #rspec? #ALL.all ...)'

def on_send(node)
return unless rspec_method?(node)
Expand Down
12 changes: 6 additions & 6 deletions lib/rubocop/cop/rspec/empty_example_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class EmptyExampleGroup < Base
# @param node [RuboCop::AST::Node]
# @yield [RuboCop::AST::Node] example group body
def_node_matcher :example_group_body, <<~PATTERN
(block #{send_pattern('#ExampleGroups.all')} args $_)
(block (send #rspec? #ExampleGroups.all ...) args $_)
PATTERN

# @!method example_or_group_or_include?(node)
Expand All @@ -72,10 +72,10 @@ class EmptyExampleGroup < Base
# @return [Array<RuboCop::AST::Node>] matching nodes
def_node_matcher :example_or_group_or_include?, <<~PATTERN
{
#{block_pattern(
'{#Examples.all #ExampleGroups.all #Includes.all}'
)}
#{send_pattern('{#Examples.all #Includes.all}')}
(block
(send #rspec? {#Examples.all #ExampleGroups.all #Includes.all} ...)
...)
(send nil? {#Examples.all #Includes.all} ...)
}
PATTERN

Expand All @@ -95,7 +95,7 @@ class EmptyExampleGroup < Base
# @param node [RuboCop::AST::Node]
# @return [Array<RuboCop::AST::Node>] matching nodes
def_node_matcher :examples_inside_block?, <<~PATTERN
(block !#{send_pattern('#Hooks.all')} _ #examples?)
(block !(send nil? #Hooks.all ...) _ #examples?)
PATTERN

# @!method examples_directly_or_in_block?(node)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/empty_hook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class EmptyHook < Base

# @!method empty_hook?(node)
def_node_matcher :empty_hook?, <<~PATTERN
(block $#{send_pattern('#Hooks.all')} _ nil?)
(block $(send nil? #Hooks.all ...) _ nil?)
PATTERN

def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/expect_in_hook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ExpectInHook < Base
MSG = 'Do not use `%<expect>s` in `%<hook>s` hook'

# @!method expectation(node)
def_node_search :expectation, send_pattern('#Expectations.all')
def_node_search :expectation, '(send nil? #Expectations.all ...)'

def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
return unless hook?(node)
Expand Down
7 changes: 3 additions & 4 deletions lib/rubocop/cop/rspec/focus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,9 @@ class Focus < Base
PATTERN

# @!method focused_block?(node)
def_node_matcher :focused_block?,
send_pattern(<<~PATTERN)
{#ExampleGroups.focused #Examples.focused}
PATTERN
def_node_matcher :focused_block?, <<~PATTERN
(send #rspec? {#ExampleGroups.focused #Examples.focused} ...)
PATTERN

def on_send(node)
focus_metadata(node) do |focus|
Expand Down
7 changes: 5 additions & 2 deletions lib/rubocop/cop/rspec/hooks_before_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ class HooksBeforeExamples < Base
# @!method example_or_group?(node)
def_node_matcher :example_or_group?, <<-PATTERN
{
#{block_or_numblock_pattern('{#ExampleGroups.all #Examples.all}')}
#{send_pattern('#Includes.examples')}
({block numblock} {
(send #rspec? #ExampleGroups.all ...)
(send nil? #Examples.all ...)
} ...)
(send nil? #Includes.examples ...)
}
PATTERN

Expand Down
8 changes: 4 additions & 4 deletions lib/rubocop/cop/rspec/let_before_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ class LetBeforeExamples < Base
# @!method example_or_group?(node)
def_node_matcher :example_or_group?, <<-PATTERN
{
#{block_pattern('{#ExampleGroups.all #Examples.all}')}
#{send_pattern('#Includes.examples')}
(block (send nil? {#ExampleGroups.all #Examples.all} ...) ...)
(send nil? #Includes.examples ...)
}
PATTERN

# @!method include_examples?(node)
def_node_matcher :include_examples?, <<~PATTERN
{
#{block_pattern(':include_examples')}
#{send_pattern(':include_examples')}
(block (send nil? :include_examples ...) ...)
(send nil? :include_examples ...)
}
PATTERN

Expand Down
14 changes: 6 additions & 8 deletions lib/rubocop/cop/rspec/let_setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,12 @@ class LetSetup < Base
MSG = 'Do not use `let!` to setup objects not referenced in tests.'

# @!method example_or_shared_group_or_including?(node)
def_node_matcher :example_or_shared_group_or_including?,
block_pattern(<<~PATTERN)
{
#SharedGroups.all
#ExampleGroups.all
#Includes.all
}
PATTERN
def_node_matcher :example_or_shared_group_or_including?, <<~PATTERN
(block {
(send #rspec? {#SharedGroups.all #ExampleGroups.all} ...)
(send nil? #Includes.all ...)
} ...)
PATTERN

# @!method let_bang(node)
def_node_matcher :let_bang, <<-PATTERN
Expand Down
3 changes: 2 additions & 1 deletion lib/rubocop/cop/rspec/multiple_expectations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ class MultipleExpectations < Base
PATTERN

# @!method expect?(node)
def_node_matcher :expect?, send_pattern('#Expectations.all')
def_node_matcher :expect?, '(send nil? #Expectations.all ...)'

# @!method aggregate_failures_block?(node)
def_node_matcher :aggregate_failures_block?, <<-PATTERN
(block (send nil? :aggregate_failures ...) ...)
Expand Down
10 changes: 6 additions & 4 deletions lib/rubocop/cop/rspec/named_subject.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,14 @@ class NamedSubject < Base
MSG = 'Name your test subject if you need to reference it explicitly.'

# @!method example_or_hook_block?(node)
def_node_matcher :example_or_hook_block?,
block_pattern('{#Examples.all #Hooks.all}')
def_node_matcher :example_or_hook_block?, <<~PATTERN
(block (send nil? {#Examples.all #Hooks.all} ...) ...)
PATTERN

# @!method shared_example?(node)
def_node_matcher :shared_example?,
block_pattern('#SharedGroups.examples')
def_node_matcher :shared_example?, <<~PATTERN
(block (send #rspec? #SharedGroups.examples ...) ...)
PATTERN

# @!method subject_usage(node)
def_node_search :subject_usage, '$(send nil? :subject)'
Expand Down
9 changes: 4 additions & 5 deletions lib/rubocop/cop/rspec/no_expectation_example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,16 @@ class NoExpectationExample < Base
# @!method regular_or_focused_example?(node)
# @param [RuboCop::AST::Node] node
# @return [Boolean]
def_node_matcher :regular_or_focused_example?,
block_or_numblock_pattern(
'{#Examples.regular | #Examples.focused}'
)
def_node_matcher :regular_or_focused_example?, <<~PATTERN
({block numblock} (send nil? {#Examples.regular #Examples.focused} ...) ...)
PATTERN

# @!method includes_expectation?(node)
# @param [RuboCop::AST::Node] node
# @return [Boolean]
def_node_search :includes_expectation?, <<~PATTERN
{
#{send_pattern('#Expectations.all')}
(send nil? #Expectations.all ...)
(send nil? `#matches_allowed_pattern? ...)
}
PATTERN
Expand Down
4 changes: 3 additions & 1 deletion lib/rubocop/cop/rspec/overwriting_setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ class OverwritingSetup < Base
MSG = '`%<name>s` is already defined.'

# @!method setup?(node)
def_node_matcher :setup?, block_pattern('{#Helpers.all #Subjects.all}')
def_node_matcher :setup?, <<~PATTERN
(block (send nil? {#Helpers.all #Subjects.all} ...) ...)
PATTERN

# @!method first_argument_name(node)
def_node_matcher :first_argument_name, '(send _ _ ({str sym} $_))'
Expand Down
24 changes: 12 additions & 12 deletions lib/rubocop/cop/rspec/pending.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,20 @@ class Pending < Base
MSG = 'Pending spec found.'

# @!method skippable?(node)
def_node_matcher :skippable?,
send_pattern(<<~PATTERN)
{#ExampleGroups.regular #Examples.regular}
PATTERN
def_node_matcher :skippable?, <<~PATTERN
{
(send #rspec? #ExampleGroups.regular ...)
(send nil? #Examples.regular ...)
}
PATTERN

# @!method pending_block?(node)
def_node_matcher :pending_block?,
send_pattern(<<~PATTERN)
{
#ExampleGroups.skipped
#Examples.skipped
#Examples.pending
}
PATTERN
def_node_matcher :pending_block?, <<~PATTERN
{
(send #rspec? #ExampleGroups.skipped ...)
(send nil? {#Examples.skipped #Examples.pending} ...)
}
PATTERN

def on_send(node)
return unless pending_block?(node) || skipped?(node)
Expand Down
7 changes: 3 additions & 4 deletions lib/rubocop/cop/rspec/repeated_include_example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,11 @@ class RepeatedIncludeExample < Base

# @!method include_examples?(node)
def_node_matcher :include_examples?,
send_pattern('#Includes.examples')
'(send nil? #Includes.examples ...)'

# @!method shared_examples_name(node)
def_node_matcher :shared_examples_name, <<-PATTERN
(send _ #Includes.examples $_ ...)
PATTERN
def_node_matcher :shared_examples_name,
'(send nil? #Includes.examples $_name ...)'

def on_begin(node)
return unless several_include_examples?(node)
Expand Down
25 changes: 12 additions & 13 deletions lib/rubocop/cop/rspec/shared_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,27 +57,26 @@ class SharedContext < Base
MSG_CONTEXT = "Use `shared_context` when you don't define examples."

# @!method examples?(node)
def_node_search :examples?,
send_pattern('{#Includes.examples #Examples.all}')
def_node_search :examples?, <<~PATTERN
(send nil? {#Includes.examples #Examples.all} ...)
PATTERN

# @!method context?(node)
def_node_search :context?, <<-PATTERN
(
send #rspec? {
#Subjects.all
#Helpers.all
#Includes.context
#Hooks.all
} ...
(send nil?
{#Subjects.all #Helpers.all #Includes.context #Hooks.all} ...
)
PATTERN

# @!method shared_context(node)
def_node_matcher :shared_context,
block_pattern('#SharedGroups.context')
def_node_matcher :shared_context, <<~PATTERN
(block (send #rspec? #SharedGroups.context ...) ...)
PATTERN

# @!method shared_example(node)
def_node_matcher :shared_example,
block_pattern('#SharedGroups.examples')
def_node_matcher :shared_example, <<~PATTERN
(block (send #rspec? #SharedGroups.examples ...) ...)
PATTERN

def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
context_with_only_examples(node) do
Expand Down
10 changes: 6 additions & 4 deletions lib/rubocop/cop/rspec/shared_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ class SharedExamples < Base
extend AutoCorrector

# @!method shared_examples(node)
def_node_matcher :shared_examples,
send_pattern(
'{#SharedGroups.all #Includes.all}'
)
def_node_matcher :shared_examples, <<~PATTERN
{
(send #rspec? #SharedGroups.all ...)
(send nil? #Includes.all ...)
}
PATTERN

def on_send(node)
shared_examples(node) do
Expand Down
14 changes: 6 additions & 8 deletions lib/rubocop/rspec/example_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ class ExampleGroup < Concept
#
# Selectors which indicate that we should stop searching
#
def_node_matcher :scope_change?,
block_pattern(<<~PATTERN)
{
#SharedGroups.all
#ExampleGroups.all
#Includes.all
}
PATTERN
def_node_matcher :scope_change?, <<~PATTERN
(block {
(send #rspec? {#SharedGroups.all #ExampleGroups.all} ...)
(send nil? #Includes.all ...)
} ...)
PATTERN

def lets
find_all_in_scope(node, :let?)
Expand Down
Loading

0 comments on commit ed70c1a

Please sign in to comment.