From fb7b183cce7ba8178de3f7771839dfd17b838619 Mon Sep 17 00:00:00 2001 From: Phil Pirozhkov Date: Sat, 29 Oct 2022 10:32:32 +0300 Subject: [PATCH] Ignore calls without the first positional argument --- CHANGELOG.md | 1 + .../consistent_parentheses_style.rb | 7 ++-- .../consistent_parentheses_style_spec.rb | 34 +++++++++++++++++++ 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ab3640f9..8a848f20 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Master (Unreleased) - Add `named_only` style to `RSpec/NamedSubject`. ([@kuahyeow]) +- Fix `RSpec/FactoryBot/ConsistentParenthesesStyle` to ignore calls without the first positional argument. ([@pirj]) ## 2.14.2 (2022-10-25) diff --git a/lib/rubocop/cop/factory_bot/consistent_parentheses_style.rb b/lib/rubocop/cop/factory_bot/consistent_parentheses_style.rb index 5d02cbe4..3369dc41 100644 --- a/lib/rubocop/cop/factory_bot/consistent_parentheses_style.rb +++ b/lib/rubocop/cop/factory_bot/consistent_parentheses_style.rb @@ -57,9 +57,10 @@ def self.autocorrect_incompatible_with # @!method factory_call(node) def_node_matcher :factory_call, <<-PATTERN - (send - ${#factory_bot? nil?} %FACTORY_CALLS - $...) + (send + {#factory_bot? nil?} %FACTORY_CALLS + {sym str send lvar} _* + ) PATTERN def on_send(node) diff --git a/spec/rubocop/cop/factory_bot/consistent_parentheses_style_spec.rb b/spec/rubocop/cop/factory_bot/consistent_parentheses_style_spec.rb index ac7b834d..59a6bb90 100644 --- a/spec/rubocop/cop/factory_bot/consistent_parentheses_style_spec.rb +++ b/spec/rubocop/cop/factory_bot/consistent_parentheses_style_spec.rb @@ -64,6 +64,8 @@ ^^^^^^^^^^^^^ Prefer method call with parentheses build_stubbed_list :user, 10 ^^^^^^^^^^^^^^^^^^ Prefer method call with parentheses + build factory + ^^^^^ Prefer method call with parentheses RUBY expect_correction(<<~RUBY) @@ -72,6 +74,7 @@ create_list(:user, 10) build_stubbed(:user) build_stubbed_list(:user, 10) + build(factory) RUBY end end @@ -131,6 +134,20 @@ RUBY end end + + it 'flags the call with an explicit receiver' do + expect_offense(<<~RUBY) + FactoryBot.create :user + ^^^^^^ Prefer method call with parentheses + RUBY + end + + it 'ignores FactoryBot DSL methods without a first positional argument' do + expect_no_offenses(<<~RUBY) + create + create foo: :bar + RUBY + end end context 'when EnforcedStyle is :omit_parentheses' do @@ -187,6 +204,8 @@ ^^^^^^^^^^^^^ Prefer method call without parentheses build_stubbed_list(:user, 10) ^^^^^^^^^^^^^^^^^^ Prefer method call without parentheses + build(factory) + ^^^^^ Prefer method call without parentheses RUBY expect_correction(<<~RUBY) @@ -195,6 +214,7 @@ create_list :user, 10 build_stubbed :user build_stubbed_list :user, 10 + build factory RUBY end end @@ -313,5 +333,19 @@ RUBY end end + + it 'flags the call with an explicit receiver' do + expect_offense(<<~RUBY) + FactoryBot.create(:user) + ^^^^^^ Prefer method call without parentheses + RUBY + end + + it 'ignores FactoryBot DSL methods without a first positional argument' do + expect_no_offenses(<<~RUBY) + create() + create(foo: :bar) + RUBY + end end end