Class: Herb::AST::Node
- Inherits:
-
Object
- Object
- Herb::AST::Node
- Defined in:
- lib/herb/ast/node.rb,
ext/herb/nodes.c
Direct Known Subclasses
CDATANode, DocumentNode, ERBBeginNode, ERBBlockNode, ERBCaseMatchNode, ERBCaseNode, ERBContentNode, ERBElseNode, ERBEndNode, ERBEnsureNode, ERBForNode, ERBIfNode, ERBInNode, ERBOpenTagNode, ERBRenderNode, ERBRescueNode, ERBStrictLocalsNode, ERBUnlessNode, ERBUntilNode, ERBWhenNode, ERBWhileNode, ERBYieldNode, HTMLAttributeNameNode, HTMLAttributeNode, HTMLAttributeValueNode, HTMLCloseTagNode, HTMLCommentNode, HTMLConditionalElementNode, HTMLConditionalOpenTagNode, HTMLDoctypeNode, HTMLElementNode, HTMLOmittedCloseTagNode, HTMLOpenTagNode, HTMLTextNode, HTMLVirtualCloseTagNode, LiteralNode, RubyHTMLAttributesSplatNode, RubyLiteralNode, RubyParameterNode, RubyRenderKeywordsNode, RubyRenderLocalNode, WhitespaceNode, XMLDeclarationNode
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
: Array.
-
#location ⇒ Object
readonly
: Location.
-
#source ⇒ Object
: String?.
-
#type ⇒ Object
readonly
: String.
Instance Method Summary collapse
-
#accept(_visitor) ⇒ Object
: (Visitor) -> void.
-
#child_nodes ⇒ Object
(also: #deconstruct)
: () -> Array.
-
#class_name ⇒ Object
: () -> String.
-
#compact_child_nodes ⇒ Object
: () -> Array.
-
#initialize(type, location, errors = []) ⇒ Node
constructor
: (String, Location, ?Array) -> void.
-
#inspect_array(array, item_name: "item", prefix: " ", indent: 0, depth: 0, depth_limit: 25) ⇒ Object
: ( | Array, | ?item_name: String, | ?prefix: String, | ?indent: Integer, | ?depth: Integer, | ?depth_limit: Integer | ) -> String.
-
#inspect_errors(prefix: " ") ⇒ Object
: (?prefix: String) -> String.
-
#node_name ⇒ Object
: () -> String.
-
#recursive_errors ⇒ Object
: () -> Array.
-
#to_hash ⇒ Object
: () -> serialized_node.
-
#to_json(state = nil) ⇒ Object
: (?untyped) -> String.
-
#tree_inspect(indent: 0, depth: 0, depth_limit: 25) ⇒ Object
: (?indent: Integer, ?depth: Integer, ?depth_limit: Integer) -> String.
Constructor Details
Instance Attribute Details
#errors ⇒ Object (readonly)
: Array
14 15 16 |
# File 'lib/herb/ast/node.rb', line 14 def errors @errors end |
#location ⇒ Object (readonly)
: Location
13 14 15 |
# File 'lib/herb/ast/node.rb', line 13 def location @location end |
#source ⇒ Object
: String?
15 16 17 |
# File 'lib/herb/ast/node.rb', line 15 def source @source end |
#type ⇒ Object (readonly)
: String
12 13 14 |
# File 'lib/herb/ast/node.rb', line 12 def type @type end |
Instance Method Details
#accept(_visitor) ⇒ Object
: (Visitor) -> void
102 103 104 |
# File 'lib/herb/ast/node.rb', line 102 def accept(_visitor) raise NoMethodError, "undefined method `accept' for #{inspect}" end |
#child_nodes ⇒ Object Also known as: deconstruct
: () -> Array
107 108 109 |
# File 'lib/herb/ast/node.rb', line 107 def child_nodes raise NoMethodError, "undefined method `child_nodes' for #{inspect}" end |
#class_name ⇒ Object
: () -> String
41 42 43 |
# File 'lib/herb/ast/node.rb', line 41 def class_name self.class.name || "Node" end |
#compact_child_nodes ⇒ Object
: () -> Array
114 115 116 |
# File 'lib/herb/ast/node.rb', line 114 def compact_child_nodes child_nodes.compact end |
#inspect_array(array, item_name: "item", prefix: " ", indent: 0, depth: 0, depth_limit: 25) ⇒ Object
: ( | Array, | ?item_name: String, | ?prefix: String, | ?indent: Integer, | ?depth: Integer, | ?depth_limit: Integer | ) -> String
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/herb/ast/node.rb', line 70 def inspect_array(array, item_name: "item", prefix: " ", indent: 0, depth: 0, depth_limit: 25) output = +"" if array.any? output += "(#{array.count} #{array.one? ? item_name : "#{item_name}s"})" output += "\n" items = array.map { |item| kwargs = { indent: indent, depth: depth, depth_limit: depth_limit } if array.last == item "└── #{item.tree_inspect(**kwargs).gsub(/^/, " ").lstrip}" else "├── #{item.tree_inspect(**kwargs).gsub(/^/, "│ ")}".gsub("├── │ ", "├──") end } output += items.join.gsub(/^/, prefix) else output += "[]" output += "\n" end output end |
#inspect_errors(prefix: " ") ⇒ Object
: (?prefix: String) -> String
56 57 58 59 60 |
# File 'lib/herb/ast/node.rb', line 56 def inspect_errors(prefix: " ") return "" if errors.empty? "├── errors: #{inspect_array(errors, item_name: "error", prefix: prefix)}" end |
#node_name ⇒ Object
: () -> String
46 47 48 |
# File 'lib/herb/ast/node.rb', line 46 def node_name class_name.split("::").last || "Node" end |
#recursive_errors ⇒ Object
: () -> Array
119 120 121 |
# File 'lib/herb/ast/node.rb', line 119 def recursive_errors errors + compact_child_nodes.flat_map(&:recursive_errors) end |
#to_hash ⇒ Object
: () -> serialized_node
32 33 34 35 36 37 38 |
# File 'lib/herb/ast/node.rb', line 32 def to_hash { type: type, location: location.to_hash, errors: errors.map(&:to_hash), } end |
#to_json(state = nil) ⇒ Object
: (?untyped) -> String
51 52 53 |
# File 'lib/herb/ast/node.rb', line 51 def to_json(state = nil) to_hash.to_json(state) end |
#tree_inspect(indent: 0, depth: 0, depth_limit: 25) ⇒ Object
: (?indent: Integer, ?depth: Integer, ?depth_limit: Integer) -> String
97 98 99 |
# File 'lib/herb/ast/node.rb', line 97 def tree_inspect(indent: 0, depth: 0, depth_limit: 25) raise NotImplementedError end |