Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a false positive for FactoryBot/AssociationStyle when EnforcedStyle: Explicit and using trait within trait #59

Merged
merged 1 commit into from
Sep 3, 2023

Conversation

ydah
Copy link
Member

@ydah ydah commented Jul 5, 2023

Fix: #54


Before submitting the PR make sure the following are checked:

  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • [-] Updated documentation.
  • Added an entry to the CHANGELOG.md if the new code introduces user-observable changes.
  • The build (bundle exec rake) passes (be sure to run this locally, since it may produce updated documentation that you will need to commit).

@ydah ydah self-assigned this Jul 5, 2023
@ydah ydah requested review from pirj, bquorning and Darhazer July 5, 2023 22:30
Comment on lines 229 to 252
node.each_ancestor(:block).any? do |ancestor|
if ancestor.send_node.method?(:factory)
trait_name(ancestor).include?(node.method_name)
end
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Performance wise, shouldn't we compile a list of traits on the very definition of the factory, and then reuse it. Or just go to the top block, as now you're traveling part of the tree every time you are going up (when you want to look also into the siblings)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated this PR. Thank you.

@ydah ydah requested a review from Darhazer August 7, 2023 14:29
@@ -158,7 +163,8 @@ def autocorrect_to_implicit_style(corrector, node)

def bad?(node)
if style == :explicit
implicit_association?(node)
implicit_association?(node) &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cosmetic. Here it feels that ‘implicit_association?’ can return true when it only looks like an implicit association, not necessarily is.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should rename the matcher? I can't think of a good name now 🤔

@ydah ydah requested a review from pirj August 10, 2023 08:28
@ydah ydah requested a review from pirj August 13, 2023 02:19
@bquorning bquorning removed their request for review August 13, 2023 06:38
…Style: Explicit` and using trait within trait

Fix: #54
@ydah
Copy link
Member Author

ydah commented Aug 25, 2023

I updated this PR.

Copy link
Member

@pirj pirj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.
Have you had a chance to run it against a real codebase?

@ydah
Copy link
Member Author

ydah commented Aug 28, 2023

I ran bundle exec rubocop --only FactoryBot/AssociationStyle on a large Rails project I have on hand and did not encounter any errors or false positives.

11593 files inspected, no offenses detected

@ydah ydah merged commit 2b2b422 into master Sep 3, 2023
@ydah ydah deleted the ydah/fix/54 branch September 3, 2023 09:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FactoryBot/AssociationStyle.EnforcedStyle: Explicit adds offence when using traits in factories
3 participants