Skip to content

Commit

Permalink
[Fix #135] false positive for omit_parentheses
Browse files Browse the repository at this point in the history
When ConsistentParenthesesStyle is :omit_parentheses, we ignore factory
calls that have an omitted hash value (ruby 3.1 shorthand syntax).
Parentheses are required if the arguments end with the key of the
omitted value. (e.g. `create(:user, name:)`)
However, if a trait was used, (e.g. `create(:user, :trait, name:)`)
omit_hash_value? matcher did not match it, and thus would remove the
parentheses, potentially causing a syntax error.

This change adds a wildcard to the omit_hash_value? matcher to allow
any number of symbols/traits at the start of the pattern
(before the hash values).

see #135
  • Loading branch information
thejonroberts authored and pirj committed Nov 21, 2024
1 parent c0d4398 commit 45968e9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Master (Unreleased)

- Fix a false positive for `FactoryBot/ConsistentParenthesesStyle` when using traits and omitting hash values. ([@thejonroberts])

## 2.26.1 (2024-06-12)

- Bump RuboCop requirement to +1.61. ([@ydah])
Expand Down Expand Up @@ -110,6 +112,7 @@
[@seanpdoyle]: https://github.com/seanpdoyle
[@tdeo]: https://github.com/tdeo
[@tejasbubane]: https://github.com/tejasbubane
[@thejonroberts]: https://github.com/thejonroberts
[@vzvu3k6k]: https://github.com/vzvu3k6k
[@walf443]: https://github.com/walf443
[@ybiquitous]: https://github.com/ybiquitous
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class ConsistentParenthesesStyle < ::RuboCop::Cop::Base
def_node_matcher :omit_hash_value?, <<~PATTERN
(send
#factory_call? %FACTORY_CALLS
{sym str send lvar}
{sym str send lvar} _*
(hash
<value_omission? ...>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@
expect_no_offenses(<<~RUBY)
create(:user, name:)
create(:user, name:, client:)
create(:user, :trait, name:, client:)
RUBY
end

Expand All @@ -426,6 +427,8 @@
expect_no_offenses(<<~RUBY)
create(:user, client:, name: 'foo')
create(:user, client: 'foo', name:)
create(:user, :trait, client:, name: 'foo')
create(:user, :trait, client: 'foo', name:)
RUBY
end

Expand All @@ -434,10 +437,13 @@
expect_offense(<<~RUBY)
create(:user, name: 'foo')
^^^^^^ Prefer method call without parentheses
create(:user, :trait, name: 'foo')
^^^^^^ Prefer method call without parentheses
RUBY

expect_correction(<<~RUBY)
create :user, name: 'foo'
create :user, :trait, name: 'foo'
RUBY
end

Expand All @@ -446,10 +452,13 @@
expect_offense(<<~RUBY)
create(:user, foo(name:))
^^^^^^ Prefer method call without parentheses
create(:user, :trait, foo(name:))
^^^^^^ Prefer method call without parentheses
RUBY

expect_correction(<<~RUBY)
create :user, foo(name:)
create :user, :trait, foo(name:)
RUBY
end
end
Expand Down

0 comments on commit 45968e9

Please sign in to comment.