From 85bae8d0c782ba1b467d63e928a86f767ade9472 Mon Sep 17 00:00:00 2001 From: Yudai Takada Date: Fri, 5 Jan 2024 23:37:46 +0900 Subject: [PATCH] Fix a false positive for `FactoryBot/CreateList` (#97) Fix: https://github.com/rubocop/rubocop-factory_bot/issues/87 --- CHANGELOG.md | 2 ++ lib/rubocop/cop/factory_bot/create_list.rb | 2 +- .../cop/factory_bot/create_list_spec.rb | 18 +++++++----------- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fd7ff9fb..dbcea6ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Master (Unreleased) +- Fix a false positive for `FactoryBot/CreateList` when create call does have method calls and repeat multiple times with other argument. ([@ydah]) + ## 2.25.0 (2024-01-04) - Fix a false positive for `FactoryBot/FactoryNameStyle` when namespaced models. ([@ydah]) diff --git a/lib/rubocop/cop/factory_bot/create_list.rb b/lib/rubocop/cop/factory_bot/create_list.rb index fee0c430..1fc95aef 100644 --- a/lib/rubocop/cop/factory_bot/create_list.rb +++ b/lib/rubocop/cop/factory_bot/create_list.rb @@ -90,7 +90,7 @@ class CreateList < ::RuboCop::Cop::Base # rubocop:disable Metrics/ClassLength # @!method arguments_include_method_call?(node) def_node_matcher :arguments_include_method_call?, <<~PATTERN - (send #factory_call? :create sym `(send ...)) + (send #factory_call? :create sym ... `(send ...)) PATTERN # @!method factory_call(node) diff --git a/spec/rubocop/cop/factory_bot/create_list_spec.rb b/spec/rubocop/cop/factory_bot/create_list_spec.rb index 44506fdd..f7c50080 100644 --- a/spec/rubocop/cop/factory_bot/create_list_spec.rb +++ b/spec/rubocop/cop/factory_bot/create_list_spec.rb @@ -84,6 +84,13 @@ RUBY end + it 'ignores n.times when create call does have method calls ' \ + 'and repeat multiple times with other argument' do + expect_no_offenses(<<~RUBY) + 3.times { |n| create :user, :admin, repositories_count: rand } + RUBY + end + it 'ignores n.times when create call does have method calls ' \ 'and not repeat' do expect_no_offenses(<<~RUBY) @@ -138,17 +145,6 @@ RUBY end - it 'flags usage of n.times with keyword arguments' do - expect_offense(<<~RUBY) - 5.times { create :user, :trait, key: val } - ^^^^^^^ Prefer create_list. - RUBY - - expect_correction(<<~RUBY) - create_list :user, 5, :trait, key: val - RUBY - end - it 'flags usage of n.times with block argument' do expect_offense(<<~RUBY) 3.times do