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)


9
10
11
12
13
14
# File 'lib/theme_check/node.rb', line 9

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.



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

def parent
  @parent
end

#templateObject (readonly)

Returns the value of attribute template.



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

def template
  @template
end

#valueObject (readonly)

Returns the value of attribute value.



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

def value
  @value
end

Instance Method Details

#block?Boolean

A block of type of node?

Returns:

  • (Boolean)


92
93
94
# File 'lib/theme_check/node.rb', line 92

def block?
  block_tag? || block_body? || document?
end

#block_body?Boolean

The body of blocks

Returns:

  • (Boolean)


87
88
89
# File 'lib/theme_check/node.rb', line 87

def block_body?
  @value.is_a?(Liquid::BlockBody)
end

#block_tag?Boolean

A tag %…endtag % node?

Returns:

  • (Boolean)


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

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

#childrenObject

Array of children nodes.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/theme_check/node.rb', line 34

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)


71
72
73
# File 'lib/theme_check/node.rb', line 71

def comment?
  @value.is_a?(Liquid::Comment)
end

#document?Boolean Also known as: root?

Top level node of every template.

Returns:

  • (Boolean)


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

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

#inside_liquid_tag?Boolean

Is this node inside a ‘liquid … %` block?

Returns:

  • (Boolean)


108
109
110
111
112
113
114
# File 'lib/theme_check/node.rb', line 108

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.



97
98
99
# File 'lib/theme_check/node.rb', line 97

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)


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

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

#markupObject

The original source code of the node. Doesn’t contain wrapping braces.



17
18
19
20
21
22
23
# File 'lib/theme_check/node.rb', line 17

def markup
  if tag?
    @value.raw
  elsif @value.instance_variable_defined?(:@markup)
    @value.instance_variable_get(:@markup)
  end
end

#markup=(markup) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/theme_check/node.rb', line 25

def markup=(markup)
  if tag?
    @value.raw = markup
  elsif @value.instance_variable_defined?(:@markup)
    @value.instance_variable_set(:@markup, markup)
  end
end

#rangeObject



125
126
127
128
# File 'lib/theme_check/node.rb', line 125

def range
  start = template.full_line(line_number).index(markup)
  [start, start + markup.length - 1]
end

#tag?Boolean

A tag % node?

Returns:

  • (Boolean)


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

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.



103
104
105
# File 'lib/theme_check/node.rb', line 103

def type_name
  @type_name ||= @value.class.name.demodulize.underscore.to_sym
end

#whitespace_trimmed?Boolean

Is this node inside a ‘… -%`

Returns:

  • (Boolean)


117
118
119
120
121
122
123
# File 'lib/theme_check/node.rb', line 117

def whitespace_trimmed?
  if line_number
    template.excerpt(line_number).start_with?("{%-")
  else
    false
  end
end