From 27c10193850e511605a07ab3f44ed5048e65314f Mon Sep 17 00:00:00 2001 From: Tristan Hume Date: Thu, 14 Aug 2014 16:23:38 -0400 Subject: [PATCH] Add line numbers to warnings --- lib/liquid/block.rb | 6 ++++-- lib/liquid/parser_switching.rb | 1 + lib/liquid/token.rb | 4 ++++ test/integration/error_handling_test.rb | 6 ++++++ 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/liquid/block.rb b/lib/liquid/block.rb index a53c282..6d45c71 100644 --- a/lib/liquid/block.rb +++ b/lib/liquid/block.rb @@ -32,7 +32,8 @@ module Liquid # fetch the tag from registered blocks if tag = Template.tags[$1] - new_tag = tag.parse($1, $2, tokens, @options) + markup = token.is_a?(Token) ? token.child($2) : $2 + new_tag = tag.parse($1, markup, tokens, @options) new_tag.line_number = token.line_number if token.is_a?(Token) @blank &&= new_tag.blank? @nodelist << new_tag @@ -103,7 +104,8 @@ module Liquid def create_variable(token) token.scan(ContentOfVariable) do |content| - return Variable.new(content.first, @options) + markup = token.is_a?(Token) ? token.child(content.first) : content.first + return Variable.new(markup, @options) end raise SyntaxError.new(options[:locale].t("errors.syntax.variable_termination".freeze, :token => token, :tag_end => VariableEnd.inspect)) end diff --git a/lib/liquid/parser_switching.rb b/lib/liquid/parser_switching.rb index 042419c..288fe7f 100644 --- a/lib/liquid/parser_switching.rb +++ b/lib/liquid/parser_switching.rb @@ -8,6 +8,7 @@ module Liquid begin return strict_parse_with_error_context(markup) rescue SyntaxError => e + e.line_number = markup.line_number if markup.is_a?(Token) @warnings ||= [] @warnings << e return lax_parse(markup) diff --git a/lib/liquid/token.rb b/lib/liquid/token.rb index 3a82014..acf8ef9 100644 --- a/lib/liquid/token.rb +++ b/lib/liquid/token.rb @@ -10,5 +10,9 @@ module Liquid def raw "" end + + def child(string) + Token.new(string, @line_number) + end end end diff --git a/test/integration/error_handling_test.rb b/test/integration/error_handling_test.rb index a66e43c..6bc2c23 100644 --- a/test/integration/error_handling_test.rb +++ b/test/integration/error_handling_test.rb @@ -121,6 +121,12 @@ class ErrorHandlingTest < Minitest::Test assert_equal '', template.render end + def test_warning_line_numbers + template = Liquid::Template.parse("{% if ~~~ %}\n{{%%%}}{% else %}\n{{ hello. }}{% endif %}", :error_mode => :warn, :line_numbers => true) + assert_equal 3, template.warnings.size + assert_equal [1,2,3], template.warnings.map(&:line_number) + end + # Liquid should not catch Exceptions that are not subclasses of StandardError, like Interrupt and NoMemoryError def test_exceptions_propagate assert_raises Exception do