Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
152 additions
and
1 deletion.
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
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
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,62 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Sevencop | ||
# Identifies a String including "field" is passed to `order` or `reorder`. | ||
# | ||
# @safety | ||
# This cop is unsafe because it can register a false positive. | ||
# | ||
# @example | ||
# | ||
# # bad | ||
# articles.order('field(id, ?)', a) | ||
# | ||
# # good | ||
# articles.order(Arel.sql('field(id, ?)'), a) | ||
# | ||
# # bad | ||
# reorder('field(id, ?)', a) | ||
# | ||
# # good | ||
# reorder(Arel.sql('field(id, ?)'), a) | ||
# | ||
class OrderField < Base | ||
extend AutoCorrector | ||
|
||
MSG = 'Wrap safe SQL String by `Arel.sql`.' | ||
|
||
RESTRICT_ON_SEND = %i[ | ||
order | ||
reorder | ||
].freeze | ||
|
||
ORDER_METHOD_NAMES = ::Set[ | ||
*RESTRICT_ON_SEND | ||
] | ||
|
||
def_node_matcher :order_with_field?, <<~PATTERN | ||
(send | ||
_ ORDER_METHOD_NAMES | ||
(str /field\(.+\)/) | ||
... | ||
) | ||
PATTERN | ||
|
||
# @param [RuboCop::AST::SendNode] node | ||
def on_send(node) | ||
return unless order_with_field?(node) | ||
|
||
first_argument_node = node.arguments.first | ||
add_offense(first_argument_node) do |corrector| | ||
corrector.replace( | ||
node.arguments.first, | ||
"Arel.sql(#{first_argument_node.source})" | ||
) | ||
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,58 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Sevencop::OrderField, :config do | ||
context 'without field' do | ||
it 'registers no offense' do | ||
expect_no_offenses(<<~TEXT) | ||
articles.order('id DESC') | ||
TEXT | ||
end | ||
end | ||
|
||
context 'with Hash' do | ||
it 'registers no offense' do | ||
expect_no_offenses(<<~TEXT) | ||
articles.order(id: :desc) | ||
TEXT | ||
end | ||
end | ||
|
||
context 'with receiver' do | ||
it 'autocorrects offense' do | ||
expect_offense(<<~TEXT) | ||
articles.order('field(id, ?)', a) | ||
^^^^^^^^^^^^^^ Wrap safe SQL String by `Arel.sql`. | ||
TEXT | ||
|
||
expect_correction(<<~RUBY) | ||
articles.order(Arel.sql('field(id, ?)'), a) | ||
RUBY | ||
end | ||
end | ||
|
||
context 'without receiver' do | ||
it 'autocorrects offense' do | ||
expect_offense(<<~TEXT) | ||
order('field(id, ?)', a) | ||
^^^^^^^^^^^^^^ Wrap safe SQL String by `Arel.sql`. | ||
TEXT | ||
|
||
expect_correction(<<~RUBY) | ||
order(Arel.sql('field(id, ?)'), a) | ||
RUBY | ||
end | ||
end | ||
|
||
context 'with reorder' do | ||
it 'autocorrects offense' do | ||
expect_offense(<<~TEXT) | ||
reorder('field(id, ?)', a) | ||
^^^^^^^^^^^^^^ Wrap safe SQL String by `Arel.sql`. | ||
TEXT | ||
|
||
expect_correction(<<~RUBY) | ||
reorder(Arel.sql('field(id, ?)'), a) | ||
RUBY | ||
end | ||
end | ||
end |