Class: Herb::AST::ERBContentNode

Inherits:
Node
  • Object
show all
Includes:
Colors
Defined in:
lib/herb/ast/nodes.rb,
lib/herb/ast/erb_content_node.rb,
ext/herb/nodes.c

Overview

: type serialized_erb_content_node = { | tag_opening: Herb::Token?, | content: Herb::Token?, | tag_closing: Herb::Token?, | analyzed_ruby: nil, | parsed: bool, | valid: bool, | prism_node: String?, | }

Constant Summary

Constants included from Colors

Colors::CLEAR_SCREEN, Colors::HIDE_CURSOR, Colors::SHOW_CURSOR

Instance Attribute Summary collapse

Attributes inherited from Node

#errors, #location, #source, #type

Instance Method Summary collapse

Methods included from Colors

bold, bright_magenta, cyan, dimmed, enabled?, fg, fg_bg, green, magenta, red, white, yellow

Methods inherited from Node

#class_name, #inspect_array, #inspect_errors, #node_name, #recursive_errors, #to_json

Constructor Details

#initialize(type, location, errors, tag_opening, content, tag_closing, analyzed_ruby, parsed, valid, prism_node) ⇒ ERBContentNode

: (String, Location, Array, Herb::Token, Herb::Token, Herb::Token, nil, bool, bool, String) -> void



1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
# File 'lib/herb/ast/nodes.rb', line 1718

def initialize(type, location, errors, tag_opening, content, tag_closing, analyzed_ruby, parsed, valid, prism_node)
  super(type, location, errors)
  @tag_opening = tag_opening
  @content = content
  @tag_closing = tag_closing
  @analyzed_ruby = analyzed_ruby
  @parsed = parsed
  @valid = valid
  @prism_node = prism_node
end

Instance Attribute Details

#analyzed_rubyObject (readonly)

: nil



1712
1713
1714
# File 'lib/herb/ast/nodes.rb', line 1712

def analyzed_ruby
  @analyzed_ruby
end

#contentObject (readonly)

: Herb::Token?



1710
1711
1712
# File 'lib/herb/ast/nodes.rb', line 1710

def content
  @content
end

#parsedObject (readonly)

: bool



1713
1714
1715
# File 'lib/herb/ast/nodes.rb', line 1713

def parsed
  @parsed
end

#prism_nodeObject (readonly)

: String?



1715
1716
1717
# File 'lib/herb/ast/nodes.rb', line 1715

def prism_node
  @prism_node
end

#tag_closingObject (readonly)

: Herb::Token?



1711
1712
1713
# File 'lib/herb/ast/nodes.rb', line 1711

def tag_closing
  @tag_closing
end

#tag_openingObject (readonly)

: Herb::Token?



1709
1710
1711
# File 'lib/herb/ast/nodes.rb', line 1709

def tag_opening
  @tag_opening
end

#validObject (readonly)

: bool



1714
1715
1716
# File 'lib/herb/ast/nodes.rb', line 1714

def valid
  @valid
end

Instance Method Details

#accept(visitor) ⇒ Object

: (Visitor) -> void



1759
1760
1761
# File 'lib/herb/ast/nodes.rb', line 1759

def accept(visitor)
  visitor.visit_erb_content_node(self)
end

#child_nodesObject

: () -> Array



1764
1765
1766
# File 'lib/herb/ast/nodes.rb', line 1764

def child_nodes
  []
end

#compact_child_nodesObject

: () -> Array



1769
1770
1771
# File 'lib/herb/ast/nodes.rb', line 1769

def compact_child_nodes
  child_nodes.compact
end

#deserialized_prism_nodeObject

: () -> Prism::node?



1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
# File 'lib/herb/ast/nodes.rb', line 1730

def deserialized_prism_node
  prism_node = @prism_node
  return nil unless prism_node
  return nil unless source

  begin
    require "prism"
  rescue LoadError
    warn "The 'prism' gem is required to deserialize Prism nodes. Add it to your Gemfile or install it with: gem install prism"
    return nil
  end

  Prism.load(source, prism_node).value
end

#inspectObject

: () -> String



1774
1775
1776
# File 'lib/herb/ast/nodes.rb', line 1774

def inspect
  tree_inspect.rstrip.gsub(/\s+$/, "")
end

#parsed_prism_nodeObject

: () -> Prism::node?



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/herb/ast/erb_content_node.rb', line 8

def parsed_prism_node
  erb_content = @content&.value&.strip
  return nil unless erb_content

  begin
    require "prism"
  rescue LoadError
    return nil
  end

  prism_result = Prism.parse(erb_content)
  return nil unless prism_result.success?

  prism_result.value.statements.body.first
end

#prismObject

: () -> Prism::node?



25
26
27
28
29
# File 'lib/herb/ast/erb_content_node.rb', line 25

def prism
  return @prism if defined?(@prism)

  @prism = deserialized_prism_node || parsed_prism_node
end

#to_hashObject

: () -> serialized_erb_content_node



1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
# File 'lib/herb/ast/nodes.rb', line 1746

def to_hash
  super.merge({
    tag_opening: tag_opening,
    content: content,
    tag_closing: tag_closing,
    analyzed_ruby: analyzed_ruby,
    parsed: parsed,
    valid: valid,
    prism_node: prism_node,
  }) #: Herb::serialized_erb_content_node
end

#tree_inspect(indent: 0, depth: 0, depth_limit: 10) ⇒ Object

: (?indent: Integer, ?depth: Integer, ?depth_limit: Integer) -> String



1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
# File 'lib/herb/ast/nodes.rb', line 1779

def tree_inspect(indent: 0, depth: 0, depth_limit: 10)
  output = +""

  output += white("@ #{bold(yellow(node_name.to_s))} #{dimmed("(location: #{location.tree_inspect})")}")
  output += "\n"

  if depth >= depth_limit
    output += dimmed("└── [depth limit reached ...]\n\n")

    return output.gsub(/^/, "    " * indent)
  end

  output += inspect_errors(prefix: "")

  output += white("├── tag_opening: ")
  output += tag_opening ? tag_opening.tree_inspect : magenta("")
  output += "\n"
  output += white("├── content: ")
  output += content ? content.tree_inspect : magenta("")
  output += "\n"
  output += white("├── tag_closing: ")
  output += tag_closing ? tag_closing.tree_inspect : magenta("")
  output += "\n"
  output += white("├── parsed: ")
  output += [true, false].include?(parsed) ? bold(magenta(parsed.to_s)) : magenta("")
  output += "\n"
  dynamic_symbol = prism_node ? "├──" : "└──"
  output += white("#{dynamic_symbol} valid: ")
  output += [true, false].include?(valid) ? bold(magenta(valid.to_s)) : magenta("")
  output += "\n"
  if prism_node && source
    output += white("└── prism_node: ")
    output += Herb::PrismInspect.inspect_prism_serialized(prism_node, source, "    ")
    output += "\n"
  end
  output += "\n"

  output.gsub(/^/, "    " * indent)
end