Skip to content

Commit

Permalink
Merge pull request #1446 from rubocop/fb-parentheses-arguments
Browse files Browse the repository at this point in the history
Ignore calls without the first positional argument
  • Loading branch information
pirj authored Oct 29, 2022
2 parents 1218bc9 + f4bb839 commit 4a2a155
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -72,6 +74,7 @@
create_list(:user, 10)
build_stubbed(:user)
build_stubbed_list(:user, 10)
build(factory)
RUBY
end
end
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -195,6 +214,7 @@
create_list :user, 10
build_stubbed :user
build_stubbed_list :user, 10
build factory
RUBY
end
end
Expand Down Expand Up @@ -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

0 comments on commit 4a2a155

Please sign in to comment.