From 97f1b5896e9072e8410c0ed65bb9132fd777e626 Mon Sep 17 00:00:00 2001 From: Andrew Konchin Date: Sun, 28 Jun 2020 04:28:23 +0300 Subject: [PATCH] FactoryBot/AttributeDefinedStatically. Optimize #on_block callback --- .../cop/rspec/factory_bot/attribute_defined_statically.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/rubocop/cop/rspec/factory_bot/attribute_defined_statically.rb b/lib/rubocop/cop/rspec/factory_bot/attribute_defined_statically.rb index 863eb401f..b081d0ff7 100644 --- a/lib/rubocop/cop/rspec/factory_bot/attribute_defined_statically.rb +++ b/lib/rubocop/cop/rspec/factory_bot/attribute_defined_statically.rb @@ -31,12 +31,15 @@ class AttributeDefinedStatically < Cop (send _ !#reserved_method? $...) PATTERN - def_node_search :factory_attributes, <<-PATTERN + def_node_matcher :factory_attributes, <<-PATTERN (block (send _ #attribute_defining_method? ...) _ { (begin $...) $(send ...) } ) PATTERN def on_block(node) - factory_attributes(node).to_a.flatten.each do |attribute| + attributes = factory_attributes(node) || [] + attributes = [attributes] unless attributes.is_a?(Array) + + attributes.each do |attribute| next unless offensive_receiver?(attribute.receiver, node) next if proc?(attribute) || association?(attribute.first_argument)