From 3d908ff4ac032011cc30898dad44a9ce93fbef88 Mon Sep 17 00:00:00 2001 From: Phil Pirozhkov Date: Sun, 11 Aug 2019 23:35:46 +0300 Subject: [PATCH] Use children matching in AttributeDefinedStatically cop --- .../rspec/factory_bot/attribute_defined_statically.rb | 11 ++--------- 1 file changed, 2 insertions(+), 9 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 6d2d37e19..863eb401f 100644 --- a/lib/rubocop/cop/rspec/factory_bot/attribute_defined_statically.rb +++ b/lib/rubocop/cop/rspec/factory_bot/attribute_defined_statically.rb @@ -38,7 +38,7 @@ class AttributeDefinedStatically < Cop def on_block(node) factory_attributes(node).to_a.flatten.each do |attribute| next unless offensive_receiver?(attribute.receiver, node) - next if proc?(attribute) || association?(attribute) + next if proc?(attribute) || association?(attribute.first_argument) add_offense(attribute) end @@ -72,14 +72,7 @@ def proc?(attribute) value_matcher(attribute).to_a.all?(&:block_pass_type?) end - def association?(attribute) - argument = attribute.first_argument - argument.hash_type? && factory_key?(argument) - end - - def factory_key?(hash_node) - hash_node.keys.any? { |key| key.sym_type? && key.value == :factory } - end + def_node_matcher :association?, '(hash <(pair (sym :factory) _) ...>)' def autocorrect_replacing_parens(node) left_braces, right_braces = braces(node)