Skip to content

Commit

Permalink
return correct line number regardless of whether annotations are on
Browse files Browse the repository at this point in the history
  • Loading branch information
joelhawksley committed Mar 31, 2020
1 parent bab6ace commit fb3cf49
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
14 changes: 13 additions & 1 deletion lib/view_component/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,20 @@ def compile(raise_template_errors: false)
return false
end

# If template name annotations are turned on, a line is dynamically
# added with a comment. In this case, we want to return a different
# starting line number so errors that are raised will point to the
# correct line in the component template.
line_number =
if ActionView::Base.respond_to?(:annotate_template_file_names) &&
ActionView::Base.annotate_template_file_names
-2
else
-1
end

templates.each do |template|
class_eval <<-RUBY, template[:path], -1
class_eval <<-RUBY, template[:path], line_number
def #{call_method_name(template[:variant])}
@output_buffer = ActionView::OutputBuffer.new
#{compiled_template(template[:path])}
Expand Down
2 changes: 1 addition & 1 deletion test/view_component/view_component_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ def test_backtrace_returns_correct_file_and_line_number
render_inline(ExceptionInTemplateComponent.new)
end

assert_match %r[app/components/exception_in_template_component\.html\.erb], error.backtrace[0]
assert_match %r[app/components/exception_in_template_component\.html\.erb:2], error.backtrace[0]
end

def test_render_collection
Expand Down

0 comments on commit fb3cf49

Please sign in to comment.