Skip to content

Commit 018ee8d

Browse files
committed
fix bug with Rails 8.1 ERB compilation
1 parent edb7638 commit 018ee8d

File tree

6 files changed

+61
-2
lines changed

6 files changed

+61
-2
lines changed

docs/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ nav_order: 6
1010

1111
## main
1212

13+
* Fix bug where error line numbers were incorrect in Rails 8.1.
14+
15+
*Joel Hawksley*
16+
1317
## 4.1.1
1418

1519
* Add Consultport to list of companies using ViewComponent.

lib/view_component/template.rb

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,21 @@ def initialize(component:, details:, lineno: nil, path: nil)
2121

2222
class File < Template
2323
def initialize(component:, details:, path:)
24+
# Rails 8.1 added a newline to the compiled ERB output in
25+
# https://github.com/rails/rails/pull/53731
26+
lineno = Rails::VERSION::STRING > "8.0" ? -1 : 0
27+
lineno =
28+
if Rails::VERSION::MAJOR == 8 && Rails::VERSION::MINOR > 0 && details.handler == :erb
29+
- 1
30+
else
31+
0
32+
end
33+
2434
super(
2535
component: component,
2636
details: details,
2737
path: path,
28-
lineno: 0
38+
lineno: lineno
2939
)
3040
end
3141

@@ -45,11 +55,20 @@ class Inline < Template
4555
def initialize(component:, inline_template:)
4656
details = ActionView::TemplateDetails.new(nil, inline_template.language.to_sym, nil, nil)
4757

58+
# Rails 8.1 added a newline to the compiled ERB output in
59+
# https://github.com/rails/rails/pull/53731
60+
lineno =
61+
if Rails::VERSION::MAJOR == 8 && Rails::VERSION::MINOR > 0 && details.handler == :erb
62+
inline_template.lineno - 1
63+
else
64+
inline_template.lineno
65+
end
66+
4867
super(
4968
component: component,
5069
details: details,
5170
path: inline_template.path,
52-
lineno: inline_template.lineno,
71+
lineno: lineno,
5372
)
5473

5574
@source = inline_template.source.dup
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
p
2+
= a_method_that_does_not_exist_i_hope
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# frozen_string_literal: true
2+
3+
class ExceptionInSlimTemplateComponent < ViewComponent::Base
4+
end

test/sandbox/test/inline_template_test.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ def initialize(name)
2727
end
2828
end
2929

30+
class InlineRaiseSlimComponent < ViewComponent::Base
31+
attr_reader :name
32+
33+
slim_template <<~SLIM
34+
= raise ArgumentError, "oh no"
35+
SLIM
36+
37+
def initialize(name)
38+
@name = name
39+
end
40+
end
41+
3042
class InlineErbSubclassComponent < InlineErbComponent
3143
erb_template <<~ERB
3244
<h1>Hey, <%= name %>!</h1>
@@ -107,6 +119,14 @@ class InlineComponentDerivedFromComponentSupportingVariants < Level2Component
107119
assert_match %r{test/sandbox/test/inline_template_test.rb:22}, error.backtrace[0]
108120
end
109121

122+
test "error backtrace locations work in slim" do
123+
error = assert_raises ArgumentError do
124+
render_inline(InlineRaiseSlimComponent.new("Fox Mulder"))
125+
end
126+
127+
assert_match %r{test/sandbox/test/inline_template_test.rb:34}, error.backtrace[0]
128+
end
129+
110130
test "renders inline slim templates" do
111131
render_inline(InlineSlimComponent.new("Fox Mulder"))
112132

test/sandbox/test/rendering_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,16 @@ def test_backtrace_returns_correct_file_and_line_number
645645
assert_match %r{app/components/exception_in_template_component\.html\.erb:2}, error.backtrace[component_error_index]
646646
end
647647

648+
def test_backtrace_returns_correct_file_and_line_number_in_slim
649+
error =
650+
assert_raises NameError do
651+
render_inline(ExceptionInSlimTemplateComponent.new)
652+
end
653+
654+
component_error_index = (Rails::VERSION::STRING < "8.0") ? 0 : 1
655+
assert_match %r{app/components/exception_in_slim_template_component\.html\.slim:2}, error.backtrace[component_error_index]
656+
end
657+
648658
def test_render_collection
649659
products = [Product.new(name: "Radio clock"), Product.new(name: "Mints")]
650660
render_inline(ProductComponent.with_collection(products, notice: "On sale"))

0 commit comments

Comments
 (0)