123456789_123456789_123456789_123456789_123456789_

Class: Prism::ParseResult::Newlines

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Visitor
Instance Chain:
self, Visitor
Inherits: Visitor
  • ::Object
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.

This file is autoloaded only when #mark_newlines! is called, so the re-opening of the various nodes in this file will only be performed in that case. We do that to avoid storing the extra @newline instance variable on every node if we don’t need it.

Class Method Summary

Instance Method Summary

Constructor Details

.new(lines) ⇒ Newlines

Create a new Newlines visitor with the given newline offsets.

[ GitHub ]

  
# File 'lib/prism/parse_result/newlines.rb', line 27

def initialize(lines)
  # @type var lines: Integer
  @lines = Array.new(1 + lines, false)
end

Instance Method Details

#visit_block_node(node) Also known as: #visit_lambda_node

Permit block/lambda nodes to mark newlines within themselves.

[ GitHub ]

  
# File 'lib/prism/parse_result/newlines.rb', line 33

def visit_block_node(node)
  old_lines = @lines
  @lines = Array.new(old_lines.size, false)

  begin
    super(node)
  ensure
    @lines = old_lines
  end
end

#visit_if_node(node) Also known as: #visit_unless_node

Mark if/unless nodes as newlines.

[ GitHub ]

  
# File 'lib/prism/parse_result/newlines.rb', line 47

def visit_if_node(node)
  node.newline_flag!(@lines)
  super(node)
end

#visit_lambda_node(node)

Alias for #visit_block_node.

[ GitHub ]

  
# File 'lib/prism/parse_result/newlines.rb', line 44

alias_method :visit_lambda_node, :visit_block_node

#visit_statements_node(node)

Permit statements lists to mark newlines within themselves.

[ GitHub ]

  
# File 'lib/prism/parse_result/newlines.rb', line 55

def visit_statements_node(node)
  node.body.each do |child|
    child.newline_flag!(@lines)
  end
  super(node)
end

#visit_unless_node(node)

Alias for #visit_if_node.

[ GitHub ]

  
# File 'lib/prism/parse_result/newlines.rb', line 52

alias_method :visit_unless_node, :visit_if_node