mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-20 11:20:41 -07:00
Raise error on invalid snippet name
This commit is contained in:
committed by
Guilherme Carreiro
parent
98fbd985d8
commit
5ceb0e9cec
@@ -10,7 +10,7 @@ module Liquid
|
|||||||
# @liquid_description
|
# @liquid_description
|
||||||
# You can create inline snippets to make your Liquid code more modular.
|
# You can create inline snippets to make your Liquid code more modular.
|
||||||
# @liquid_syntax
|
# @liquid_syntax
|
||||||
# {% snippet input %}
|
# {% snippet snippet_name %}
|
||||||
# value
|
# value
|
||||||
# {% endsnippet %}
|
# {% endsnippet %}
|
||||||
class Snippet < Block
|
class Snippet < Block
|
||||||
@@ -19,6 +19,7 @@ module Liquid
|
|||||||
p = @parse_context.new_parser(markup)
|
p = @parse_context.new_parser(markup)
|
||||||
if p.look(:id)
|
if p.look(:id)
|
||||||
@to = p.consume(:id)
|
@to = p.consume(:id)
|
||||||
|
p.consume(:end_of_string)
|
||||||
else
|
else
|
||||||
raise SyntaxError, options[:locale].t("errors.syntax.snippet")
|
raise SyntaxError, options[:locale].t("errors.syntax.snippet")
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -468,6 +468,18 @@ class SnippetTest < Minitest::Test
|
|||||||
|
|
||||||
assert_template_result(expected, template)
|
assert_template_result(expected, template)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_snippet_with_invalid_identifier
|
||||||
|
template = <<~LIQUID
|
||||||
|
{% snippet header foo bar %}
|
||||||
|
Invalid
|
||||||
|
{% endsnippet %}
|
||||||
|
LIQUID
|
||||||
|
|
||||||
|
exception = assert_raises(SyntaxError) { Liquid::Template.parse(template) }
|
||||||
|
|
||||||
|
assert_match("Expected end_of_string but found id", exception.message)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class RigidMode < SnippetTest
|
class RigidMode < SnippetTest
|
||||||
@@ -934,7 +946,7 @@ class SnippetTest < Minitest::Test
|
|||||||
assert_template_result(expected, template, error_mode: :rigid)
|
assert_template_result(expected, template, error_mode: :rigid)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_render_with_invalid_identifier_type
|
def test_render_with_invalid_identifier
|
||||||
template = "{% render 123 %}"
|
template = "{% render 123 %}"
|
||||||
|
|
||||||
exception = assert_raises(SyntaxError) do
|
exception = assert_raises(SyntaxError) do
|
||||||
@@ -954,14 +966,18 @@ class SnippetTest < Minitest::Test
|
|||||||
assert_match("Expected a string or identifier, found nothing", exception.message)
|
assert_match("Expected a string or identifier, found nothing", exception.message)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_render_with_invalid_identifier
|
def test_snippet_with_invalid_identifier
|
||||||
template = "{% render 123 %}"
|
template = <<~LIQUID
|
||||||
|
{% snippet header foo bar %}
|
||||||
|
Invalid
|
||||||
|
{% endsnippet %}
|
||||||
|
LIQUID
|
||||||
|
|
||||||
exception = assert_raises(SyntaxError) do
|
exception = assert_raises(SyntaxError) do
|
||||||
Liquid::Template.parse(template, error_mode: :rigid)
|
Liquid::Template.parse(template, error_mode: :rigid)
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_match("Expected a string or identifier, found 123", exception.message)
|
assert_match("Expected end_of_string but found id", exception.message)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user