Skip to content

Commit

Permalink
Merge pull request #2046 from rubocop/use-new-ast-features
Browse files Browse the repository at this point in the history
Use new ast features
  • Loading branch information
bquorning authored Feb 16, 2025
2 parents 14cbb9b + dd865ae commit cd91408
Show file tree
Hide file tree
Showing 14 changed files with 19 additions and 45 deletions.
21 changes: 0 additions & 21 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,6 @@
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# This cop supports safe autocorrection (--autocorrect).
InternalAffairs/NodePatternGroups:
Exclude:
- 'lib/rubocop/cop/rspec/described_class.rb'
- 'lib/rubocop/cop/rspec/described_class_module_wrapping.rb'
- 'lib/rubocop/cop/rspec/hook_argument.rb'
- 'lib/rubocop/cop/rspec/hooks_before_examples.rb'
- 'lib/rubocop/cop/rspec/no_expectation_example.rb'
- 'lib/rubocop/cop/rspec/pending_without_reason.rb'
- 'lib/rubocop/cop/rspec/redundant_around.rb'
- 'lib/rubocop/rspec/language.rb'

# This cop supports safe autocorrection (--autocorrect).
InternalAffairs/NodeTypeMultiplePredicates:
Exclude:
- 'lib/rubocop/cop/rspec/empty_example_group.rb'
- 'lib/rubocop/cop/rspec/predicate_matcher.rb'
- 'lib/rubocop/cop/rspec/receive_messages.rb'
- 'lib/rubocop/cop/rspec/variable_definition.rb'
- 'lib/rubocop/cop/rspec/variable_name.rb'

Rake/MethodDefinitionInTask:
Exclude:
- 'tasks/cut_release.rake'
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
6 changes: 3 additions & 3 deletions lib/rubocop/cop/rspec/empty_example_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,15 @@ def offensive?(body)
return true unless body
return false if conditionals_with_examples?(body)

if body.if_type? || body.case_type?
if body.type?(:if, :case)
!examples_in_branches?(body)
else
!examples?(body)
end
end

def conditionals_with_examples?(body)
return false unless body.begin_type? || body.case_type?
return false unless body.type?(:begin, :case)

body.each_descendant(:if, :case).any? do |condition_node|
examples_in_branches?(condition_node)
Expand All @@ -172,7 +172,7 @@ def conditionals_with_examples?(body)

def examples_in_branches?(condition_node)
return false unless condition_node
return false if !condition_node.if_type? && !condition_node.case_type?
return false unless condition_node.type?(:if, :case)

condition_node.branches.any? { |branch| examples?(branch) }
end
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 @@ -182,7 +182,7 @@ def uncorrectable_matcher?(node, matcher)

def heredoc_argument?(matcher)
matcher.arguments.select do |arg|
arg.str_type? || arg.dstr_type? || arg.xstr_type?
arg.type?(:str, :dstr, :xstr)
end.any?(&:heredoc?)
end

Expand Down
3 changes: 1 addition & 2 deletions lib/rubocop/cop/rspec/receive_messages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,7 @@ def item_range_by_whole_lines(item)
end

def heredoc_or_splat?(node)
((node.str_type? || node.dstr_type?) && node.heredoc?) ||
node.splat_type?
(node.type?(:str, :dstr) && node.heredoc?) || node.splat_type?
end

def requires_quotes?(value)
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
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/variable_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def string?(node)
end

def symbol?(node)
node.sym_type? || node.dsym_type?
node.type?(:sym, :dsym)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rspec/variable_name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def on_send(node)
return unless inside_example_group?(node)

variable_definition?(node) do |variable|
return if variable.dstr_type? || variable.dsym_type?
return if variable.type?(:dstr, :dsym)
return if matches_allowed_pattern?(variable.value)

check_name(node, variable.value, variable.source_range)
Expand Down
9 changes: 3 additions & 6 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 @@ -50,10 +50,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

# @!method let?(node)
Expand Down

0 comments on commit cd91408

Please sign in to comment.