lib/theme_check/liquid_node.rb in theme-check-1.10.3 vs lib/theme_check/liquid_node.rb in theme-check-1.11.0

- old
+ new

@@ -5,10 +5,11 @@ class LiquidNode < Node attr_reader :value, :parent, :theme_file def initialize(value, parent, theme_file) raise ArgumentError, "Expected a Liquid AST Node" if value.is_a?(LiquidNode) + @value = value @parent = parent @theme_file = theme_file @tag_markup = nil @line_number_offset = 0 @@ -35,11 +36,13 @@ node.values else node end end - nodes.map { |node| LiquidNode.new(node, self, @theme_file) } + nodes + .reject(&:nil?) # We don't want nil nodes, and they can happen + .map { |node| LiquidNode.new(node, self, @theme_file) } end end # The original source code of the node. Doesn't contain wrapping braces. def markup @@ -73,15 +76,17 @@ end end def inner_markup return '' unless block? + @inner_markup ||= source[block_start_end_index...block_end_start_index] end def inner_json return nil unless schema? + @inner_json ||= JSON.parse(inner_markup) rescue JSON::ParserError # Handled by ValidSchema @inner_json = nil end @@ -151,10 +156,15 @@ # A {% comment %} block node? def comment? @value.is_a?(Liquid::Comment) end + # {% # comment %} + def inline_comment? + @value.is_a?(Liquid::InlineComment) + end + # Top level node of every liquid_file. def document? @value.is_a?(Liquid::Document) end alias_method :root?, :document? @@ -184,17 +194,37 @@ @type_name ||= StringHelpers.underscore(StringHelpers.demodulize(@value.class.name)).to_sym end def filters raise TypeError, "Attempting to lookup filters of #{type_name}. Only variables have filters." unless variable? + @value.filters end def source theme_file&.source end + # For debugging purposes, this might be easier for the eyes. + def to_h + if literal? + return @value + elsif variable_lookup? + return { + type_name: type_name, + name: value.name.to_s, + lookups: children.map(&:to_h), + } + end + + { + type_name: type_name, + markup: outer_markup, + children: children.map(&:to_h), + } + end + def block_start_markup source[block_start_start_index...block_start_end_index] end def block_start_start_index @@ -215,14 +245,16 @@ source[block_end_start_index...block_end_end_index] end def block_end_start_index return block_start_end_index unless tag? && block? + @block_end_start_index ||= block_end_match&.begin(0) || block_start_end_index end def block_end_end_index return block_end_start_index unless tag? && block? + @block_end_end_index ||= block_end_match&.end(0) || block_start_end_index end def outer_markup_start_index outer_markup_position.start_index