Class: ThemeCheck::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/theme_check/node.rb

Overview

A node from the Liquid AST, the result of parsing a template.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, parent, template) ⇒ Node

Returns a new instance of Node.

Raises:

  • (ArgumentError)


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

#parentObject (readonly)

Returns the value of attribute parent.



6
7
8
# File 'lib/theme_check/node.rb', line 6

def parent
  @parent
end

#templateObject (readonly)

Returns the value of attribute template.



6
7
8
# File 'lib/theme_check/node.rb', line 6

def template
  @template
end

#valueObject (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?

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


81
82
83
# File 'lib/theme_check/node.rb', line 81

def block_tag?
  @value.is_a?(Liquid::Block)
end

#childrenObject

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?

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


75
76
77
# File 'lib/theme_check/node.rb', line 75

def document?
  @value.is_a?(Liquid::Document)
end

#end_indexObject



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?

Returns:

  • (Boolean)


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_numberObject

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.

Returns:

  • (Boolean)


60
61
62
# File 'lib/theme_check/node.rb', line 60

def literal?
  @value.is_a?(String) || @value.is_a?(Integer)
end

#markupObject

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

#positionObject



129
130
131
# File 'lib/theme_check/node.rb', line 129

def position
  @position ||= Position.new(markup, template&.source, line_number)
end

#rangeObject



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_indexObject



133
134
135
# File 'lib/theme_check/node.rb', line 133

def start_index
  position.start_index
end

#tag?Boolean

A tag % node?

Returns:

  • (Boolean)


65
66
67
# File 'lib/theme_check/node.rb', line 65

def tag?
  @value.is_a?(Liquid::Tag)
end

#type_nameObject

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 ‘… -%`

Returns:

  • (Boolean)


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