Class: Herb::AST::ERBIfNode

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

Overview

: type serialized_erb_if_node = { | tag_opening: Herb::Token?, | content: Herb::Token?, | tag_closing: Herb::Token?, | then_keyword: Herb::Location?, | prism_node: String?, | statements: Array, | subsequent: (Herb::AST::ERBIfNode | Herb::AST::ERBElseNode)?, | 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, then_keyword, prism_node, statements, subsequent, end_node) ⇒ ERBIfNode

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



2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
# File 'lib/herb/ast/nodes.rb', line 2007

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

Instance Attribute Details

#contentObject (readonly)

: Herb::Token?



1998
1999
2000
# File 'lib/herb/ast/nodes.rb', line 1998

def content
  @content
end

#end_nodeObject (readonly)

: Herb::AST::ERBEndNode?



2004
2005
2006
# File 'lib/herb/ast/nodes.rb', line 2004

def end_node
  @end_node
end

#prism_nodeObject (readonly)

: String?



2001
2002
2003
# File 'lib/herb/ast/nodes.rb', line 2001

def prism_node
  @prism_node
end

#statementsObject (readonly)

: Array



2002
2003
2004
# File 'lib/herb/ast/nodes.rb', line 2002

def statements
  @statements
end

#subsequentObject (readonly)

: (Herb::AST::ERBIfNode | Herb::AST::ERBElseNode)?



2003
2004
2005
# File 'lib/herb/ast/nodes.rb', line 2003

def subsequent
  @subsequent
end

#tag_closingObject (readonly)

: Herb::Token?



1999
2000
2001
# File 'lib/herb/ast/nodes.rb', line 1999

def tag_closing
  @tag_closing
end

#tag_openingObject (readonly)

: Herb::Token?



1997
1998
1999
# File 'lib/herb/ast/nodes.rb', line 1997

def tag_opening
  @tag_opening
end

#then_keywordObject (readonly)

: Herb::Location?



2000
2001
2002
# File 'lib/herb/ast/nodes.rb', line 2000

def then_keyword
  @then_keyword
end

Instance Method Details

#accept(visitor) ⇒ Object

: (Visitor) -> void



2050
2051
2052
# File 'lib/herb/ast/nodes.rb', line 2050

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

#child_nodesObject

: () -> Array



2055
2056
2057
# File 'lib/herb/ast/nodes.rb', line 2055

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

#compact_child_nodesObject

: () -> Array



2060
2061
2062
# File 'lib/herb/ast/nodes.rb', line 2060

def compact_child_nodes
  child_nodes.compact
end

#deserialized_prism_nodeObject

: () -> Prism::node?



2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
# File 'lib/herb/ast/nodes.rb', line 2020

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



2065
2066
2067
# File 'lib/herb/ast/nodes.rb', line 2065

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

#to_hashObject

: () -> serialized_erb_if_node



2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
# File 'lib/herb/ast/nodes.rb', line 2036

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

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

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



2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
# File 'lib/herb/ast/nodes.rb', line 2070

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("├── then_keyword: ")
  output += then_keyword ? dimmed("(location: #{then_keyword.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("├── subsequent: ")
  if subsequent
    output += "\n"
    output += "│   └── "
    output += subsequent.tree_inspect(indent: indent, depth: depth + 1, depth_limit: depth_limit).gsub(/^/, "    " * (indent + 1)).lstrip.gsub(/^/, "").delete_prefix("")
  else
    output += magenta("∅\n")
  end
  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