Skip to content

Commit

Permalink
POC usage of node pattern generic parameters
Browse files Browse the repository at this point in the history
#804 (comment)
Support introduced in rubocop/rubocop-ast#31
Presumably this will land in RuboCop 1.0 or 1.0.1 worst case.
  • Loading branch information
pirj committed Jun 11, 2020
1 parent 5d8f75d commit 678e5bc
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 10 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Expand Up @@ -4,6 +4,8 @@ source 'https://rubygems.org'

gemspec

gem 'rubocop-ast', github: 'marcandre/rubocop-ast', branch: 'nodepatternmatch'

local_gemfile = 'Gemfile.local'

if File.exist?(local_gemfile)
Expand Down
15 changes: 5 additions & 10 deletions lib/rubocop/cop/rspec/multiple_expectations.rb
Expand Up @@ -50,17 +50,12 @@ class MultipleExpectations < Cop

MSG = 'Example has too many expectations [%<total>d/%<max>d].'

def_node_matcher :aggregate_failures?, <<-PATTERN
(block {
(send _ _ <(sym :aggregate_failures) ...>)
(send _ _ ... (hash <(pair (sym :aggregate_failures) true) ...>))
} ...)
PATTERN
ANYTHING = ->(_) { true }

def_node_matcher :aggregate_failures_present?, <<-PATTERN
def_node_matcher :aggregate_failures?, <<-PATTERN
(block {
(send _ _ <(sym :aggregate_failures) ...>)
(send _ _ ... (hash <(pair (sym :aggregate_failures) _) ...>))
(send _ _ ... (hash <(pair (sym :aggregate_failures) %1) ...>))
} ...)
PATTERN

Expand Down Expand Up @@ -89,12 +84,12 @@ def example_with_aggregate_failures?(example_node)
node_with_aggregate_failures = find_aggregate_failures(example_node)
return false unless node_with_aggregate_failures

aggregate_failures?(node_with_aggregate_failures)
aggregate_failures?(node_with_aggregate_failures, :true_type?.to_proc)
end

def find_aggregate_failures(example_node)
example_node.send_node.each_ancestor(:block)
.find { |block_node| aggregate_failures_present?(block_node) }
.find { |block_node| aggregate_failures?(block_node, ANYTHING) }
end

def find_expectation(node, &block)
Expand Down
1 change: 1 addition & 0 deletions rubocop-rspec.gemspec
Expand Up @@ -37,6 +37,7 @@ Gem::Specification.new do |spec|
}

spec.add_runtime_dependency 'rubocop', '>= 0.68.1'
spec.add_runtime_dependency 'rubocop-ast'

spec.add_development_dependency 'rack'
spec.add_development_dependency 'rake'
Expand Down

0 comments on commit 678e5bc

Please sign in to comment.