Skip to content

Commit

Permalink
Fix a false positive for FactoryBot/ConsistentParenthesesStyle when…
Browse files Browse the repository at this point in the history
… hash pinning

Fix: #84
  • Loading branch information
ydah committed Dec 31, 2023
1 parent d6a9e4a commit ade4d56
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

## Master (Unreleased)

- Fix a false positive for `FactoryBot/FactoryNameStyle` when namespaced models. ([@ydah])
- Add new `FactoryBot/ExcessiveCreateList` cop. ([@ddieulivol])
- Fix a false positive for `FactoryBot/FactoryNameStyle` when namespaced models. ([@ydah])
- Fix a false positive for `FactoryBot/ConsistentParenthesesStyle` when hash pinning. ([@ydah])

## 2.24.0 (2023-09-18)

Expand Down
6 changes: 6 additions & 0 deletions lib/rubocop/cop/factory_bot/consistent_parentheses_style.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ class ConsistentParenthesesStyle < ::RuboCop::Cop::Base
)
PATTERN

# @!method omit_hash_value?(node)
def_node_search :omit_hash_value?, <<~PATTERN
(hash <value_omission?> )
PATTERN

def self.autocorrect_incompatible_with
[Style::MethodCallWithArgsParentheses]
end
Expand All @@ -97,6 +102,7 @@ def register_offense(node)
def register_offense_with_parentheses(node)
return if style == :require_parentheses || !node.parenthesized?
return unless same_line?(node, node.first_argument)
return if omit_hash_value?(node)

add_offense(node.loc.selector,
message: MSG_OMIT_PARENS) do |corrector|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,15 @@
generate(:foo, :bar)
RUBY
end

context 'when TargetRubyVersion >= 3.1', :ruby31 do
it 'does not register an offense when using `create` ' \
'with pinned hash argument' do
expect_no_offenses(<<~RUBY)
create(:user, name:)
RUBY
end
end
end

context 'when ExplicitOnly is false' do
Expand Down

0 comments on commit ade4d56

Please sign in to comment.