mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-19 19:00:39 -07:00
Missing inline snippets should display same error as filebased
This commit is contained in:
committed by
Guilherme Carreiro
parent
5ceb0e9cec
commit
12fd93fbe2
@@ -34,3 +34,5 @@
|
|||||||
render: "Argument error in tag 'render' - Dynamically chosen templates are not allowed"
|
render: "Argument error in tag 'render' - Dynamically chosen templates are not allowed"
|
||||||
disabled:
|
disabled:
|
||||||
tag: "usage is not allowed in this context"
|
tag: "usage is not allowed in this context"
|
||||||
|
file_system:
|
||||||
|
includes: "This liquid context does not allow includes"
|
||||||
|
|||||||
@@ -2,12 +2,13 @@
|
|||||||
|
|
||||||
module Liquid
|
module Liquid
|
||||||
class SnippetDrop < Drop
|
class SnippetDrop < Drop
|
||||||
attr_reader :body, :name
|
attr_reader :body, :name, :filename
|
||||||
|
|
||||||
def initialize(body, name)
|
def initialize(body, name, filename)
|
||||||
super()
|
super()
|
||||||
@body = body
|
@body = body
|
||||||
@name = name
|
@name = name
|
||||||
|
@filename = filename
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_partial
|
def to_partial
|
||||||
|
|||||||
@@ -51,16 +51,16 @@ module Liquid
|
|||||||
|
|
||||||
if template.respond_to?(:to_partial)
|
if template.respond_to?(:to_partial)
|
||||||
partial = template.to_partial
|
partial = template.to_partial
|
||||||
template_name = template.name
|
template_name = template.filename
|
||||||
|
context_variable_name = @alias_name || template.name
|
||||||
elsif @template_name_expr.is_a?(String)
|
elsif @template_name_expr.is_a?(String)
|
||||||
partial = PartialCache.load(template, context: context, parse_context: parse_context)
|
partial = PartialCache.load(template, context: context, parse_context: parse_context)
|
||||||
template_name = partial.name
|
template_name = partial.name
|
||||||
|
context_variable_name = @alias_name || template_name.split('/').last
|
||||||
else
|
else
|
||||||
raise ::ArgumentError, parse_context.locale.t("errors.argument.render")
|
raise Liquid::ArgumentError, parse_context.locale.t("errors.file_system.includes")
|
||||||
end
|
end
|
||||||
|
|
||||||
context_variable_name = @alias_name || template_name.split('/').last
|
|
||||||
|
|
||||||
render_partial_func = ->(var, forloop) {
|
render_partial_func = ->(var, forloop) {
|
||||||
inner_context = context.new_isolated_subcontext
|
inner_context = context.new_isolated_subcontext
|
||||||
inner_context.template_name = template_name
|
inner_context.template_name = template_name
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ module Liquid
|
|||||||
end
|
end
|
||||||
|
|
||||||
def render_to_output_buffer(context, output)
|
def render_to_output_buffer(context, output)
|
||||||
snippet_drop = SnippetDrop.new(@body, @to)
|
snippet_drop = SnippetDrop.new(@body, @to, context.template_name)
|
||||||
context.scopes.last[@to] = snippet_drop
|
context.scopes.last[@to] = snippet_drop
|
||||||
context.resource_limits.increment_assign_score(assign_score_of(snippet_drop))
|
context.resource_limits.increment_assign_score(assign_score_of(snippet_drop))
|
||||||
output
|
output
|
||||||
|
|||||||
@@ -101,13 +101,6 @@ class RenderTagTest < Minitest::Test
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_dynamically_chosen_templates_are_not_allowed
|
|
||||||
error = assert_raises(::ArgumentError) do
|
|
||||||
Template.parse('{% assign name = "snippet" %}{% render name %}').render!
|
|
||||||
end
|
|
||||||
assert_equal("Argument error in tag 'render' - Dynamically chosen templates are not allowed", error.message)
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_rigid_parsing_errors
|
def test_rigid_parsing_errors
|
||||||
with_error_modes(:lax, :strict) do
|
with_error_modes(:lax, :strict) do
|
||||||
assert_template_result(
|
assert_template_result(
|
||||||
|
|||||||
@@ -480,6 +480,54 @@ class SnippetTest < Minitest::Test
|
|||||||
|
|
||||||
assert_match("Expected end_of_string but found id", exception.message)
|
assert_match("Expected end_of_string but found id", exception.message)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_render_with_non_existent_tag
|
||||||
|
template = Liquid::Template.parse(<<~LIQUID.chomp, line_numbers: true)
|
||||||
|
{% snippet foo %}
|
||||||
|
{% render non_existent %}
|
||||||
|
{% endsnippet %}
|
||||||
|
|
||||||
|
{% render foo %}
|
||||||
|
LIQUID
|
||||||
|
|
||||||
|
expected = <<~TEXT
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Liquid error (index line 2): This liquid context does not allow includes
|
||||||
|
TEXT
|
||||||
|
template.name = "index"
|
||||||
|
|
||||||
|
assert_equal(expected, template.render('errors' => ErrorDrop.new))
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_render_handles_errors
|
||||||
|
template = Liquid::Template.parse(<<~LIQUID.chomp, line_numbers: true)
|
||||||
|
{% snippet foo %}
|
||||||
|
{% render non_existent %} will raise an error.
|
||||||
|
|
||||||
|
Bla bla test.
|
||||||
|
|
||||||
|
This is an argument error: {{ 'test' | slice: 'not a number' }}
|
||||||
|
{% endsnippet %}
|
||||||
|
|
||||||
|
{% render foo %}
|
||||||
|
LIQUID
|
||||||
|
|
||||||
|
expected = <<~TEXT
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Liquid error (index line 2): This liquid context does not allow includes will raise an error.
|
||||||
|
|
||||||
|
Bla bla test.
|
||||||
|
|
||||||
|
This is an argument error: Liquid error (index line 6): invalid integer
|
||||||
|
TEXT
|
||||||
|
template.name = "index"
|
||||||
|
|
||||||
|
assert_equal(expected, template.render('errors' => ErrorDrop.new))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class RigidMode < SnippetTest
|
class RigidMode < SnippetTest
|
||||||
@@ -956,6 +1004,54 @@ class SnippetTest < Minitest::Test
|
|||||||
assert_match("Expected a string or identifier, found 123", exception.message)
|
assert_match("Expected a string or identifier, found 123", exception.message)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_render_with_non_existent_tag
|
||||||
|
template = Liquid::Template.parse(<<~LIQUID.chomp, line_numbers: true, error_mode: :rigid)
|
||||||
|
{% snippet foo %}
|
||||||
|
{% render non_existent %}
|
||||||
|
{% endsnippet %}
|
||||||
|
|
||||||
|
{% render foo %}
|
||||||
|
LIQUID
|
||||||
|
|
||||||
|
expected = <<~TEXT
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Liquid error (index line 2): This liquid context does not allow includes
|
||||||
|
TEXT
|
||||||
|
template.name = "index"
|
||||||
|
|
||||||
|
assert_equal(expected, template.render('errors' => ErrorDrop.new))
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_render_handles_errors
|
||||||
|
template = Liquid::Template.parse(<<~LIQUID.chomp, line_numbers: true, error_mode: :rigid)
|
||||||
|
{% snippet foo %}
|
||||||
|
{% render non_existent %} will raise an error.
|
||||||
|
|
||||||
|
Bla bla test.
|
||||||
|
|
||||||
|
This is an argument error: {{ 'test' | slice: 'not a number' }}
|
||||||
|
{% endsnippet %}
|
||||||
|
|
||||||
|
{% render foo %}
|
||||||
|
LIQUID
|
||||||
|
|
||||||
|
expected = <<~TEXT
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Liquid error (index line 2): This liquid context does not allow includes will raise an error.
|
||||||
|
|
||||||
|
Bla bla test.
|
||||||
|
|
||||||
|
This is an argument error: Liquid error (index line 6): invalid integer
|
||||||
|
TEXT
|
||||||
|
template.name = "index"
|
||||||
|
|
||||||
|
assert_equal(expected, template.render('errors' => ErrorDrop.new))
|
||||||
|
end
|
||||||
|
|
||||||
def test_render_with_no_identifier
|
def test_render_with_no_identifier
|
||||||
template = "{% render %}"
|
template = "{% render %}"
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user