-
-
Notifications
You must be signed in to change notification settings - Fork 263
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/WhereRange
cop
#1272
Conversation
The original ticket mentions a chain of where. Also, for cases with an open-ended range the difference is not that critical. Can there be an option to strictly prefer ranges, or prefer ranges to double ‘where’ and ‘AND’? |
And thanks for handling this! ❤️ |
That will make this cop much more complex. I would like to avoid that. Most of the offences, I believe, are defined via a single |
At a glance, we need to consider what ruby (2.6, 2.7+ or eaerlier) and Rails versions are our targets. Depending on this, suggestions may not apply. See this styleguide note and this comment.
The cop does consider this, right? |
I understand that, and I see that even now the cop is far from trivial. |
cbce188
to
26f59ff
Compare
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
Model.where(column: value..) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Endless ranges (start..)
are supported since Ruby 2.6, while beginless ranges (..end)
are supported since Ruby 2.7. Each should be considered accordingly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ruby 2.7 is EOL for a long time. Not to mention 2.6.
Instead, wdyt about dropping the support for it altogether from the gem?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The runtime Ruby version (2.7+) and parsing Ruby version (2.0+) are different. This is the parsing Ruby version.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean by the original comment, that I should update the test cases only or the code of the cop too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RSpec supports Ruby 1.8.7
RuboCop “Targets Ruby 2.0+ code analysis”.
RuboCop Rails:
Rails cops support the following versions:
Rails 4.2+
Please don’t get me wrong, I always advocate to drop support for EOL versions, even in minor/patch versions. But while those versions are supported, we need to be compatible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added minimum_target_ruby_version 2.7
. Do you mean to lower it to 2.6
and handle both cases of endless ranges support differently?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it’s just perfect. My apologies for my ambiguous language, I meant not to break it for earlier ruby/rails versions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean to lower it to
2.6
and handle both cases of endless ranges support differently?
Yes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated. Please, take a look.
26f59ff
to
750a16a
Compare
it 'does not register an offense when using anonymous `<=`' do | ||
expect_no_offenses(<<~RUBY) | ||
Model.where('column <= ?', value) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using anonymous `<`' do | ||
expect_no_offenses(<<~RUBY) | ||
Model.where('column < ?', value) | ||
RUBY | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prism matrix is failure. Can you use unsupported_on: :prism
?
https://github.com/rubocop/rubocop-rails/actions/runs/8860610072/job/24331577196?pr=1272
it 'does not register an offense when using anonymous `<=`' do | |
expect_no_offenses(<<~RUBY) | |
Model.where('column <= ?', value) | |
RUBY | |
end | |
it 'does not register an offense when using anonymous `<`' do | |
expect_no_offenses(<<~RUBY) | |
Model.where('column < ?', value) | |
RUBY | |
end | |
context 'TargetRubyVersion <= 2.6', :ruby26, unsupported_on: :prism do | |
it 'does not register an offense when using anonymous `<=`' do | |
expect_no_offenses(<<~RUBY) | |
Model.where('column <= ?', value) | |
RUBY | |
end | |
it 'does not register an offense when using anonymous `<`' do | |
expect_no_offenses(<<~RUBY) | |
Model.where('column < ?', value) | |
RUBY | |
end | |
end |
lib/rubocop/cop/rails/where_range.rb
Outdated
# column <[=] :value | ||
LTEQ_NAMED_RE = /\A([\w.]+)\s+(<=?)\s+:(\w+)\z/.freeze | ||
# column >= :value1 AND column <[=] :value2 | ||
RANGE_NAMED_RE = /\A([\w.]+)\s+>=\s+:(\w+)\s+AND\s+\1\s+(<=?)\s+:(\w+)\z/i.freeze |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can these constants be moved above minimum_target_ruby_version
?
750a16a
to
5a2e7df
Compare
Made updates. |
Thanks! |
Closes #689.
It supports
where
andwhere.not
,where
with a a regular and an array syntax.