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 ‘size` characters from the beginning of the given range.
-
#remove_preceding(node_or_range, size) ⇒ Object
Removes ‘size` characters prior to the source range.
-
#remove_trailing(node_or_range, size) ⇒ Object
Removes ‘size` characters from the end of the given range.
Constructor Details
#initialize(source) ⇒ Corrector
corrector = Corrector.new(cop)
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/rubocop/cop/corrector.rb', line 18 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
71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/rubocop/cop/corrector.rb', line 71 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`.
52 53 54 55 56 |
# File 'lib/rubocop/cop/corrector.rb', line 52 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.
37 38 39 40 41 42 43 44 |
# File 'lib/rubocop/cop/corrector.rb', line 37 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`.
64 65 66 67 68 |
# File 'lib/rubocop/cop/corrector.rb', line 64 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 |