Skip to content

Commit

Permalink
Fix an error occurred for FactoryBot/IdSequence when sequence wit…
Browse files Browse the repository at this point in the history
…h non-symbol argment or without argument (#98)

Fix: #80
  • Loading branch information
ydah authored Jan 6, 2024
1 parent 85bae8d commit dd523d9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
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)

- Fix a false positive for `FactoryBot/CreateList` when create call does have method calls and repeat multiple times with other argument. ([@ydah])
- Fix an error occurred for `FactoryBot/IdSequence` when `sequence` with non-symbol argument or without argument. ([@ydah])

## 2.25.0 (2024-01-04)

Expand Down
5 changes: 4 additions & 1 deletion lib/rubocop/cop/factory_bot/id_sequence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ module FactoryBot
class IdSequence < ::RuboCop::Cop::Base
extend AutoCorrector
include RangeHelp
include RuboCop::FactoryBot::Language

MSG = 'Do not create a sequence for an id attribute'
RESTRICT_ON_SEND = %i[sequence].freeze

def on_send(node)
return unless node.first_argument.value == :id
return unless node.receiver.nil? || factory_bot?(node.receiver)
return unless node.first_argument&.sym_type? &&
node.first_argument.value == :id

add_offense(node) do |corrector|
range_to_remove = range_by_whole_lines(
Expand Down
24 changes: 24 additions & 0 deletions spec/rubocop/cop/factory_bot/id_sequence_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,28 @@
end
RUBY
end

it 'does not register an offense for a `sequence` with non-symbol argment' do
expect_no_offenses(<<~RUBY)
FactoryBot.define do
sequence(id)
end
RUBY
end

it 'does not register an offense for a `sequence` without argument' do
expect_no_offenses(<<~RUBY)
FactoryBot.define do
sequence
end
RUBY
end

it 'does not register an offense for a `sequence` with receiver' do
expect_no_offenses(<<~RUBY)
FactoryBot.define do
foo.sequence(:id)
end
RUBY
end
end

0 comments on commit dd523d9

Please sign in to comment.