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 Attribute Summary collapse
-
#corrections ⇒ Object
readonly
Returns the value of attribute corrections.
-
#diagnostics ⇒ Object
readonly
Returns the value of attribute diagnostics.
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
sizecharacters from the beginning of the given range. -
#remove_preceding(range, size) ⇒ Object
Removes
sizecharacters prior to the source range. -
#remove_trailing(range, size) ⇒ Object
Removes
sizecharacters from the end of the given range. -
#replace(range, content) ⇒ Object
Replaces the code of the source range
rangewithcontent. -
#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 39 40 41 42 43 44 |
# 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) @diagnostics = [] # Don't print warnings to stderr if corrections conflict with each other @source_rewriter.diagnostics.consumer = lambda do |diagnostic| @diagnostics << diagnostic end end |
Instance Attribute Details
#corrections ⇒ Object (readonly)
Returns the value of attribute corrections.
46 47 48 |
# File 'lib/rubocop/cop/corrector.rb', line 46 def corrections @corrections end |
#diagnostics ⇒ Object (readonly)
Returns the value of attribute diagnostics.
46 47 48 |
# File 'lib/rubocop/cop/corrector.rb', line 46 def diagnostics @diagnostics end |
Instance Method Details
#insert_after(range, content) ⇒ Object
Inserts new code after the given source range.
86 87 88 |
# File 'lib/rubocop/cop/corrector.rb', line 86 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.
78 79 80 |
# File 'lib/rubocop/cop/corrector.rb', line 78 def insert_before(range, content) @source_rewriter.insert_before(range, content) end |
#remove(range) ⇒ Object
Removes the source range.
70 71 72 |
# File 'lib/rubocop/cop/corrector.rb', line 70 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.
115 116 117 118 119 120 |
# File 'lib/rubocop/cop/corrector.rb', line 115 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.
102 103 104 105 106 107 |
# File 'lib/rubocop/cop/corrector.rb', line 102 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 |
#remove_trailing(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.
128 129 130 131 132 133 |
# File 'lib/rubocop/cop/corrector.rb', line 128 def remove_trailing(range, size) to_remove = Parser::Source::Range.new(range.source_buffer, range.end_pos - size, range.end_pos) @source_rewriter.remove(to_remove) end |
#replace(range, content) ⇒ Object
Replaces the code of the source range range with content.
94 95 96 |
# File 'lib/rubocop/cop/corrector.rb', line 94 def replace(range, content) @source_rewriter.replace(range, content) end |
#rewrite ⇒ String
Does the actual rewrite and returns string corresponding to the rewritten source.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/rubocop/cop/corrector.rb', line 52 def rewrite # rubocop:disable Lint/HandleExceptions @corrections.each do |correction| begin @source_rewriter.transaction do correction.call(self) end rescue ::Parser::ClobberingError end end # rubocop:enable Lint/HandleExceptions @source_rewriter.process end |