From 1a47e9e0bc011cd4e9af0531940a14146be9acfc Mon Sep 17 00:00:00 2001 From: Phil Pirozhkov Date: Thu, 11 Jun 2020 23:36:06 +0300 Subject: [PATCH] POC usage of node pattern generic parameters https://github.com/rubocop-hq/rubocop-rspec/pull/804#discussion_r438747249 Support introduced in https://github.com/rubocop-hq/rubocop-ast/pull/31 Presumably this will land in RuboCop 1.0 or 1.0.1 worst case. --- lib/rubocop/cop/rspec/multiple_expectations.rb | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/lib/rubocop/cop/rspec/multiple_expectations.rb b/lib/rubocop/cop/rspec/multiple_expectations.rb index 2910d5213..184ddcafd 100644 --- a/lib/rubocop/cop/rspec/multiple_expectations.rb +++ b/lib/rubocop/cop/rspec/multiple_expectations.rb @@ -50,17 +50,12 @@ class MultipleExpectations < Cop MSG = 'Example has too many expectations [%d/%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 @@ -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)