-
Notifications
You must be signed in to change notification settings - Fork 0
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
4 changed files
with
118 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Chewy | ||
# This cop checks for the use of block with single expression in `update_index` method | ||
# and suggests to use method symbol as second argument instead of block. | ||
# | ||
# # bad | ||
# update_index('index_name') { self } | ||
# | ||
# # good | ||
# update_index('index_name', :self) | ||
# | ||
class UpdateIndexArgument < Base | ||
extend AutoCorrector | ||
|
||
MSG = 'Use method symbol as second argument instead of block.' | ||
|
||
RESTRICT_ON_SEND = %i[update_index].freeze | ||
|
||
# @!method block_with_single_expression?(node) | ||
def_node_matcher :block_with_single_expression?, <<~PATTERN | ||
(block | ||
(send nil? :update_index (str _) $...) | ||
(args) | ||
$...) | ||
PATTERN | ||
|
||
def on_block(node) | ||
block_with_single_expression?(node) do |existing_args, method_name| | ||
return if method_name.last.children.compact.size > 1 | ||
|
||
method_name = method_name.first.source | ||
add_offense(node.loc.expression) do |corrector| | ||
corrector.replace(node.loc.expression, corrected_code(node, existing_args, method_name)) | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def corrected_code(node, existing_args, method_name) | ||
method_call = node.children[0] | ||
index_name = method_call.children[2].source | ||
|
||
if existing_args.any? | ||
"update_index(#{index_name}, :#{method_name}, #{existing_args.map(&:source).join(', ')})" | ||
else | ||
"update_index(#{index_name}, :#{method_name})" | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Chewy::UpdateIndexArgument, :config do | ||
it 'registers an offense when using block with single expression' do | ||
expect_offense(<<~RUBY) | ||
update_index('index_name') { self } | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use method symbol as second argument instead of block. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
update_index('index_name', :self) | ||
RUBY | ||
|
||
expect_offense(<<~RUBY) | ||
update_index('index_name', if: :update?) { self } | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use method symbol as second argument instead of block. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
update_index('index_name', :self, if: :update?) | ||
RUBY | ||
|
||
expect_offense(<<~RUBY) | ||
update_index('index_name') { some_ids } | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use method symbol as second argument instead of block. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
update_index('index_name', :some_ids) | ||
RUBY | ||
|
||
expect_offense(<<~RUBY) | ||
update_index('index_name', if: :update?) { some_ids } | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use method symbol as second argument instead of block. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
update_index('index_name', :some_ids, if: :update?) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when no block' do | ||
expect_no_offenses(<<~RUBY) | ||
update_index('index_name', :self) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when the bock containe multiple expression' do | ||
expect_no_offenses(<<~RUBY) | ||
update_index('index_name') { something.call } | ||
RUBY | ||
|
||
expect_no_offenses(<<~RUBY) | ||
update_index('index_name') { something(id) } | ||
RUBY | ||
end | ||
end |