-
-
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#1272](https://github.com/rubocop/rubocop-rails/pull/1272): Add new `Rails/WhereRange` cop. ([@fatkodima][]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Rails | ||
# Identifies places where manually constructed SQL | ||
# in `where` can be replaced with ranges. | ||
# | ||
# @example | ||
# # bad | ||
# User.where('age >= ?', 18) | ||
# User.where.not('age >= ?', 18) | ||
# User.where('age < ?', 18) | ||
# User.where('age >= ? AND age < ?', 18, 21) | ||
# User.where('age >= :start', start: 18) | ||
# User.where('users.age >= ?', 18) | ||
# | ||
# # good | ||
# User.where(age: 18..) | ||
# User.where.not(age: 18..) | ||
# User.where(age: ...18) | ||
# User.where(age: 18...21) | ||
# User.where(users: { age: 18.. }) | ||
# | ||
# # good | ||
# # There are no beginless ranges in ruby. | ||
# User.where('age > ?', 18) | ||
# | ||
class WhereRange < Base | ||
include RangeHelp | ||
extend AutoCorrector | ||
extend TargetRubyVersion | ||
extend TargetRailsVersion | ||
|
||
MSG = 'Use `%<good_method>s` instead of manually constructing SQL.' | ||
|
||
RESTRICT_ON_SEND = %i[where not].freeze | ||
|
||
# column >= ? | ||
GTEQ_ANONYMOUS_RE = /\A([\w.]+)\s+>=\s+\?\z/.freeze | ||
# column <[=] ? | ||
LTEQ_ANONYMOUS_RE = /\A([\w.]+)\s+(<=?)\s+\?\z/.freeze | ||
# column >= ? AND column <[=] ? | ||
RANGE_ANONYMOUS_RE = /\A([\w.]+)\s+>=\s+\?\s+AND\s+\1\s+(<=?)\s+\?\z/i.freeze | ||
# column >= :value | ||
GTEQ_NAMED_RE = /\A([\w.]+)\s+>=\s+:(\w+)\z/.freeze | ||
# 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 | ||
|
||
minimum_target_ruby_version 2.6 | ||
minimum_target_rails_version 6.0 | ||
|
||
def_node_matcher :where_range_call?, <<~PATTERN | ||
{ | ||
(call _ {:where :not} (array $str_type? $_ +)) | ||
(call _ {:where :not} $str_type? $_ +) | ||
} | ||
PATTERN | ||
|
||
def on_send(node) | ||
return if node.method?(:not) && !where_not?(node) | ||
|
||
where_range_call?(node) do |template_node, values_node| | ||
column, value = extract_column_and_value(template_node, values_node) | ||
|
||
return unless column | ||
|
||
range = offense_range(node) | ||
good_method = build_good_method(node.method_name, column, value) | ||
message = format(MSG, good_method: good_method) | ||
|
||
add_offense(range, message: message) do |corrector| | ||
corrector.replace(range, good_method) | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def where_not?(node) | ||
receiver = node.receiver | ||
receiver&.send_type? && receiver&.method?(:where) | ||
end | ||
|
||
# rubocop:disable Metrics | ||
def extract_column_and_value(template_node, values_node) | ||
value = | ||
case template_node.value | ||
when GTEQ_ANONYMOUS_RE | ||
"#{values_node[0].source}.." | ||
when LTEQ_ANONYMOUS_RE | ||
range_operator = range_operator(Regexp.last_match(2)) | ||
"#{range_operator}#{values_node[0].source}" if target_ruby_version >= 2.7 | ||
when RANGE_ANONYMOUS_RE | ||
if values_node.size >= 2 | ||
range_operator = range_operator(Regexp.last_match(2)) | ||
"#{values_node[0].source}#{range_operator}#{values_node[1].source}" | ||
end | ||
when GTEQ_NAMED_RE | ||
value_node = values_node[0] | ||
|
||
if value_node.hash_type? | ||
pair = find_pair(value_node, Regexp.last_match(2)) | ||
"#{pair.value.source}.." if pair | ||
end | ||
when LTEQ_NAMED_RE | ||
value_node = values_node[0] | ||
|
||
if value_node.hash_type? | ||
pair = find_pair(value_node, Regexp.last_match(2)) | ||
if pair && target_ruby_version >= 2.7 | ||
range_operator = range_operator(Regexp.last_match(2)) | ||
"#{range_operator}#{pair.value.source}" | ||
end | ||
end | ||
when RANGE_NAMED_RE | ||
value_node = values_node[0] | ||
|
||
if value_node.hash_type? | ||
range_operator = range_operator(Regexp.last_match(3)) | ||
pair1 = find_pair(value_node, Regexp.last_match(2)) | ||
pair2 = find_pair(value_node, Regexp.last_match(4)) | ||
"#{pair1.value.source}#{range_operator}#{pair2.value.source}" if pair1 && pair2 | ||
end | ||
end | ||
|
||
[Regexp.last_match(1), value] if value | ||
end | ||
# rubocop:enable Metrics | ||
|
||
def range_operator(comparison_operator) | ||
comparison_operator == '<' ? '...' : '..' | ||
end | ||
|
||
def find_pair(hash_node, value) | ||
hash_node.pairs.find { |pair| pair.key.value.to_sym == value.to_sym } | ||
end | ||
|
||
def offense_range(node) | ||
range_between(node.loc.selector.begin_pos, node.source_range.end_pos) | ||
end | ||
|
||
def build_good_method(method_name, column, value) | ||
if column.include?('.') | ||
table, column = column.split('.') | ||
|
||
"#{method_name}(#{table}: { #{column}: #{value} })" | ||
else | ||
"#{method_name}(#{column}: #{value})" | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,202 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Rails::WhereRange, :config do | ||
context 'Ruby <= 2.5', :ruby25 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 | ||
|
||
context 'Rails 5.1', :rails51 do | ||
it 'does not register an offense when using anonymous `>=`' do | ||
expect_no_offenses(<<~RUBY) | ||
Model.where('column >= ?', value) | ||
RUBY | ||
end | ||
end | ||
|
||
context 'Rails 6.0', :rails60 do | ||
context 'Ruby >= 2.6', :ruby26 do | ||
it 'registers an offense and corrects when using anonymous `>=`' do | ||
expect_offense(<<~RUBY) | ||
Model.where('column >= ?', value) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `where(column: value..)` instead of manually constructing SQL. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
Model.where(column: value..) | ||
RUBY | ||
end | ||
|
||
it 'registers an offense and corrects when using named `>=`' do | ||
expect_offense(<<~RUBY) | ||
Model.where('column >= :min', min: value) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `where(column: value..)` instead of manually constructing SQL. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
Model.where(column: value..) | ||
RUBY | ||
end | ||
|
||
it 'registers an offense and corrects when using anonymous `>=` with an explicit table' do | ||
expect_offense(<<~RUBY) | ||
Model.where('table.column >= ?', value) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `where(table: { column: value.. })` instead of manually constructing SQL. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
Model.where(table: { column: value.. }) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using anonymous `>`' do | ||
expect_no_offenses(<<~RUBY) | ||
Model.where('column > ?', value) | ||
RUBY | ||
end | ||
|
||
it 'registers an offense and corrects when using anonymous `>= AND <`' do | ||
expect_offense(<<~RUBY) | ||
Model.where('column >= ? AND column < ?', value1, value2) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `where(column: value1...value2)` instead of manually constructing SQL. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
Model.where(column: value1...value2) | ||
RUBY | ||
end | ||
|
||
it 'registers an offense and corrects when using anonymous `>= AND <=`' do | ||
expect_offense(<<~RUBY) | ||
Model.where('column >= ? AND column <= ?', value1, value2) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `where(column: value1..value2)` instead of manually constructing SQL. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
Model.where(column: value1..value2) | ||
RUBY | ||
end | ||
|
||
it 'registers an offense and corrects when using named `>= AND <`' do | ||
expect_offense(<<~RUBY) | ||
Model.where('column >= :min AND column < :max', min: value1, max: value2) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `where(column: value1...value2)` instead of manually constructing SQL. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
Model.where(column: value1...value2) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using different columns' do | ||
expect_no_offenses(<<~RUBY) | ||
Model.where('column1 >= ? AND column2 < ?', value1, value2) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using named `>= AND <` and placeholders do not exist' do | ||
expect_no_offenses(<<~RUBY) | ||
Model.where('column >= :min AND column < :max', min: value) | ||
RUBY | ||
end | ||
|
||
it 'registers an offense and corrects when using `>=` with an array' do | ||
expect_offense(<<~RUBY) | ||
Model.where(['column >= ?', value]) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `where(column: value..)` instead of manually constructing SQL. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
Model.where(column: value..) | ||
RUBY | ||
end | ||
|
||
it 'registers an offense and corrects when using `>= AND <=` with an array' do | ||
expect_offense(<<~RUBY) | ||
Model.where(['column >= ? AND column <= ?', value1, value2]) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `where(column: value1..value2)` instead of manually constructing SQL. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
Model.where(column: value1..value2) | ||
RUBY | ||
end | ||
|
||
it 'registers an offense and corrects when using `where.not`' do | ||
expect_offense(<<~RUBY) | ||
Model.where.not('column >= ?', value) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^ Use `not(column: value..)` instead of manually constructing SQL. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
Model.where.not(column: value..) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using ranges' do | ||
expect_no_offenses(<<~RUBY) | ||
Model.where(column: value..) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `not` with ranges' do | ||
expect_no_offenses(<<~RUBY) | ||
Model.where.not(column: value..) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `not` not preceding by `where`' do | ||
expect_no_offenses(<<~RUBY) | ||
foo.not('column >= ?', value) | ||
RUBY | ||
end | ||
end | ||
|
||
context 'Ruby >= 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 | ||
|
||
context 'Ruby >= 2.7', :ruby27 do | ||
it 'registers an offense and corrects when using anonymous `<=`' do | ||
expect_offense(<<~RUBY) | ||
Model.where('column <= ?', value) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `where(column: ..value)` instead of manually constructing SQL. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
Model.where(column: ..value) | ||
RUBY | ||
end | ||
|
||
it 'registers an offense and corrects when using anonymous `<`' do | ||
expect_offense(<<~RUBY) | ||
Model.where('column < ?', value) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `where(column: ...value)` instead of manually constructing SQL. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
Model.where(column: ...value) | ||
RUBY | ||
end | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
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 to2.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.
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.