Remove ... syntax references

This commit is contained in:
Julia Boutin 2025-10-22 16:21:17 -06:00 committed by Guilherme Carreiro
parent 40e45e32ac
commit 0cc6cdd553
3 changed files with 138 additions and 769 deletions

View File

@ -17,7 +17,6 @@ module Liquid
DASH = [:dash, "-"].freeze
DOT = [:dot, "."].freeze
DOTDOT = [:dotdot, ".."].freeze
DOTDOTDOT = [:dotdotdot, "..."].freeze
DOT_ORD = ".".ord
DOUBLE_STRING_LITERAL = /"[^\"]*"/
EOS = [:end_of_string].freeze
@ -114,15 +113,10 @@ module Liquid
if (special = SPECIAL_TABLE[peeked])
ss.scan_byte
# Special case for ".." and "..."
# Special case for ".."
if special == DOT && ss.peek_byte == DOT_ORD
ss.scan_byte
if ss.peek_byte == DOT_ORD
ss.scan_byte
output << DOTDOTDOT
else
output << DOTDOT
end
output << DOTDOT
elsif special == DASH
# Special case for negative numbers
if (peeked_byte = ss.peek_byte) && NUMBER_TABLE[peeked_byte]

View File

@ -76,30 +76,7 @@ module Liquid
inner_context['forloop'] = forloop if forloop
@attributes.each do |key, value|
if key.start_with?("...") && is_inline
if key == "..."
context.scopes.each do |scope|
scope.each do |k, v|
inner_context[k] = v
end
end
else
obj = context.evaluate(value)
if obj.is_a?(Liquid::Drop)
(obj.class.invokable_methods - ['to_liquid']).each do |method_name|
inner_context[method_name] = obj.invoke_drop(method_name)
end
elsif obj.is_a?(Hash)
obj.each do |k, v|
inner_context[k] = v
end
else
raise ::ArgumentError
end
end
else
inner_context[key] = context.evaluate(value)
end
inner_context[key] = context.evaluate(value)
end
inner_context[context_variable_name] = var unless var.nil?
@ -132,23 +109,11 @@ module Liquid
p.consume?(:comma)
@attributes = {}
while p.look(:dotdotdot) || p.look(:id)
if p.consume?(:dotdotdot)
if p.look(:id)
identifier = p.read(:id)
key = "...#{identifier}"
@attributes.delete(key)
@attributes[key] = safe_parse_expression(p)
else
@attributes.delete("...")
@attributes["..."] = true
end
else
key = p.consume
p.consume(:colon)
@attributes.delete(key)
@attributes[key] = safe_parse_expression(p)
end
while p.look(:id)
key = p.consume
p.consume(:colon)
@attributes[key] = safe_parse_expression(p)
p.consume?(:comma) # optional comma
end
@ -180,20 +145,8 @@ module Liquid
@is_for_loop = (with_or_for == FOR)
@attributes = {}
markup.scan(/(\.\.\.)(\w+)?(?=\s|,|$)|#{TagAttributes.source}/) do |spread, identifier, key, value|
if spread
if identifier
spread_key = "...#{identifier}"
@attributes.delete(spread_key)
@attributes[spread_key] = parse_expression(identifier)
else
@attributes.delete("...")
@attributes["..."] = true
end
elsif key && value
@attributes.delete(key)
@attributes[key] = parse_expression(value)
end
markup.scan(TagAttributes) do |key, value|
@attributes[key] = parse_expression(value)
end
end

View File

@ -205,6 +205,49 @@ class SnippetTest < Minitest::Test
assert_template_result(expected, template)
end
def test_render_snippets_as_arguments
template = <<~LIQUID.strip
{% assign color_scheme = 'dark' %}
{% snippet header %}
<div class="header">
{{ message }}
</div>
{% endsnippet %}
{% snippet main %}
{% assign color_scheme = 'auto' %}
<div class="main main--{{ color_scheme }}">
{% render header, message: 'Welcome!' %}
</div>
{% endsnippet %}
{% render main, header: header %}
LIQUID
expected = <<~OUTPUT
<div class="main main--auto">
<div class="header">
Welcome!
</div>
</div>
OUTPUT
assert_template_result(expected, template)
end
def test_render_inline_snippet_shouldnt_leak_context
template = <<~LIQUID.strip
{% snippet input %}
@ -263,7 +306,7 @@ class SnippetTest < Minitest::Test
assert_template_result(expected, template)
end
def test_render_inline_snippet_without_outside_context
def test_render_inline_snippet_ignores_outside_context
template = <<~LIQUID.strip
{% assign color_scheme = 'dark' %}
@ -291,18 +334,23 @@ class SnippetTest < Minitest::Test
assert_template_result(expected, template)
end
def test_render_inline_snippet_with_outside_context
template = <<~LIQUID.strip
{% assign color_scheme = 'dark' %}
def test_render_captured_snippet
template = <<~LIQUID
{% snippet header %}
<div class="header header--{{ color_scheme }}">
<div class="header">
{{ message }}
</div>
{% endsnippet %}
{% capture up_header %}
{%- render header, message: 'Welcome!' -%}
{% endcapture %}
{% render header, ..., message: 'Welcome!' %}
{{ up_header | upcase }}
{{ header | upcase }}
{{ header }}
LIQUID
expected = <<~OUTPUT
@ -310,10 +358,14 @@ class SnippetTest < Minitest::Test
<DIV CLASS="HEADER">
WELCOME!
</DIV>
<div class="header header--dark">
Welcome!
</div>
SNIPPETDROP
SnippetDrop
OUTPUT
assert_template_result(expected, template)
@ -332,7 +384,7 @@ class SnippetTest < Minitest::Test
{{ color_scheme }}
{% render header, ..., message: 'Welcome!' %}
{% render header, message: 'Welcome!', color_scheme: color_scheme %}
{{ color_scheme }}
LIQUID
@ -356,328 +408,6 @@ class SnippetTest < Minitest::Test
assert_template_result(expected, template)
end
def test_render_inline_snippet_with_correct_argument_precedence
template = <<~LIQUID.strip
{% assign color_scheme = 'dark' %}
{% assign message = 'Goodbye!' %}
{% snippet header %}
<div class="header header--{{ color_scheme }}">
{{ message }}
</div>
{% endsnippet %}
{% render header, message: 'Welcome!', ... %}
LIQUID
expected = <<~OUTPUT
<div class="header header--dark">
Goodbye!
</div>
OUTPUT
assert_template_result(expected, template)
end
def test_render_inline_snippet_with_correct_argument_order
template = <<~LIQUID.strip
{% assign color_scheme = 'dark' %}
{% assign message = 'Goodbye!' %}
{% snippet header %}
<div class="header header--{{ color_scheme }}">
{{ message }}
</div>
{% endsnippet %}
{% render header, ..., message: 'Welcome!' %}
LIQUID
expected = <<~OUTPUT
<div class="header header--dark">
Welcome!
</div>
OUTPUT
assert_template_result(expected, template)
end
def test_render_inline_snippet_with_correct_duplicate_argument_precedence
template = <<~LIQUID.strip
{% assign color_scheme = 'dark' %}
{% assign message = 'Goodbye!' %}
{% snippet header %}
<div class="header header--{{ color_scheme }}">
{{ message }}
</div>
{% endsnippet %}
{% render header, message: 'Welcome!', ..., message: 'Hi!' %}
LIQUID
expected = <<~OUTPUT
<div class="header header--dark">
Hi!
</div>
OUTPUT
assert_template_result(expected, template)
end
def test_render_inline_snippet_with_spread_hash
template = <<~LIQUID.strip
{% snippet header %}
<div>
{{ word }} {{ number }}
</div>
{% endsnippet %}
{% render header, ...details %}
LIQUID
expected = <<~OUTPUT
<div>
potato 5
</div>
OUTPUT
assert_template_result(expected, template, { 'details' => { 'word' => 'potato', 'number' => 5 } })
end
def test_render_inline_snippet_with_spread_drop
product_drop = Class.new(Liquid::Drop) do
def title
'Cool Product'
end
def price
99
end
def vendor
'Acme'
end
end
template = <<~LIQUID.strip
{% snippet card %}
<div>
{{ title }} - ${{ price }} by {{ vendor }}
</div>
{% endsnippet %}
{% render card, ...product %}
LIQUID
expected = <<~OUTPUT
<div>
Cool Product - $99 by Acme
</div>
OUTPUT
assert_template_result(expected, template, { 'product' => product_drop.new })
end
def test_render_inline_snippet_with_overwritten_spread_drop
product_drop = Class.new(Liquid::Drop) do
def title
'Cool Product'
end
def price
99
end
def vendor
'Acme'
end
end
template = <<~LIQUID.strip
{% snippet card %}
<div>
{{ title }} - ${{ price }} by {{ vendor }}
</div>
{% endsnippet %}
{% render card, ...product, price: 10 %}
LIQUID
expected = <<~OUTPUT
<div>
Cool Product - $10 by Acme
</div>
OUTPUT
assert_template_result(expected, template, { 'product' => product_drop.new })
end
def test_render_inline_snippet_spread_before_explicit_args
template = <<~LIQUID.strip
{% snippet card %}
<div>{{ price }}</div>
{% endsnippet %}
{% render card, ...details, price: 10 %}
LIQUID
expected = <<~OUTPUT
<div>10</div>
OUTPUT
assert_template_result(expected, template, { 'details' => { 'price' => 99 } })
end
def test_render_inline_snippet_multiple_spreads
product_drop = Class.new(Liquid::Drop) do
def title
'Cool Product'
end
end
template = <<~LIQUID.strip
{% snippet card %}
<div>{{ title }} - {{ price }} {{ color }}</div>
{% endsnippet %}
{% render card, ...defaults, ...product %}
LIQUID
expected = <<~OUTPUT
<div>Cool Product - 10 </div>
OUTPUT
assert_template_result(
expected,
template,
{
'defaults' => { 'title' => 'Default', 'price' => 10 },
'product' => product_drop.new,
},
)
end
def test_render_captured_snippet
template = <<~LIQUID
{% assign color_scheme = 'dark' %}
{% snippet header %}
<div class="header header--{{ color_scheme }}">
{{ message }}
</div>
{% endsnippet %}
{% capture up_header %}
{%- render header, ..., message: 'Welcome!' -%}
{% endcapture %}
{{ up_header | upcase }}
{{ header | upcase }}
{{ header }}
LIQUID
expected = <<~OUTPUT
<DIV CLASS="HEADER HEADER--DARK">
WELCOME!
</DIV>
SNIPPETDROP
SnippetDrop
OUTPUT
assert_template_result(expected, template)
end
def test_render_snippets_as_arguments
template = <<~LIQUID.strip
{% assign color_scheme = 'dark' %}
{% snippet header %}
<div class="header header--{{ color_scheme }}">
{{ message }}
</div>
{% endsnippet %}
{% snippet main %}
{% assign color_scheme = 'auto' %}
<div class="main main--{{ color_scheme }}">
{% render header, ..., message: 'Welcome!' %}
</div>
{% endsnippet %}
{% render main, header: header %}
LIQUID
expected = <<~OUTPUT
<div class="main main--auto">
<div class="header header--auto">
Welcome!
</div>
</div>
OUTPUT
assert_template_result(expected, template)
end
def test_render_inline_snippet_forloop
template = <<~LIQUID.strip
{% snippet item %}
@ -738,36 +468,6 @@ class SnippetTest < Minitest::Test
assert_template_result(expected, template)
end
def test_render_inline_snippet_inside_loop
template = <<~LIQUID.strip
{% assign color_scheme = 'dark' %}
{% assign array = '1,2,3' | split: ',' %}
{% for i in array %}
{% snippet header %}
<div class="header header--{{ color_scheme }}">
{{ message }} {{ i }}
</div>
{% endsnippet %}
{% endfor %}
{% render header, ..., message: '👉' %}
LIQUID
expected = <<~OUTPUT
<div class="header header--dark">
👉#{" "}
</div>
OUTPUT
assert_template_result(expected, template)
end
end
class RigidMode < SnippetTest
@ -970,6 +670,49 @@ class SnippetTest < Minitest::Test
assert_template_result(expected, template, error_mode: :rigid)
end
def test_render_snippets_as_arguments
template = <<~LIQUID.strip
{% assign color_scheme = 'dark' %}
{% snippet header %}
<div class="header">
{{ message }}
</div>
{% endsnippet %}
{% snippet main %}
{% assign color_scheme = 'auto' %}
<div class="main main--{{ color_scheme }}">
{% render header, message: 'Welcome!' %}
</div>
{% endsnippet %}
{% render main, header: header %}
LIQUID
expected = <<~OUTPUT
<div class="main main--auto">
<div class="header">
Welcome!
</div>
</div>
OUTPUT
assert_template_result(expected, template, error_mode: :rigid)
end
def test_render_inline_snippet_shouldnt_leak_context
template = <<~LIQUID.strip
{% snippet input %}
@ -1028,7 +771,7 @@ class SnippetTest < Minitest::Test
assert_template_result(expected, template, error_mode: :rigid)
end
def test_render_inline_snippet_without_outside_context
def test_render_inline_snippet_ignores_outside_context
template = <<~LIQUID.strip
{% assign color_scheme = 'dark' %}
@ -1056,34 +799,6 @@ class SnippetTest < Minitest::Test
assert_template_result(expected, template, error_mode: :rigid)
end
def test_render_inline_snippet_with_outside_context
template = <<~LIQUID.strip
{% assign color_scheme = 'dark' %}
{% snippet header %}
<div class="header header--{{ color_scheme }}">
{{ message }}
</div>
{% endsnippet %}
{% render header, ..., message: 'Welcome!' %}
LIQUID
expected = <<~OUTPUT
<div class="header header--dark">
Welcome!
</div>
OUTPUT
assert_template_result(expected, template, error_mode: :rigid)
end
def test_inline_snippet_local_scope_takes_precedence
template = <<~LIQUID
{% assign color_scheme = 'dark' %}
@ -1097,7 +812,7 @@ class SnippetTest < Minitest::Test
{{ color_scheme }}
{% render header, ..., message: 'Welcome!' %}
{% render header, message: 'Welcome!', color_scheme: color_scheme %}
{{ color_scheme }}
LIQUID
@ -1121,306 +836,6 @@ class SnippetTest < Minitest::Test
assert_template_result(expected, template, error_mode: :rigid)
end
def test_render_inline_snippet_with_correct_argument_precedence
template = <<~LIQUID.strip
{% assign color_scheme = 'dark' %}
{% assign message = 'Goodbye!' %}
{% snippet header %}
<div class="header header--{{ color_scheme }}">
{{ message }}
</div>
{% endsnippet %}
{% render header, message: 'Welcome!', ... %}
LIQUID
expected = <<~OUTPUT
<div class="header header--dark">
Goodbye!
</div>
OUTPUT
assert_template_result(expected, template, error_mode: :rigid)
end
def test_render_inline_snippet_with_correct_argument_order
template = <<~LIQUID.strip
{% assign color_scheme = 'dark' %}
{% assign message = 'Goodbye!' %}
{% snippet header %}
<div class="header header--{{ color_scheme }}">
{{ message }}
</div>
{% endsnippet %}
{% render header, ..., message: 'Welcome!' %}
LIQUID
expected = <<~OUTPUT
<div class="header header--dark">
Welcome!
</div>
OUTPUT
assert_template_result(expected, template, error_mode: :rigid)
end
def test_render_inline_snippet_with_correct_duplicate_argument_precedence
template = <<~LIQUID.strip
{% assign color_scheme = 'dark' %}
{% assign message = 'Goodbye!' %}
{% snippet header %}
<div class="header header--{{ color_scheme }}">
{{ message }}
</div>
{% endsnippet %}
{% render header, message: 'Welcome!', ..., message: 'Hi!' %}
LIQUID
expected = <<~OUTPUT
<div class="header header--dark">
Hi!
</div>
OUTPUT
assert_template_result(expected, template, error_mode: :rigid)
end
def test_render_inline_snippet_with_spread_drop
product_drop = Class.new(Liquid::Drop) do
def title
'Cool Product'
end
def price
99
end
def vendor
'Acme'
end
end
template = <<~LIQUID.strip
{% snippet card %}
<div>
{{ title }} - ${{ price }} by {{ vendor }}
</div>
{% endsnippet %}
{% render card, ...product %}
LIQUID
expected = <<~OUTPUT
<div>
Cool Product - $99 by Acme
</div>
OUTPUT
assert_template_result(expected, template, { 'product' => product_drop.new }, error_mode: :rigid)
end
def test_render_inline_snippet_with_overwritten_spread_drop
product_drop = Class.new(Liquid::Drop) do
def title
'Cool Product'
end
def price
99
end
def vendor
'Acme'
end
end
template = <<~LIQUID.strip
{% snippet card %}
<div>
{{ title }} - ${{ price }} by {{ vendor }}
</div>
{% endsnippet %}
{% render card, ...product, price: 10 %}
LIQUID
expected = <<~OUTPUT
<div>
Cool Product - $10 by Acme
</div>
OUTPUT
assert_template_result(expected, template, { 'product' => product_drop.new }, error_mode: :rigid)
end
def test_render_inline_snippet_spread_before_explicit_args
template = <<~LIQUID.strip
{% snippet card %}
<div>{{ price }}</div>
{% endsnippet %}
{% render card, ...details, price: 10 %}
LIQUID
expected = <<~OUTPUT
<div>10</div>
OUTPUT
assert_template_result(expected, template, { 'details' => { 'price' => 99 } }, error_mode: :rigid)
end
def test_render_inline_snippet_multiple_spreads
product_drop = Class.new(Liquid::Drop) do
def title
'Cool Product'
end
end
template = <<~LIQUID.strip
{% snippet card %}
<div>{{ title }} - {{ price }} {{ color }}</div>
{% endsnippet %}
{% render card, ...defaults, ...product %}
LIQUID
expected = <<~OUTPUT
<div>Cool Product - 10 </div>
OUTPUT
assert_template_result(
expected,
template,
{
'defaults' => { 'title' => 'Default', 'price' => 10 },
'product' => product_drop.new,
},
error_mode: :rigid,
)
end
def test_render_captured_snippet
template = <<~LIQUID
{% assign color_scheme = 'dark' %}
{% snippet header %}
<div class="header header--{{ color_scheme }}">
{{ message }}
</div>
{% endsnippet %}
{% capture up_header %}
{%- render header, ..., message: 'Welcome!' -%}
{% endcapture %}
{{ up_header | upcase }}
{{ header | upcase }}
{{ header }}
LIQUID
expected = <<~OUTPUT
<DIV CLASS="HEADER HEADER--DARK">
WELCOME!
</DIV>
SNIPPETDROP
SnippetDrop
OUTPUT
assert_template_result(expected, template, error_mode: :rigid)
end
def test_render_snippets_as_arguments
template = <<~LIQUID.strip
{% assign color_scheme = 'dark' %}
{% snippet header %}
<div class="header header--{{ color_scheme }}">
{{ message }}
</div>
{% endsnippet %}
{% snippet main %}
{% assign color_scheme = 'auto' %}
<div class="main main--{{ color_scheme }}">
{% render header, ..., message: 'Welcome!' %}
</div>
{% endsnippet %}
{% render main, header: header %}
LIQUID
expected = <<~OUTPUT
<div class="main main--auto">
<div class="header header--auto">
Welcome!
</div>
</div>
OUTPUT
assert_template_result(expected, template, error_mode: :rigid)
end
def test_render_inline_snippet_forloop
template = <<~LIQUID.strip
{% snippet item %}
@ -1482,20 +897,23 @@ class SnippetTest < Minitest::Test
assert_template_result(expected, template, error_mode: :rigid)
end
def test_render_inline_snippet_inside_loop
template = <<~LIQUID.strip
{% assign color_scheme = 'dark' %}
{% assign array = '1,2,3' | split: ',' %}
{% for i in array %}
def test_render_captured_snippet
template = <<~LIQUID
{% snippet header %}
<div class="header header--{{ color_scheme }}">
{{ message }} {{ i }}
<div class="header">
{{ message }}
</div>
{% endsnippet %}
{% endfor %}
{% render header, ..., message: '👉' %}
{% capture up_header %}
{%- render header, message: 'Welcome!' -%}
{% endcapture %}
{{ up_header | upcase }}
{{ header | upcase }}
{{ header }}
LIQUID
expected = <<~OUTPUT
@ -1503,10 +921,14 @@ class SnippetTest < Minitest::Test
<DIV CLASS="HEADER">
WELCOME!
</DIV>
<div class="header header--dark">
👉#{" "}
</div>
SNIPPETDROP
SnippetDrop
OUTPUT
assert_template_result(expected, template, error_mode: :rigid)