rubocop-factory_bot icon indicating copy to clipboard operation
rubocop-factory_bot copied to clipboard

`FactoryBot/CreateList: Prefer 1.times.map` conflicts with `Lint/UselessTimes`

Open taryneast opened this issue 2 years ago • 1 comments

The first line of this spec-code :

         [ create(:event_delivery, invoice: invoice, flat_rate: false, carer: carer) ] +
            create_list(:event_delivery, 2, invoice: invoice, flat_rate: true, carer: carer)

Triggered an offense of the form: FactoryBot/CreateList: Prefer 1.times.map. and was listed as correctable.

However when correcting it, it triggered a different rubocop as follows:

Inspecting 1 file
W

Offenses:

<filename here>:270:11: C: [Corrected] FactoryBot/CreateList: Prefer 1.times.map.
          [create(:event_delivery, invoice: invoice, flat_rate: false, carer: carer)] +
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
<filename here>:270:11: W: Lint/UselessTimes: Useless call to 1.times detected.
          1.times.map { create(:event_delivery, invoice: invoice, flat_rate: false, carer: carer) } +
          ^^^^^^^

1 file inspected, 2 offenses detected, 1 offense corrected

It seems that 1.times.map is not the right solution to this, but instead it should be:

         create_list(:event_delivery, 1, invoice: invoice, flat_rate: false, carer: carer) +
            create_list(:event_delivery, 2, invoice: invoice, flat_rate: true, carer: carer)

Which seems to make both cops happy. Any chance we could update the cop to do this instead?

taryneast avatar Sep 27 '23 23:09 taryneast

Thanks for reporting.

We could allow for length 1 even if we detect that arguments include method calls here https://github.com/rubocop/rubocop-factory_bot/blob/0da664d97a8632769451a1633eafd852db5cf2a4/lib/rubocop/cop/factory_bot/create_list.rb#L128 and line 125, too.

I don't think our specs describe how we deal with most cases for a single-entry list. Would you like to discuss that for two combinations of styles and the presence of method calls in arguments?

pirj avatar Sep 28 '23 06:09 pirj