diff --git a/lib/liquid.rb b/lib/liquid.rb index 7d9da26..eef582f 100644 --- a/lib/liquid.rb +++ b/lib/liquid.rb @@ -45,6 +45,7 @@ module Liquid end require "liquid/version" +require 'liquid/traversal' require 'liquid/lexer' require 'liquid/parser' require 'liquid/i18n' diff --git a/lib/liquid/condition.rb b/lib/liquid/condition.rb index 72bd2ee..aca9420 100644 --- a/lib/liquid/condition.rb +++ b/lib/liquid/condition.rb @@ -128,6 +128,15 @@ module Liquid end end end + + class Traversal < Liquid::Traversal + def children + [ + @node.left, @node.right, + @node.child_condition, @node.attachment + ].compact + end + end end class ElseCondition < Condition diff --git a/lib/liquid/tags/assign.rb b/lib/liquid/tags/assign.rb index ee6fa76..53c9eb2 100644 --- a/lib/liquid/tags/assign.rb +++ b/lib/liquid/tags/assign.rb @@ -47,6 +47,12 @@ module Liquid 1 end end + + class Traversal < Liquid::Traversal + def children + [@node.from] + end + end end Template.register_tag('assign'.freeze, Assign) diff --git a/lib/liquid/tags/case.rb b/lib/liquid/tags/case.rb index f55aa61..433d404 100644 --- a/lib/liquid/tags/case.rb +++ b/lib/liquid/tags/case.rb @@ -82,6 +82,12 @@ module Liquid block.attach(BlockBody.new) @blocks << block end + + class Traversal < Liquid::Traversal + def children + [@node.left] + @node.blocks + end + end end Template.register_tag('case'.freeze, Case) diff --git a/lib/liquid/tags/cycle.rb b/lib/liquid/tags/cycle.rb index 6cf77a2..aaac686 100644 --- a/lib/liquid/tags/cycle.rb +++ b/lib/liquid/tags/cycle.rb @@ -53,6 +53,12 @@ module Liquid $1 ? Expression.parse($1) : nil end.compact end + + class Traversal < Liquid::Traversal + def children + Array(@node.variables) + end + end end Template.register_tag('cycle', Cycle) diff --git a/lib/liquid/tags/for.rb b/lib/liquid/tags/for.rb index c529aae..68c1f92 100644 --- a/lib/liquid/tags/for.rb +++ b/lib/liquid/tags/for.rb @@ -191,6 +191,12 @@ module Liquid def render_else(context) @else_block ? @else_block.render(context) : ''.freeze end + + class Traversal < Liquid::Traversal + def children + (super + [@node.limit, @node.from, @node.collection_name]).compact + end + end end Template.register_tag('for'.freeze, For) diff --git a/lib/liquid/tags/if.rb b/lib/liquid/tags/if.rb index 2a91741..54560c2 100644 --- a/lib/liquid/tags/if.rb +++ b/lib/liquid/tags/if.rb @@ -110,6 +110,12 @@ module Liquid Condition.new(a) end end + + class Traversal < Liquid::Traversal + def children + @node.blocks + end + end end Template.register_tag('if'.freeze, If) diff --git a/lib/liquid/tags/include.rb b/lib/liquid/tags/include.rb index a334d83..23cc591 100644 --- a/lib/liquid/tags/include.rb +++ b/lib/liquid/tags/include.rb @@ -109,6 +109,15 @@ module Liquid file_system.read_template_file(context.evaluate(@template_name_expr)) end + + class Traversal < Liquid::Traversal + def children + [ + @node.template_name_expr, + @node.variable_name_expr + ] + @node.attributes.values + end + end end Template.register_tag('include'.freeze, Include) diff --git a/lib/liquid/tags/table_row.rb b/lib/liquid/tags/table_row.rb index 99d12ec..3994553 100644 --- a/lib/liquid/tags/table_row.rb +++ b/lib/liquid/tags/table_row.rb @@ -50,6 +50,12 @@ module Liquid result << "\n" result end + + class Traversal < Liquid::Traversal + def children + super + @node.attributes.values + [@node.collection_name] + end + end end Template.register_tag('tablerow'.freeze, TableRow) diff --git a/lib/liquid/traversal.rb b/lib/liquid/traversal.rb index 339fc2c..0cebd96 100644 --- a/lib/liquid/traversal.rb +++ b/lib/liquid/traversal.rb @@ -3,8 +3,11 @@ module Liquid class Traversal def self.for(node, callbacks = Hash.new(proc {})) - kase = CASES.find { |(klass, _)| node.is_a?(klass) }&.last - (kase || self).new(node, callbacks) + if defined?(node.class::Traversal) + node.class::Traversal + else + self + end.new(node, callbacks) end def initialize(node, callbacks) @@ -35,84 +38,5 @@ module Liquid def children @node.respond_to?(:nodelist) ? Array(@node.nodelist) : [] end - - class Assign < Traversal - def children - [@node.from] - end - end - - class Case < Traversal - def children - [@node.left] + @node.blocks - end - end - - class Condition < Traversal - def children - [ - @node.left, @node.right, - @node.child_condition, @node.attachment - ].compact - end - end - - class Cycle < Traversal - def children - Array(@node.variables) - end - end - - class For < Traversal - def children - (super + [@node.limit, @node.from, @node.collection_name]).compact - end - end - - class If < Traversal - def children - @node.blocks - end - end - - class Include < Traversal - def children - [ - @node.template_name_expr, - @node.variable_name_expr - ] + @node.attributes.values - end - end - - class TableRow < Traversal - def children - super + @node.attributes.values + [@node.collection_name] - end - end - - class Variable < Traversal - def children - [@node.name] + @node.filters.flatten - end - end - - class VariableLookup < Traversal - def children - @node.lookups - end - end - - CASES = { - Liquid::Assign => Assign, - Liquid::Case => Case, - Liquid::Condition => Condition, - Liquid::Cycle => Cycle, - Liquid::For => For, - Liquid::If => If, - Liquid::Include => Include, - Liquid::TableRow => TableRow, - Liquid::Variable => Variable, - Liquid::VariableLookup => VariableLookup - }.freeze end end diff --git a/lib/liquid/variable.rb b/lib/liquid/variable.rb index 5f88eb3..1258b2d 100644 --- a/lib/liquid/variable.rb +++ b/lib/liquid/variable.rb @@ -138,5 +138,11 @@ module Liquid raise error end end + + class Traversal < Liquid::Traversal + def children + [@node.name] + @node.filters.flatten + end + end end end diff --git a/lib/liquid/variable_lookup.rb b/lib/liquid/variable_lookup.rb index 3ed4e4a..f1b7834 100644 --- a/lib/liquid/variable_lookup.rb +++ b/lib/liquid/variable_lookup.rb @@ -78,5 +78,11 @@ module Liquid def state [@name, @lookups, @command_flags] end + + class Traversal < Liquid::Traversal + def children + @node.lookups + end + end end end diff --git a/test/integration/traversal_test.rb b/test/integration/traversal_test.rb index 6254cb9..7bcfac8 100644 --- a/test/integration/traversal_test.rb +++ b/test/integration/traversal_test.rb @@ -1,7 +1,6 @@ # frozen_string_literal: true require 'test_helper' -require 'liquid/traversal' class TraversalTest < Minitest::Test include Liquid