Class: RuboCop::Cop::UnusedArgCorrector

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

Overview

This auto-corrects 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(processed_source, node) ⇒ Object



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

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

  @processed_source = processed_source

  if node.blockarg_type?
    correct_for_blockarg_type(node)
  else
    lambda do |corrector|
      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
end

.correct_for_blockarg_type(node) ⇒ Object



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

def correct_for_blockarg_type(node)
  lambda do |corrector|
    range = range_with_surrounding_space(range: node.source_range,
                                         side: :left)
    range = range_with_surrounding_comma(range, :left)
    corrector.remove(range)
  end
end