Class: RuboCop::Cop::Corrector
- Inherits:
-
Parser::Source::TreeRewriter
- Object
- Parser::Source::TreeRewriter
- RuboCop::Cop::Corrector
- 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
Constant Summary collapse
- NOOP_CONSUMER =
noop
->(diagnostic) {}
Class Method Summary collapse
-
.source_buffer(source) ⇒ Object
Duck typing for get to a ::Parser::Source::Buffer.
Instance Method Summary collapse
-
#initialize(source) ⇒ Corrector
constructor
corrector = Corrector.new(cop).
-
#remove_leading(node_or_range, size) ⇒ Object
Removes
sizecharacters from the beginning of the given range. -
#remove_preceding(node_or_range, size) ⇒ Object
Removes
sizecharacters prior to the source range. -
#remove_trailing(node_or_range, size) ⇒ Object
Removes
sizecharacters from the end of the given range. -
#swap(node_or_range1, node_or_range2) ⇒ Object
Swaps sources at the given ranges.
Constructor Details
#initialize(source) ⇒ Corrector
corrector = Corrector.new(cop)
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/rubocop/cop/corrector.rb', line 32 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 = NOOP_CONSUMER end |
Class Method Details
.source_buffer(source) ⇒ Object
Duck typing for get to a ::Parser::Source::Buffer
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/rubocop/cop/corrector.rb', line 15 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.
63 64 65 66 67 |
# File 'lib/rubocop/cop/corrector.rb', line 63 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.
51 52 53 54 55 |
# File 'lib/rubocop/cop/corrector.rb', line 51 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.
75 76 77 78 79 |
# File 'lib/rubocop/cop/corrector.rb', line 75 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 |
#swap(node_or_range1, node_or_range2) ⇒ Object
Swaps sources at the given ranges.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/rubocop/cop/corrector.rb', line 85 def swap(node_or_range1, node_or_range2) range1 = to_range(node_or_range1) range2 = to_range(node_or_range2) if range1.end_pos == range2.begin_pos insert_before(range1, range2.source) remove(range2) elsif range2.end_pos == range1.begin_pos insert_before(range2, range1.source) remove(range1) else replace(range1, range2.source) replace(range2, range1.source) end end |