Class: Herb::AST::ERBForNode

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

Overview

: type serialized_erb_for_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) ⇒ ERBForNode

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



2924
2925
2926
2927
2928
2929
2930
2931
2932
# File 'lib/herb/ast/nodes.rb', line 2924

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?



2917
2918
2919
# File 'lib/herb/ast/nodes.rb', line 2917

def content
  @content
end

#end_nodeObject (readonly)

: Herb::AST::ERBEndNode?



2921
2922
2923
# File 'lib/herb/ast/nodes.rb', line 2921

def end_node
  @end_node
end

#prism_nodeObject (readonly)

: String?



2919
2920
2921
# File 'lib/herb/ast/nodes.rb', line 2919

def prism_node
  @prism_node
end

#statementsObject (readonly)

: Array



2920
2921
2922
# File 'lib/herb/ast/nodes.rb', line 2920

def statements
  @statements
end

#tag_closingObject (readonly)

: Herb::Token?



2918
2919
2920
# File 'lib/herb/ast/nodes.rb', line 2918

def tag_closing
  @tag_closing
end

#tag_openingObject (readonly)

: Herb::Token?



2916
2917
2918
# File 'lib/herb/ast/nodes.rb', line 2916

def tag_opening
  @tag_opening
end

Instance Method Details

#accept(visitor) ⇒ Object

: (Visitor) -> void



2963
2964
2965
# File 'lib/herb/ast/nodes.rb', line 2963

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

#child_nodesObject

: () -> Array



2968
2969
2970
# File 'lib/herb/ast/nodes.rb', line 2968

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

#compact_child_nodesObject

: () -> Array



2973
2974
2975
# File 'lib/herb/ast/nodes.rb', line 2973

def compact_child_nodes
  child_nodes.compact
end

#deserialized_prism_nodeObject

: () -> Prism::node?



2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
# File 'lib/herb/ast/nodes.rb', line 2935

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



2978
2979
2980
# File 'lib/herb/ast/nodes.rb', line 2978

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

#to_hashObject

: () -> serialized_erb_for_node



2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
# File 'lib/herb/ast/nodes.rb', line 2951

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_for_node
end

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

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



2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
# File 'lib/herb/ast/nodes.rb', line 2983

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