add parse_simple to skip simple_lookup? check when caller validates

This commit is contained in:
Tobi Lutke
2026-04-04 17:42:32 -07:00
committed by Chris Pak
parent 65d7568403
commit 80edd212a0
2 changed files with 9 additions and 4 deletions
+2 -2
View File
@@ -163,9 +163,9 @@ module Liquid
elsif Expression::LITERALS.key?(expr_markup)
@name = Expression::LITERALS[expr_markup]
elsif cache
@name = cache[expr_markup] || (cache[expr_markup] = VariableLookup.parse(expr_markup, ss, cache).freeze)
@name = cache[expr_markup] || (cache[expr_markup] = VariableLookup.parse_simple(expr_markup, ss, cache).freeze)
else
@name = VariableLookup.parse(expr_markup, ss || StringScanner.new(""), nil).freeze
@name = VariableLookup.parse_simple(expr_markup, ss || StringScanner.new(""), nil).freeze
end
# End of markup? No filters.
+7 -2
View File
@@ -10,6 +10,11 @@ module Liquid
new(markup, string_scanner, cache)
end
# Fast parse that skips simple_lookup? check — caller guarantees simple identifier chain
def self.parse_simple(markup, string_scanner = nil, cache = nil)
new(markup, string_scanner, cache, true)
end
# Fast manual scanner replacing markup.scan(VariableParser)
# VariableParser = /\[(?>[^\[\]]+|\g<0>)*\]|[\w-]+\??/
# Splits "product.variants[0].title" into ["product", "variants", "[0]", "title"]
@@ -93,9 +98,9 @@ module Liquid
true
end
def initialize(markup, string_scanner = StringScanner.new(""), cache = nil)
def initialize(markup, string_scanner = StringScanner.new(""), cache = nil, simple = false)
# Fast path: simple identifier chain without brackets
if self.class.simple_lookup?(markup)
if simple || self.class.simple_lookup?(markup)
dot_pos = markup.index('.')
if dot_pos.nil?
@name = markup