Class: RuboCop::Cop::Corrector

Inherits:
Parser::Source::TreeRewriter
  • Object
show all
Defined in:
lib/rubocop/cop/corrector.rb

Overview

This class takes a source buffer and rewrite its source based on the different correction rules supplied.

Important! The nodes modified by the corrections should be part of the AST of the source_buffer.

Direct Known Subclasses

Legacy::Corrector

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ Corrector

corrector = Corrector.new(cop)

Parameters:

  • source (Parser::Source::Buffer, or anything leading to one via `(processed_source.)buffer`)

16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rubocop/cop/corrector.rb', line 16

def initialize(source)
  source = self.class.source_buffer(source)
  super(
    source,
    different_replacements: :raise,
    swallowed_insertions: :raise,
    crossing_deletions: :accept
  )

  # Don't print warnings to stderr if corrections conflict with each other
  diagnostics.consumer = ->(diagnostic) {}
end

Class Method Details

.source_buffer(source) ⇒ Object

Duck typing for get to a ::Parser::Source::Buffer


69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rubocop/cop/corrector.rb', line 69

def self.source_buffer(source)
  source = source.processed_source if source.respond_to?(:processed_source)
  source = source.buffer if source.respond_to?(:buffer)
  source = source.source_buffer if source.respond_to?(:source_buffer)

  unless source.is_a? ::Parser::Source::Buffer
    raise TypeError, 'Expected argument to lead to a Parser::Source::Buffer ' \
                     "but got #{source.inspect}"
  end

  source
end

Instance Method Details

#remove_leading(node_or_range, size) ⇒ Object

Removes `size` characters from the beginning of the given range. If `size` is greater than the size of `range`, the removed region can overrun the end of `range`.

Parameters:

  • range (Parser::Source::Range, Rubocop::AST::Node)

    or node

  • size (Integer)

50
51
52
53
54
# File 'lib/rubocop/cop/corrector.rb', line 50

def remove_leading(node_or_range, size)
  range = to_range(node_or_range)
  to_remove = range.with(end_pos: range.begin_pos + size)
  remove(to_remove)
end

#remove_preceding(node_or_range, size) ⇒ Object

Removes `size` characters prior to the source range.

Parameters:

  • range (Parser::Source::Range, Rubocop::AST::Node)

    or node

  • size (Integer)

35
36
37
38
39
40
41
42
# File 'lib/rubocop/cop/corrector.rb', line 35

def remove_preceding(node_or_range, size)
  range = to_range(node_or_range)
  to_remove = range.with(
    begin_pos: range.begin_pos - size,
    end_pos:   range.begin_pos
  )
  remove(to_remove)
end

#remove_trailing(node_or_range, size) ⇒ Object

Removes `size` characters from the end of the given range. If `size` is greater than the size of `range`, the removed region can overrun the beginning of `range`.

Parameters:

  • range (Parser::Source::Range, Rubocop::AST::Node)

    or node

  • size (Integer)

62
63
64
65
66
# File 'lib/rubocop/cop/corrector.rb', line 62

def remove_trailing(node_or_range, size)
  range = to_range(node_or_range)
  to_remove = range.with(begin_pos: range.end_pos - size)
  remove(to_remove)
end