Class: Unparser::Comments
- Inherits:
-
Object
- Object
- Unparser::Comments
- Defined in:
- lib/unparser/comments.rb
Overview
Holds the comments that remain to be emitted
ignore :reek:RepeatedConditional
Class Method Summary collapse
-
.source_range(node, part) ⇒ Parser::Source::Range?
private
Return source location part.
Instance Method Summary collapse
-
#consume(node, source_part = :expression) ⇒ undefined
private
Consume part or all of the node.
-
#initialize(comments) ⇒ undefined
constructor
private
Initialize object.
-
#source_range(*arguments) ⇒ undefined
private
Proxy to singleton.
-
#take_all ⇒ Array
private
Take all remaining comments.
-
#take_before(node, source_part) ⇒ Array
private
Take comments appear in the source before the specified part of the node.
-
#take_eol_comments ⇒ Array
private
Take end-of-line comments.
Constructor Details
#initialize(comments) ⇒ undefined
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initialize object
32 33 34 35 |
# File 'lib/unparser/comments.rb', line 32 def initialize(comments) @comments = comments.dup @last_range_consumed = nil end |
Class Method Details
.source_range(node, part) ⇒ Parser::Source::Range?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Return source location part
FIXME: This method should not be needed. It does to much inline signalling.
:reek:ManualDispatch
109 110 111 112 |
# File 'lib/unparser/comments.rb', line 109 def self.source_range(node, part) location = node.location location.public_send(part) if location.respond_to?(part) end |
Instance Method Details
#consume(node, source_part = :expression) ⇒ undefined
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Consume part or all of the node
46 47 48 49 |
# File 'lib/unparser/comments.rb', line 46 def consume(node, source_part = :expression) range = source_range(node, source_part) @last_range_consumed = range if range end |
#source_range(*arguments) ⇒ undefined
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Proxy to singleton
NOTICE:
Delegating to stateless helpers is a pattern I saw many times in our code.
Maybe we should make another helper module? include SingletonDelegator.new(:source_range) ?
20 21 22 |
# File 'lib/unparser/comments.rb', line 20 def source_range(*arguments) self.class.source_range(*arguments) end |
#take_all ⇒ Array
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Take all remaining comments
70 71 72 |
# File 'lib/unparser/comments.rb', line 70 def take_all take_while { true } end |
#take_before(node, source_part) ⇒ Array
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Take comments appear in the source before the specified part of the node
83 84 85 86 87 88 89 90 |
# File 'lib/unparser/comments.rb', line 83 def take_before(node, source_part) range = source_range(node, source_part) if range take_while { |comment| comment.location.expression.end_pos <= range.begin_pos } else EMPTY_ARRAY end end |
#take_eol_comments ⇒ Array
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Take end-of-line comments
57 58 59 60 61 62 |
# File 'lib/unparser/comments.rb', line 57 def take_eol_comments return EMPTY_ARRAY unless @last_range_consumed comments = take_up_to_line(@last_range_consumed.end.line) unshift_documents(comments) end |