mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-29 07:05:12 -07:00
Line numbers for all parse errors
This commit is contained in:
@@ -15,6 +15,7 @@ module Liquid
|
|||||||
@nodelist.clear
|
@nodelist.clear
|
||||||
|
|
||||||
while token = tokens.shift
|
while token = tokens.shift
|
||||||
|
begin
|
||||||
unless token.empty?
|
unless token.empty?
|
||||||
case
|
case
|
||||||
when token.start_with?(TAGSTART)
|
when token.start_with?(TAGSTART)
|
||||||
@@ -52,6 +53,10 @@ module Liquid
|
|||||||
@blank &&= (token =~ /\A\s*\z/)
|
@blank &&= (token =~ /\A\s*\z/)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
rescue SyntaxError => e
|
||||||
|
e.set_line_number_from_token(token)
|
||||||
|
raise
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Make sure that it's ok to end parsing in the current block.
|
# Make sure that it's ok to end parsing in the current block.
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ module Liquid
|
|||||||
|
|
||||||
def set_line_number_from_token(token)
|
def set_line_number_from_token(token)
|
||||||
return unless token.respond_to?(:line_number)
|
return unless token.respond_to?(:line_number)
|
||||||
|
return if self.line_number
|
||||||
self.line_number = token.line_number
|
self.line_number = token.line_number
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -100,6 +100,55 @@ class ErrorHandlingTest < Minitest::Test
|
|||||||
assert_equal Liquid::ArgumentError, template.errors.first.class
|
assert_equal Liquid::ArgumentError, template.errors.first.class
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_with_line_numbers_adds_numbers_to_parser_errors
|
||||||
|
err = assert_raises(SyntaxError) do
|
||||||
|
template = Liquid::Template.parse(%q{
|
||||||
|
foobar
|
||||||
|
|
||||||
|
{% "cat" | foobar %}
|
||||||
|
|
||||||
|
bla
|
||||||
|
},
|
||||||
|
:line_numbers => true
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_match /Liquid syntax error \(line 4\)/, err.message
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_parsing_warn_with_line_numbers_adds_numbers_to_lexer_errors
|
||||||
|
template = Liquid::Template.parse(%q{
|
||||||
|
foobar
|
||||||
|
|
||||||
|
{% if 1 =! 2 %}ok{% endif %}
|
||||||
|
|
||||||
|
bla
|
||||||
|
},
|
||||||
|
:error_mode => :warn,
|
||||||
|
:line_numbers => true
|
||||||
|
)
|
||||||
|
|
||||||
|
assert_equal ['Liquid syntax error (line 4): Unexpected character = in "1 =! 2"'],
|
||||||
|
template.warnings.map(&:message)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_parsing_strict_with_line_numbers_adds_numbers_to_lexer_errors
|
||||||
|
err = assert_raises(SyntaxError) do
|
||||||
|
Liquid::Template.parse(%q{
|
||||||
|
foobar
|
||||||
|
|
||||||
|
{% if 1 =! 2 %}ok{% endif %}
|
||||||
|
|
||||||
|
bla
|
||||||
|
},
|
||||||
|
:error_mode => :strict,
|
||||||
|
:line_numbers => true
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_equal 'Liquid syntax error (line 4): Unexpected character = in "1 =! 2"', err.message
|
||||||
|
end
|
||||||
|
|
||||||
def test_strict_error_messages
|
def test_strict_error_messages
|
||||||
err = assert_raises(SyntaxError) do
|
err = assert_raises(SyntaxError) do
|
||||||
Liquid::Template.parse(' {% if 1 =! 2 %}ok{% endif %} ', :error_mode => :strict)
|
Liquid::Template.parse(' {% if 1 =! 2 %}ok{% endif %} ', :error_mode => :strict)
|
||||||
|
|||||||
Reference in New Issue
Block a user