Closed taylorrf closed 9 months ago
Can you please share the code surrounding spec/factories/sequences.rb:28?
@pirj Sure, here's the relevant parts of the code surrounding spec/factories/sequences.rb:28
:
27: def self.formatted_sequence(name, digits: 6, separator: '-')
28: sequence(name) { |n|
29: formatted_identifier = _format_n(n, digits: digits, separator: separator)
30: yield formatted_identifier
31: }
32: end
33:
34: def self._format_n(n, digits: 6, separator: '-')
36: format_string = "%0#{digits}d" # see Kernel#sprintf
37: (format_string % n) \
38: .reverse.scan(/\d{3}/).join(separator).reverse
39: end
It’s a matter of adding a check here that node.first_argument.sym_type?
and adding a couple of tests.
I’m uncertain how widespread dstr/str type arguments are and if it’s worth supporting that.
Will you be up to submit a PR, @taylorrf ?
Just to add, I am also experiencing this issue in our organization's project. The lines that crashes RuboCop (twice in fact) look like this:
model.collection.select do |li|
(li.sequence == 0 || li.sequence.blank?) && (li.retry_number == 0 || li.retry_number.blank?)
end
This is written in a trait
block. Curiously, there is no call to sequence
in the factory for the li
's type.
This occurs when running bundle exec rubocop --auto-gen-config
from the terminal.
Here is the tail of the output from that command:
2 errors occurred:
An error occurred while FactoryBot/IdSequence cop was inspecting /.../spec/factories/[the_factory_file].rb:99:11.
An error occurred while FactoryBot/IdSequence cop was inspecting /.../spec/factories/[the_factory_file]:99:31.
Errors are usually caused by RuboCop bugs.
Please, report your problems to RuboCop's issue tracker.
https://github.com/rubocop/rubocop/issues
Mention the following information in the issue report:
1.58.0 (using Parser 3.2.2.4, rubocop-ast 1.30.0, running on ruby 3.0.6) [arm64-darwin22]
Also, here's the version information:
% bundle exec rubocop -V
1.58.0 (using Parser 3.2.2.4, rubocop-ast 1.30.0, running on ruby 3.0.6) [arm64-darwin22]
- rubocop-performance 1.19.1
- rubocop-rails 2.22.2
- rubocop-rake 0.6.0
- rubocop-rspec 2.25.0
For the initial case it would be sufficient to add a check that the first_argument.sym_type?
here https://github.com/rubocop/rubocop-factory_bot/blob/5a92242927b9d90386ccb97cc1f9888e2aa30f9d/lib/rubocop/cop/factory_bot/id_sequence.rb#L27
For the second - that there’s no explicit receiver.
It makes sense to add a node pattern matcher, as it would incorporate both of those checks.
Would anyone be interested in sending a PR provided any necessary support?
Expected behavior
FactoryBot/IdSequence
does not raise an error while inspectingspec/factories/sequences.rb
Actual behavior
Output for
bundle exec rubocop spec/factories/sequences.rb --debug
Steps to reproduce the problem
bundle exec rubocop spec/factories/sequences.rb
RuboCop version