Class: Prism::ParseResult::Newlines
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
Visitor
|
|
Instance Chain:
self,
Visitor
|
|
Inherits: |
Visitor
|
Defined in: | lib/prism/parse_result/newlines.rb |
Overview
The :line
tracepoint event gets fired whenever the Ruby VM encounters an expression on a new line. The types of expressions that can trigger this event are:
-
if statements
-
unless statements
-
nodes that are children of statements lists
In order to keep track of the newlines, we have a list of offsets that come back from the parser. We assign these offsets to the first nodes that we find in the tree that are on those lines.
Note that the logic in this file should be kept in sync with the Java MarkNewlinesVisitor, since that visitor is responsible for marking the newlines for JRuby/TruffleRuby.
Class Method Summary
-
.new(newline_marked) ⇒ Newlines
constructor
Create a new
Newlines
visitor with the given newline offsets.
Instance Method Summary
-
#visit_block_node(node)
(also: #visit_lambda_node)
Permit block/lambda nodes to mark newlines within themselves.
-
#visit_if_node(node)
(also: #visit_unless_node)
Mark if/unless nodes as newlines.
-
#visit_lambda_node(node)
Alias for #visit_block_node.
-
#visit_statements_node(node)
Permit statements lists to mark newlines within themselves.
-
#visit_unless_node(node)
Alias for #visit_if_node.
Constructor Details
.new(newline_marked) ⇒ Newlines
Create a new Newlines
visitor with the given newline offsets.
# File 'lib/prism/parse_result/newlines.rb', line 22
def initialize(newline_marked) @newline_marked = newline_marked end
Instance Method Details
#visit_block_node(node) Also known as: #visit_lambda_node
Permit block/lambda nodes to mark newlines within themselves.
# File 'lib/prism/parse_result/newlines.rb', line 27
def visit_block_node(node) old_newline_marked = @newline_marked @newline_marked = Array.new(old_newline_marked.size, false) begin super(node) ensure @newline_marked = old_newline_marked end end
#visit_if_node(node) Also known as: #visit_unless_node
Mark if/unless nodes as newlines.
# File 'lib/prism/parse_result/newlines.rb', line 41
def visit_if_node(node) node.set_newline_flag(@newline_marked) super(node) end
#visit_lambda_node(node)
Alias for #visit_block_node.
# File 'lib/prism/parse_result/newlines.rb', line 38
alias_method :visit_lambda_node, :visit_block_node
#visit_statements_node(node)
Permit statements lists to mark newlines within themselves.
# File 'lib/prism/parse_result/newlines.rb', line 49
def visit_statements_node(node) node.body.each do |child| child.set_newline_flag(@newline_marked) end super(node) end
#visit_unless_node(node)
Alias for #visit_if_node.
# File 'lib/prism/parse_result/newlines.rb', line 46
alias_method :visit_unless_node, :visit_if_node