Skip to content

Commit

Permalink
Fix rubocop#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 at the start of the factory call
(before the hash values).

see rubocop#135
  • Loading branch information
thejonroberts committed Nov 21, 2024
1 parent 88891a3 commit 6469281
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
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 6469281

Please sign in to comment.