Skip to content

Commit

Permalink
Use node pattern generic parameters to simplify cop
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 Jul 15, 2020
1 parent 5d4a1be commit 3b41df3
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions lib/rubocop/cop/rspec/multiple_expectations.rb
Expand Up @@ -50,17 +50,13 @@ 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 = ->(_node) { true }
TRUE = ->(node) { node.true_type? }

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 +85,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)
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

0 comments on commit 3b41df3

Please sign in to comment.