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/WhereNot cop #274

Merged
merged 1 commit into from
Jul 28, 2020
Merged

Add new Rails/WhereNot cop #274

merged 1 commit into from
Jul 28, 2020

Conversation

fatkodima
Copy link
Contributor

https://rails.rubystyle.guide/#where-not

# bad
User.where('name != ?', 'Gabe')
User.where('name != :name', name: 'Gabe')
User.where('name IS NOT NULL')
User.where('name NOT IN (?)', ['john', 'jane'])
User.where('name NOT IN (:names)', names: ['john', 'jane'])

# good
User.where.not(name: 'Gabe')
User.where.not(name: nil)
User.where.not(name: ['john', 'jane'])

I implemented in this PR the most basic and popular cases, to make this cop simpler and avoid manually parsing complex sql strings.

@andyw8
Copy link
Contributor

andyw8 commented Jun 28, 2020

I like the concept of checking for this, but I have a feeling that trying to parse SQL with regular expressions is going to be problematic.

@fatkodima
Copy link
Contributor Author

Yes, definitely parsing complex SQL with regular expressions would not lead to something good, but in this case I'm parsing primitive and precise expressions (like column != ?, column NOT IN (?) and column IS NOT NULL), so I believe this is fine.

config/default.yml Outdated Show resolved Hide resolved
@fatkodima
Copy link
Contributor Author

Updated.

@koic koic merged commit 23c5b71 into rubocop:master Jul 28, 2020
@koic
Copy link
Member

koic commented Jul 28, 2020

Thanks!

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.

3 participants