Class: Herb::AST::ERBUntilNode

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

Overview

: type serialized_erb_until_node = { | tag_opening: Herb::Token?, | content: Herb::Token?, | tag_closing: Herb::Token?, | prism_node: String?, | statements: Array, | end_node: Herb::AST::ERBEndNode?, | }

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, prism_node, statements, end_node) ⇒ ERBUntilNode

: (String, Location, Array, Herb::Token, Herb::Token, Herb::Token, String, Array, Herb::AST::ERBEndNode) -> void



2802
2803
2804
2805
2806
2807
2808
2809
2810
# File 'lib/herb/ast/nodes.rb', line 2802

def initialize(type, location, errors, tag_opening, content, tag_closing, prism_node, statements, end_node)
  super(type, location, errors)
  @tag_opening = tag_opening
  @content = content
  @tag_closing = tag_closing
  @prism_node = prism_node
  @statements = statements
  @end_node = end_node
end

Instance Attribute Details

#contentObject (readonly)

: Herb::Token?



2795
2796
2797
# File 'lib/herb/ast/nodes.rb', line 2795

def content
  @content
end

#end_nodeObject (readonly)

: Herb::AST::ERBEndNode?



2799
2800
2801
# File 'lib/herb/ast/nodes.rb', line 2799

def end_node
  @end_node
end

#prism_nodeObject (readonly)

: String?



2797
2798
2799
# File 'lib/herb/ast/nodes.rb', line 2797

def prism_node
  @prism_node
end

#statementsObject (readonly)

: Array



2798
2799
2800
# File 'lib/herb/ast/nodes.rb', line 2798

def statements
  @statements
end

#tag_closingObject (readonly)

: Herb::Token?



2796
2797
2798
# File 'lib/herb/ast/nodes.rb', line 2796

def tag_closing
  @tag_closing
end

#tag_openingObject (readonly)

: Herb::Token?



2794
2795
2796
# File 'lib/herb/ast/nodes.rb', line 2794

def tag_opening
  @tag_opening
end

Instance Method Details

#accept(visitor) ⇒ Object

: (Visitor) -> void



2841
2842
2843
# File 'lib/herb/ast/nodes.rb', line 2841

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

#child_nodesObject

: () -> Array



2846
2847
2848
# File 'lib/herb/ast/nodes.rb', line 2846

def child_nodes
  [*(statements || []), end_node]
end

#compact_child_nodesObject

: () -> Array



2851
2852
2853
# File 'lib/herb/ast/nodes.rb', line 2851

def compact_child_nodes
  child_nodes.compact
end

#deserialized_prism_nodeObject

: () -> Prism::node?



2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
# File 'lib/herb/ast/nodes.rb', line 2813

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



2856
2857
2858
# File 'lib/herb/ast/nodes.rb', line 2856

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

#to_hashObject

: () -> serialized_erb_until_node



2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
# File 'lib/herb/ast/nodes.rb', line 2829

def to_hash
  super.merge({
    tag_opening: tag_opening,
    content: content,
    tag_closing: tag_closing,
    prism_node: prism_node,
    statements: statements,
    end_node: end_node,
  }) #: Herb::serialized_erb_until_node
end

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

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



2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
# File 'lib/herb/ast/nodes.rb', line 2861

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"
  if prism_node && source
    output += white("├── prism_node: ")
    output += Herb::PrismInspect.inspect_prism_serialized(prism_node, source, "")
    output += "\n"
  end
  output += white("├── statements: ")
  output += inspect_array(statements, prefix: "", indent: indent, depth: depth + 1, depth_limit: depth_limit)
  output += white("└── end_node: ")
  if end_node
    output += "\n"
    output += "    └── "
    output += end_node.tree_inspect(indent: indent, depth: depth + 1, depth_limit: depth_limit).gsub(/^/, "    " * (indent + 1)).lstrip.gsub(/^/, "    ").delete_prefix("    ")
  else
    output += magenta("∅\n")
  end
  output += "\n"

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