Class: RuboCop::Cop::UnusedArgCorrector

Inherits:
Object
  • Object
show all
Extended by:
RangeHelp
Defined in:
lib/rubocop/cop/correctors/unused_arg_corrector.rb

Overview

This autocorrects unused arguments.

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.processed_sourceObject (readonly)

Returns the value of attribute processed_source.



10
11
12
# File 'lib/rubocop/cop/correctors/unused_arg_corrector.rb', line 10

def processed_source
  @processed_source
end

Class Method Details

.correct(corrector, processed_source, node) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rubocop/cop/correctors/unused_arg_corrector.rb', line 12

def correct(corrector, processed_source, node)
  return if %i[kwarg kwoptarg].include?(node.type)

  @processed_source = processed_source

  if node.blockarg_type?
    correct_for_blockarg_type(corrector, node)
  else
    variable_name = if node.optarg_type?
                      node.node_parts[0]
                    else
                      # Extract only a var name without splat (`*`)
                      node.source.gsub(/\A\*+/, '')
                    end

    corrector.replace(node.loc.name, "_#{variable_name}")
  end
end

.correct_for_blockarg_type(corrector, node) ⇒ Object



31
32
33
34
35
36
# File 'lib/rubocop/cop/correctors/unused_arg_corrector.rb', line 31

def correct_for_blockarg_type(corrector, node)
  range = range_with_surrounding_space(node.source_range, side: :left)
  range = range_with_surrounding_comma(range, :left)

  corrector.remove(range)
end