Skip to content

Commit

Permalink
Add cop UpdateIndexArgument (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bhacaz authored Sep 5, 2024
1 parent 60db0d2 commit 65b8944
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 0 deletions.
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

0 comments on commit 65b8944

Please sign in to comment.