rubocop / rubocop-factory_bot

Code style checking for factory_bot files.
https://docs.rubocop.org/rubocop-factory_bot
MIT License
47 stars 13 forks source link

FactoryBot/ConsistentParenthesesStyle in disagree with Style/HashSyntax when enforcing omit_parentheses in `before` blocks #135

Open hfvmarques opened 4 days ago

hfvmarques commented 4 days ago

It's been a while that sometimes I'm getting this. I always have a "workaround" but it's not pretty, but I think that this could be addressed properly.

When you have a before block, and inside there is more than a factory, and within that creates/builds there are hashes with the same name in key/value, there is a crossroad (I don't know if that's a good word). I'll explain:

before do
  create :entity, :with_this_trait, :with_that_trait, user: user
  create :other_entity, :with_this_trait, :with_that_trait, user: user
end

The code above raises the flag of Style/HashSyntax, so we have to do this:

before do
  create :entity, :with_this_trait, :with_that_trait, user:
  create :other_entity, :with_this_trait, :with_that_trait, user: 
end

But the code above does not run because of Ruby sintax error, so we have to do this:

before do
  create(:entity, :with_this_trait, :with_that_trait, user:)
  create(:other_entity, :with_this_trait, :with_that_trait, user:)
end

But if you have the omit_parenthesis EnforcedStyle, it raises the flag of FactoryBot/ConsistentParenthesesStyle.

So, there you have it. Sometime when there are other attributes to use, I put the last one that does not have the same name in key/value such as this:

before do
  create :entity, :with_this_trait, :with_that_trait, user:, other_attribute: variable
  create :other_entity, :with_this_trait, :with_that_trait, user:, other_attribute: other_variable
end

Or even get some more lines in it like this:

before do
  create(
    :entity, :with_this_trait, :with_that_trait, user:
  )
  create(
    :other_entity, :with_this_trait, :with_that_trait, user:
  )
end

But that's very ugly.

So, do you guys think this is a ruby problem, a rubocop problem or a rubocop-factory_bot problem?

pirj commented 4 days ago

Do I read it correctly that Style/HashSyntax‘s autocorrection is causing syntax errors? Can you open an issue with them?

I’m not too familiar with the syntax, it looks like it expects the value for the keyword argument on the next line?

I am not convinced that this “shorthand” syntax is for the good, as it makes it less obvious that many keyword arguments are fine, when they usually are a sign of a design that can be improved.

To me, I’d say that it’s Style/HashSyntax‘s problem with autocorrecting, and that it shouldn’t even bother RSpec DSL, and would recommend disabling it in spec/.rubocop.yml