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 cop UpdateIndexArgument #83

Merged
merged 4 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# main

* Add Add Sidekiq/PerformInline ([#82](https://github.com/petalmd/rubocop-petal/pull/82))
* Add Add Chewy/UpdateIndexArgument ([#83](https://github.com/petalmd/rubocop-petal/pull/83))

# v1.4.0 (2024-06-14)

Expand Down
4 changes: 4 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ Chewy/FieldType:
Include:
- app/chewy/**/*

Chewy/UpdateIndexArgument:
Description: 'Prevent single expression in update_index block.'
Enabled: true

Grape/PreferNamespace:
Description: 'Prevent usage of namespace aliases.'
Enabled: true
Expand Down
56 changes: 56 additions & 0 deletions lib/rubocop/cop/chewy/update_index_argument.rb
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
57 changes: 57 additions & 0 deletions spec/rubocop/cop/chewy/update_index_argument_spec.rb
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