From 15e5e3410242bbe7b44e4e08e03150dfc0557a74 Mon Sep 17 00:00:00 2001 From: Yauheni Dakuka Date: Thu, 20 Feb 2025 13:01:40 +0400 Subject: [PATCH] [Fix rubocop#79] Fix false positives for `FactoryBot/AssociationStyle` with `strategy: :create` option. --- CHANGELOG.md | 2 ++ .../cop/factory_bot/association_style.rb | 7 ++++++ .../cop/factory_bot/association_style_spec.rb | 23 +++++++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1139d483..cbd59187 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Master (Unreleased) - Fix a false positive for `FactoryBot/ConsistentParenthesesStyle` when using traits and omitting hash values. ([@thejonroberts]) +- Fix false positives for `FactoryBot/AssociationStyle` with `strategy: :create` option. ([@ydakuka]) ## 2.26.1 (2024-06-12) @@ -117,3 +118,4 @@ [@walf443]: https://github.com/walf443 [@ybiquitous]: https://github.com/ybiquitous [@ydah]: https://github.com/ydah +[@ydakuka]: https://github.com/ydakuka diff --git a/lib/rubocop/cop/factory_bot/association_style.rb b/lib/rubocop/cop/factory_bot/association_style.rb index 4a08f46d..712fb140 100644 --- a/lib/rubocop/cop/factory_bot/association_style.rb +++ b/lib/rubocop/cop/factory_bot/association_style.rb @@ -105,6 +105,11 @@ def on_send(node) ) PATTERN + # @!method strategy_create_pair?(node) + def_node_matcher :strategy_create_pair?, <<~PATTERN + (pair (sym :strategy) (sym :create)) + PATTERN + # @!method implicit_association?(node) def_node_matcher :implicit_association?, <<~PATTERN (send nil? !#non_implicit_association_method_name? ...) @@ -234,6 +239,8 @@ def options_from_explicit(node) return {} unless node.last_argument.hash_type? node.last_argument.pairs.inject({}) do |options, pair| + next options if strategy_create_pair?(pair) + options.merge(pair.key.value => pair.value.source) end end diff --git a/spec/rubocop/cop/factory_bot/association_style_spec.rb b/spec/rubocop/cop/factory_bot/association_style_spec.rb index 874cfbd3..6d64ebd7 100644 --- a/spec/rubocop/cop/factory_bot/association_style_spec.rb +++ b/spec/rubocop/cop/factory_bot/association_style_spec.rb @@ -178,6 +178,29 @@ def inspected_source_filename end end + context 'with `strategy: :create` option' do + it 'registers an offense' do + expect_offense(<<~RUBY) + factory :article do + association :user, strategy: :create + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use implicit style to define associations. + association :reviewer, factory: :user, strategy: :create + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use implicit style to define associations. + association :tag, :pop, strategy: :create + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use implicit style to define associations. + end + RUBY + + expect_correction(<<~RUBY) + factory :article do + user + reviewer factory: %i[user] + tag factory: %i[tag pop] + end + RUBY + end + end + context 'when `association` is called in trait block ' \ 'and column name is keyword' do it 'does not register an offense' do