Class: RuboCop::Cop::Corrector
- Inherits:
-
Object
- Object
- 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.
Instance Method Summary collapse
-
#initialize(source_buffer, corrections) ⇒ Corrector
constructor
A new instance of Corrector.
-
#insert_after(range, content) ⇒ Object
Inserts new code after the given source range.
-
#insert_before(range, content) ⇒ Object
Inserts new code before the given source range.
-
#remove(range) ⇒ Object
Removes the source range.
-
#remove_leading(range, size) ⇒ Object
Removes
size
characters from the beginning of the given range. -
#remove_preceding(range, size) ⇒ Object
Removes
size
characters prior to the source range. -
#replace(range, content) ⇒ Object
Replaces the code of the source range
range
withcontent
. -
#rewrite ⇒ String
Does the actual rewrite and returns string corresponding to the rewritten source.
Constructor Details
#initialize(source_buffer, corrections) ⇒ Corrector
Returns a new instance of Corrector.
34 35 36 37 38 |
# File 'lib/rubocop/cop/corrector.rb', line 34 def initialize(source_buffer, corrections) @source_buffer = source_buffer @corrections = corrections @source_rewriter = Parser::Source::Rewriter.new(source_buffer) end |
Instance Method Details
#insert_after(range, content) ⇒ Object
Inserts new code after the given source range.
72 73 74 |
# File 'lib/rubocop/cop/corrector.rb', line 72 def insert_after(range, content) @source_rewriter.insert_after(range, content) end |
#insert_before(range, content) ⇒ Object
Inserts new code before the given source range.
64 65 66 |
# File 'lib/rubocop/cop/corrector.rb', line 64 def insert_before(range, content) @source_rewriter.insert_before(range, content) end |
#remove(range) ⇒ Object
Removes the source range.
56 57 58 |
# File 'lib/rubocop/cop/corrector.rb', line 56 def remove(range) @source_rewriter.remove(range) end |
#remove_leading(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
.
101 102 103 104 105 106 |
# File 'lib/rubocop/cop/corrector.rb', line 101 def remove_leading(range, size) to_remove = Parser::Source::Range.new(range.source_buffer, range.begin_pos, range.begin_pos + size) @source_rewriter.remove(to_remove) end |
#remove_preceding(range, size) ⇒ Object
Removes size
characters prior to the source range.
88 89 90 91 92 93 |
# File 'lib/rubocop/cop/corrector.rb', line 88 def remove_preceding(range, size) to_remove = Parser::Source::Range.new(range.source_buffer, range.begin_pos - size, range.begin_pos) @source_rewriter.remove(to_remove) end |
#replace(range, content) ⇒ Object
Replaces the code of the source range range
with content
.
80 81 82 |
# File 'lib/rubocop/cop/corrector.rb', line 80 def replace(range, content) @source_rewriter.replace(range, content) end |
#rewrite ⇒ String
Does the actual rewrite and returns string corresponding to the rewritten source.
TODO: Handle conflict exceptions raised from the Source::Rewriter
45 46 47 48 49 50 51 |
# File 'lib/rubocop/cop/corrector.rb', line 45 def rewrite @corrections.each do |correction| correction.call(self) end @source_rewriter.process end |