From 3b41df361bf19973f1b17baebe245cfe26dbe244 Mon Sep 17 00:00:00 2001 From: Phil Pirozhkov Date: Thu, 11 Jun 2020 23:36:06 +0300 Subject: [PATCH] Use node pattern generic parameters to simplify cop 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 | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/lib/rubocop/cop/rspec/multiple_expectations.rb b/lib/rubocop/cop/rspec/multiple_expectations.rb index 2910d5213..31baa68a7 100644 --- a/lib/rubocop/cop/rspec/multiple_expectations.rb +++ b/lib/rubocop/cop/rspec/multiple_expectations.rb @@ -50,17 +50,13 @@ 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 = ->(_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 @@ -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)