mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-19 19:00:39 -07:00
Colocate Traversal classes with classes they traverse
This puts all knowledge of the traversal in the same file, and removes the need for a CASES registry.
This commit is contained in:
@@ -45,6 +45,7 @@ module Liquid
|
|||||||
end
|
end
|
||||||
|
|
||||||
require "liquid/version"
|
require "liquid/version"
|
||||||
|
require 'liquid/traversal'
|
||||||
require 'liquid/lexer'
|
require 'liquid/lexer'
|
||||||
require 'liquid/parser'
|
require 'liquid/parser'
|
||||||
require 'liquid/i18n'
|
require 'liquid/i18n'
|
||||||
|
|||||||
@@ -128,6 +128,15 @@ module Liquid
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class Traversal < Liquid::Traversal
|
||||||
|
def children
|
||||||
|
[
|
||||||
|
@node.left, @node.right,
|
||||||
|
@node.child_condition, @node.attachment
|
||||||
|
].compact
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class ElseCondition < Condition
|
class ElseCondition < Condition
|
||||||
|
|||||||
@@ -47,6 +47,12 @@ module Liquid
|
|||||||
1
|
1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class Traversal < Liquid::Traversal
|
||||||
|
def children
|
||||||
|
[@node.from]
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
Template.register_tag('assign'.freeze, Assign)
|
Template.register_tag('assign'.freeze, Assign)
|
||||||
|
|||||||
@@ -82,6 +82,12 @@ module Liquid
|
|||||||
block.attach(BlockBody.new)
|
block.attach(BlockBody.new)
|
||||||
@blocks << block
|
@blocks << block
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class Traversal < Liquid::Traversal
|
||||||
|
def children
|
||||||
|
[@node.left] + @node.blocks
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
Template.register_tag('case'.freeze, Case)
|
Template.register_tag('case'.freeze, Case)
|
||||||
|
|||||||
@@ -53,6 +53,12 @@ module Liquid
|
|||||||
$1 ? Expression.parse($1) : nil
|
$1 ? Expression.parse($1) : nil
|
||||||
end.compact
|
end.compact
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class Traversal < Liquid::Traversal
|
||||||
|
def children
|
||||||
|
Array(@node.variables)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
Template.register_tag('cycle', Cycle)
|
Template.register_tag('cycle', Cycle)
|
||||||
|
|||||||
@@ -191,6 +191,12 @@ module Liquid
|
|||||||
def render_else(context)
|
def render_else(context)
|
||||||
@else_block ? @else_block.render(context) : ''.freeze
|
@else_block ? @else_block.render(context) : ''.freeze
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class Traversal < Liquid::Traversal
|
||||||
|
def children
|
||||||
|
(super + [@node.limit, @node.from, @node.collection_name]).compact
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
Template.register_tag('for'.freeze, For)
|
Template.register_tag('for'.freeze, For)
|
||||||
|
|||||||
@@ -110,6 +110,12 @@ module Liquid
|
|||||||
Condition.new(a)
|
Condition.new(a)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class Traversal < Liquid::Traversal
|
||||||
|
def children
|
||||||
|
@node.blocks
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
Template.register_tag('if'.freeze, If)
|
Template.register_tag('if'.freeze, If)
|
||||||
|
|||||||
@@ -109,6 +109,15 @@ module Liquid
|
|||||||
|
|
||||||
file_system.read_template_file(context.evaluate(@template_name_expr))
|
file_system.read_template_file(context.evaluate(@template_name_expr))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class Traversal < Liquid::Traversal
|
||||||
|
def children
|
||||||
|
[
|
||||||
|
@node.template_name_expr,
|
||||||
|
@node.variable_name_expr
|
||||||
|
] + @node.attributes.values
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
Template.register_tag('include'.freeze, Include)
|
Template.register_tag('include'.freeze, Include)
|
||||||
|
|||||||
@@ -50,6 +50,12 @@ module Liquid
|
|||||||
result << "</tr>\n"
|
result << "</tr>\n"
|
||||||
result
|
result
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class Traversal < Liquid::Traversal
|
||||||
|
def children
|
||||||
|
super + @node.attributes.values + [@node.collection_name]
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
Template.register_tag('tablerow'.freeze, TableRow)
|
Template.register_tag('tablerow'.freeze, TableRow)
|
||||||
|
|||||||
+5
-81
@@ -3,8 +3,11 @@
|
|||||||
module Liquid
|
module Liquid
|
||||||
class Traversal
|
class Traversal
|
||||||
def self.for(node, callbacks = Hash.new(proc {}))
|
def self.for(node, callbacks = Hash.new(proc {}))
|
||||||
kase = CASES.find { |(klass, _)| node.is_a?(klass) }&.last
|
if defined?(node.class::Traversal)
|
||||||
(kase || self).new(node, callbacks)
|
node.class::Traversal
|
||||||
|
else
|
||||||
|
self
|
||||||
|
end.new(node, callbacks)
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize(node, callbacks)
|
def initialize(node, callbacks)
|
||||||
@@ -35,84 +38,5 @@ module Liquid
|
|||||||
def children
|
def children
|
||||||
@node.respond_to?(:nodelist) ? Array(@node.nodelist) : []
|
@node.respond_to?(:nodelist) ? Array(@node.nodelist) : []
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -138,5 +138,11 @@ module Liquid
|
|||||||
raise error
|
raise error
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class Traversal < Liquid::Traversal
|
||||||
|
def children
|
||||||
|
[@node.name] + @node.filters.flatten
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -78,5 +78,11 @@ module Liquid
|
|||||||
def state
|
def state
|
||||||
[@name, @lookups, @command_flags]
|
[@name, @lookups, @command_flags]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class Traversal < Liquid::Traversal
|
||||||
|
def children
|
||||||
|
@node.lookups
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require 'test_helper'
|
require 'test_helper'
|
||||||
require 'liquid/traversal'
|
|
||||||
|
|
||||||
class TraversalTest < Minitest::Test
|
class TraversalTest < Minitest::Test
|
||||||
include Liquid
|
include Liquid
|
||||||
|
|||||||
Reference in New Issue
Block a user