未验证 提交 673685cd 编写于 作者: R Rafael França 提交者: GitHub

Merge pull request #38950 from joelhawksley/annotations-error-line

Raise template errors on correct line with annotations enabled
...@@ -334,8 +334,10 @@ def #{method_name}(local_assigns, output_buffer) ...@@ -334,8 +334,10 @@ def #{method_name}(local_assigns, output_buffer)
raise WrongEncodingError.new(source, Encoding.default_internal) raise WrongEncodingError.new(source, Encoding.default_internal)
end end
start_line = @handler.respond_to?(:start_line) ? @handler.start_line(self) : 0
begin begin
mod.module_eval(source, identifier, 0) mod.module_eval(source, identifier, start_line)
rescue SyntaxError rescue SyntaxError
# Account for when code in the template is not syntactically valid; e.g. if we're using # Account for when code in the template is not syntactically valid; e.g. if we're using
# ERB and the user writes <%= foo( %>, attempting to call a helper `foo` and interpolate # ERB and the user writes <%= foo( %>, attempting to call a helper `foo` and interpolate
......
...@@ -40,6 +40,15 @@ def handles_encoding? ...@@ -40,6 +40,15 @@ def handles_encoding?
true true
end end
# Line number to pass to #module_eval
#
# If we're annotating the template, we need to offset the starting
# line number passed to #module_eval so that errors in the template
# will be raised on the correct line.
def start_line(template)
annotate?(template) ? -1 : 0
end
def call(template, source) def call(template, source)
# First, convert to BINARY, so in case the encoding is # First, convert to BINARY, so in case the encoding is
# wrong, we can still find an encoding tag # wrong, we can still find an encoding tag
...@@ -55,16 +64,24 @@ def call(template, source) ...@@ -55,16 +64,24 @@ def call(template, source)
# Always make sure we return a String in the default_internal # Always make sure we return a String in the default_internal
erb.encode! erb.encode!
self.class.erb_implementation.new( options = {
erb,
escape: (self.class.escape_ignore_list.include? template.type), escape: (self.class.escape_ignore_list.include? template.type),
trim: (self.class.erb_trim_mode == "-"), trim: (self.class.erb_trim_mode == "-")
format: template.format, }
short_identifier: template.short_identifier
).src if annotate?(template)
options[:preamble] = "@output_buffer.safe_append='<!-- BEGIN #{template.short_identifier} -->\n';"
options[:postamble] = "@output_buffer.safe_append='<!-- END #{template.short_identifier} -->\n';@output_buffer.to_s"
end
self.class.erb_implementation.new(erb, options).src
end end
private private
def annotate?(template)
ActionView::Base.annotate_template_file_names && template.format == :html
end
def valid_encoding(string, encoding) def valid_encoding(string, encoding)
# If a magic encoding comment was found, tag the # If a magic encoding comment was found, tag the
# String with this encoding. This is for a case # String with this encoding. This is for a case
......
...@@ -14,14 +14,8 @@ def initialize(input, properties = {}) ...@@ -14,14 +14,8 @@ def initialize(input, properties = {})
# Dup properties so that we don't modify argument # Dup properties so that we don't modify argument
properties = Hash[properties] properties = Hash[properties]
# Annotate output with template file names, if we're rendering HTML properties[:preamble] ||= ""
if ActionView::Base.annotate_template_file_names && properties[:format] == :html properties[:postamble] ||= "@output_buffer.to_s"
properties[:preamble] = "@output_buffer.safe_append='<!-- BEGIN #{properties[:short_identifier]} -->\n';"
properties[:postamble] = "@output_buffer.safe_append='<!-- END #{properties[:short_identifier]} -->\n';@output_buffer.to_s"
else
properties[:preamble] = ""
properties[:postamble] = "@output_buffer.to_s"
end
properties[:bufvar] = "@output_buffer" properties[:bufvar] = "@output_buffer"
properties[:escapefunc] = "" properties[:escapefunc] = ""
......
...@@ -1482,4 +1482,18 @@ def test_template_annotations_do_not_render_for_non_html_format ...@@ -1482,4 +1482,18 @@ def test_template_annotations_do_not_render_for_non_html_format
ensure ensure
ActionView::Base.annotate_template_file_names = false ActionView::Base.annotate_template_file_names = false
end end
def test_line_offset_with_annotations_enabled
ActionView::Base.annotate_template_file_names = true
exc = assert_raises ActionView::Template::Error do
get :render_line_offset
end
line = exc.backtrace.first
assert(line =~ %r{:(\d+):})
assert_equal "1", $1,
"The line offset is wrong, perhaps the wrong exception has been raised, exception was: #{exc.inspect}"
ensure
ActionView::Base.annotate_template_file_names = false
end
end end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册