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

Add new Rails/WhereMissing cop #744

Merged
merged 1 commit into from
Aug 1, 2022
Merged

Conversation

ydah
Copy link
Member

@ydah ydah commented Jul 7, 2022

Refs: https://rails.rubystyle.guide/#finding-missing-relationship-records

This cop is check for the use left_joins and where method
for finding missing relationship records.

# bad
Foo.left_joins(:foo).where(foos: { id: nil })

# good
Foo.where.missing(:foo)

Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • [-] Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.
  • If this is a new cop, consider making a corresponding update to the Rails Style Guide.

@dvandersluis
Copy link
Member

dvandersluis commented Jul 7, 2022

Looks like this will only catch code where the chaining is in the exact order prescribed here but order doesn’t matter in ActiveRecord.

None of these will be caught for example (and obviously there are many more forms):

Foo.where(foos: { id: nil }).left_joins(:foo)
# => Foo.where.missing(:foo)

Foo.left_joins(:foo).where(foos: { id: nil }, x: y)
# => Foo.where(x: y).where.missing(:foo)

Foo.left_joins(:foo).joins(:bar).where(foos: { id: nil })
# => Foo.joins(:bar).where.missing(:foo)

@koic
Copy link
Member

koic commented Jul 19, 2022

@ydah Ping.

@ydah
Copy link
Member Author

ydah commented Jul 19, 2022

Oh, thank you so much. I'll work on it this week.

@ydah
Copy link
Member Author

ydah commented Jul 19, 2022

I updated this PR. Thank you so much.

@koic
Copy link
Member

koic commented Jul 20, 2022

@ydah I think this cop can have autocorrection. Can you implement it?

@ydah
Copy link
Member Author

ydah commented Jul 22, 2022

I updated this PR. Auto-correction is now supported. Thank you so much.

@ydah ydah requested a review from koic July 22, 2022 02:01
lib/rubocop/cop/rails/where_missing.rb Outdated Show resolved Hide resolved
lib/rubocop/cop/rails/where_missing.rb Outdated Show resolved Hide resolved
lib/rubocop/cop/rails/where_missing.rb Outdated Show resolved Hide resolved
@Darhazer
Copy link
Member

I've run on a real code base and got

undefined method `value' for #<RuboCop::AST::HashNode:0x000000014fb49c90>
Did you mean?  values
rubocop-rails/lib/rubocop/cop/rails/where_missing.rb:51:in `same_relationship?'

It happens on

left_joins(foo: :bar).where(bars: { id: nil })

@ydah
Copy link
Member Author

ydah commented Jul 23, 2022

Oops, that case was leaking. Thank you so much.
I've updated this PR.

Copy link
Member

@Darhazer Darhazer left a comment

Choose a reason for hiding this comment

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

Sorry to be the bringer of bad news, but there are potential false positives in this cop.

Also with 2 offences within the same file, it managed to replace the left_joins with where.missing, but removed only one of the where(foos: {id: nil}) clauses.

config/default.yml Outdated Show resolved Hide resolved
lib/rubocop/cop/rails/where_missing.rb Outdated Show resolved Hide resolved
lib/rubocop/cop/rails/where_missing.rb Outdated Show resolved Hide resolved
Copy link
Member

@Darhazer Darhazer left a comment

Choose a reason for hiding this comment

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

Thank you. The cop no longer crashes or produces false positives, but there are false negatives. I guess we can't easily fix all of those though.

I left come final comments

spec/rubocop/cop/rails/where_missing_spec.rb Show resolved Hide resolved
lib/rubocop/cop/rails/where_missing.rb Outdated Show resolved Hide resolved
spec/rubocop/cop/rails/where_missing_spec.rb Outdated Show resolved Hide resolved
spec/rubocop/cop/rails/where_missing_spec.rb Show resolved Hide resolved
spec/rubocop/cop/rails/where_missing_spec.rb Show resolved Hide resolved
@ydah ydah force-pushed the add_where_missing branch 3 times, most recently from d0c2953 to fe2ecf8 Compare July 25, 2022 22:31
@ydah ydah requested a review from Darhazer July 25, 2022 22:35
Copy link
Member

@Darhazer Darhazer left a comment

Choose a reason for hiding this comment

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

🙇

I found no false positives, false negatives or incorrect corrections

Refs: https://rails.rubystyle.guide/#finding-missing-relationship-records

This cop is check for the use `left_joins` and `where` method
for finding missing relationship records.

```ruby
# bad
Foo.left_joins(:foo).where(foos: { id: nil })

# good
Foo.where.missing(:foo)
```
@ydah ydah requested a review from koic July 26, 2022 21:48
@koic koic merged commit 3b69c61 into rubocop:master Aug 1, 2022
@ydah ydah deleted the add_where_missing branch August 1, 2022 02:48
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.

4 participants