Class: RuboCop::Cop::Chewy::UpdateIndexArgument

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/chewy/update_index_argument.rb

Overview

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)

Constant Summary collapse

MSG =
'Use method symbol as second argument instead of block.'
RESTRICT_ON_SEND =
%i[update_index].freeze

Instance Method Summary collapse

Instance Method Details

#block_with_single_expression?(node) ⇒ Object



23
24
25
26
27
28
# File 'lib/rubocop/cop/chewy/update_index_argument.rb', line 23

def_node_matcher :block_with_single_expression?, <<~PATTERN
  (block
    (send nil? :update_index (str _) $...)
    (args)
    $...)
PATTERN

#on_block(node) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/rubocop/cop/chewy/update_index_argument.rb', line 30

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