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 committed Oct 29, 2022
2 parents 9431564 + fb7b183 commit d35e96a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
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
7 changes: 4 additions & 3 deletions lib/rubocop/cop/factory_bot/consistent_parentheses_style.rb
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
34 changes: 34 additions & 0 deletions spec/rubocop/cop/factory_bot/consistent_parentheses_style_spec.rb
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 d35e96a

Please sign in to comment.