Class: ThemeCheck::Node
- Inherits:
-
Object
- Object
- ThemeCheck::Node
- Defined in:
- lib/theme_check/node.rb
Overview
A node from the Liquid AST, the result of parsing a template.
Instance Attribute Summary collapse
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#template ⇒ Object
readonly
Returns the value of attribute template.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
-
#block? ⇒ Boolean
A block of type of node?.
-
#block_body? ⇒ Boolean
The body of blocks.
-
#block_tag? ⇒ Boolean
A tag %…endtag % node?.
-
#children ⇒ Object
Array of children nodes.
-
#comment? ⇒ Boolean
A comment % block node?.
-
#document? ⇒ Boolean
(also: #root?)
Top level node of every template.
- #end_index ⇒ Object
-
#initialize(value, parent, template) ⇒ Node
constructor
A new instance of Node.
-
#inside_liquid_tag? ⇒ Boolean
Is this node inside a ‘liquid … %` block?.
-
#line_number ⇒ Object
Most nodes have a line number, but it’s not guaranteed.
-
#literal? ⇒ Boolean
Literals are hard-coded values in the template.
-
#markup ⇒ Object
The original source code of the node.
- #markup=(markup) ⇒ Object
- #position ⇒ Object
- #range ⇒ Object
- #start_index ⇒ Object
-
#tag? ⇒ Boolean
A tag % node?.
-
#type_name ⇒ Object
The ‘:under_score_name` of this type of node.
-
#whitespace_trimmed? ⇒ Boolean
Is this node inside a ‘… -%`.
Constructor Details
#initialize(value, parent, template) ⇒ Node
Returns a new instance of Node.
8 9 10 11 12 13 |
# File 'lib/theme_check/node.rb', line 8 def initialize(value, parent, template) raise ArgumentError, "Expected a Liquid AST Node" if value.is_a?(Node) @value = value @parent = parent @template = template end |
Instance Attribute Details
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
6 7 8 |
# File 'lib/theme_check/node.rb', line 6 def parent @parent end |
#template ⇒ Object (readonly)
Returns the value of attribute template.
6 7 8 |
# File 'lib/theme_check/node.rb', line 6 def template @template end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
6 7 8 |
# File 'lib/theme_check/node.rb', line 6 def value @value end |
Instance Method Details
#block? ⇒ Boolean
A block of type of node?
91 92 93 |
# File 'lib/theme_check/node.rb', line 91 def block? block_tag? || block_body? || document? end |
#block_body? ⇒ Boolean
The body of blocks
86 87 88 |
# File 'lib/theme_check/node.rb', line 86 def block_body? @value.is_a?(Liquid::BlockBody) end |
#block_tag? ⇒ Boolean
A tag %…endtag % node?
81 82 83 |
# File 'lib/theme_check/node.rb', line 81 def block_tag? @value.is_a?(Liquid::Block) end |
#children ⇒ Object
Array of children nodes.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/theme_check/node.rb', line 33 def children @children ||= begin nodes = if comment? [] elsif defined?(@value.class::ParseTreeVisitor) @value.class::ParseTreeVisitor.new(@value, {}).children elsif @value.respond_to?(:nodelist) Array(@value.nodelist) else [] end # Work around a bug in Liquid::Variable::ParseTreeVisitor that doesn't return # the args in a hash as children nodes. nodes = nodes.flat_map do |node| case node when Hash node.values else node end end nodes.map { |node| Node.new(node, self, @template) } end end |
#comment? ⇒ Boolean
A comment % block node?
70 71 72 |
# File 'lib/theme_check/node.rb', line 70 def comment? @value.is_a?(Liquid::Comment) end |
#document? ⇒ Boolean Also known as: root?
Top level node of every template.
75 76 77 |
# File 'lib/theme_check/node.rb', line 75 def document? @value.is_a?(Liquid::Document) end |
#end_index ⇒ Object
137 138 139 |
# File 'lib/theme_check/node.rb', line 137 def end_index position.end_index end |
#inside_liquid_tag? ⇒ Boolean
Is this node inside a ‘liquid … %` block?
107 108 109 110 111 112 113 |
# File 'lib/theme_check/node.rb', line 107 def inside_liquid_tag? if line_number template.excerpt(line_number).start_with?("{%") else false end end |
#line_number ⇒ Object
Most nodes have a line number, but it’s not guaranteed.
96 97 98 |
# File 'lib/theme_check/node.rb', line 96 def line_number @value.line_number if @value.respond_to?(:line_number) end |
#literal? ⇒ Boolean
Literals are hard-coded values in the template.
60 61 62 |
# File 'lib/theme_check/node.rb', line 60 def literal? @value.is_a?(String) || @value.is_a?(Integer) end |
#markup ⇒ Object
The original source code of the node. Doesn’t contain wrapping braces.
16 17 18 19 20 21 22 |
# File 'lib/theme_check/node.rb', line 16 def markup if tag? @value.raw elsif @value.instance_variable_defined?(:@markup) @value.instance_variable_get(:@markup) end end |
#markup=(markup) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/theme_check/node.rb', line 24 def markup=(markup) if tag? @value.raw = markup elsif @value.instance_variable_defined?(:@markup) @value.instance_variable_set(:@markup, markup) end end |
#position ⇒ Object
129 130 131 |
# File 'lib/theme_check/node.rb', line 129 def position @position ||= Position.new(markup, template&.source, line_number) end |
#range ⇒ Object
124 125 126 127 |
# File 'lib/theme_check/node.rb', line 124 def range start = template.full_line(line_number).index(markup) [start, start + markup.length - 1] end |
#start_index ⇒ Object
133 134 135 |
# File 'lib/theme_check/node.rb', line 133 def start_index position.start_index end |
#tag? ⇒ Boolean
A tag % node?
65 66 67 |
# File 'lib/theme_check/node.rb', line 65 def tag? @value.is_a?(Liquid::Tag) end |
#type_name ⇒ Object
The ‘:under_score_name` of this type of node. Used to dispatch to the `on_<type_name>` and `after_<type_name>` check methods.
102 103 104 |
# File 'lib/theme_check/node.rb', line 102 def type_name @type_name ||= StringHelpers.underscore(StringHelpers.demodulize(@value.class.name)).to_sym end |
#whitespace_trimmed? ⇒ Boolean
Is this node inside a ‘… -%`
116 117 118 119 120 121 122 |
# File 'lib/theme_check/node.rb', line 116 def whitespace_trimmed? if line_number template.excerpt(line_number).start_with?("{%-") else false end end |