Skip to content

Commit 344a916

Browse files
kddnewtonbyroot
authored andcommitted
Support Prism for ActionView::Template.spot
1 parent 422ce1b commit 344a916

File tree

3 files changed

+48
-42
lines changed

3 files changed

+48
-42
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ GEM
410410
path_expander (1.1.1)
411411
pg (1.5.4)
412412
prettier_print (1.2.1)
413-
prism (0.27.0)
413+
prism (1.2.0)
414414
propshaft (1.0.0)
415415
actionpack (>= 7.0.0)
416416
activesupport (>= 7.0.0)

actionview/lib/action_view/template.rb

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -228,14 +228,22 @@ def locals
228228
end
229229
end
230230

231-
SOURCE_MAPPING_SUPPORTED = RUBY_VERSION < "3.4" # https://github.com/rails/rails/issues/52902
232-
233231
def spot(location) # :nodoc:
234-
ast = RubyVM::AbstractSyntaxTree.parse(compiled_source, keep_script_lines: true)
235232
node_id = RubyVM::AbstractSyntaxTree.node_id_for_backtrace_location(location)
236-
node = find_node_by_id(ast, node_id)
233+
found =
234+
if RubyVM::InstructionSequence.compile("").to_a[4][:parser] == :prism
235+
require "prism"
236+
237+
if Prism::VERSION >= "1.0.0"
238+
result = Prism.parse(compiled_source).value
239+
result.breadth_first_search { |node| node.node_id == node_id }
240+
end
241+
else
242+
node = RubyVM::AbstractSyntaxTree.parse(compiled_source, keep_script_lines: true)
243+
find_node_by_id(node, node_id)
244+
end
237245

238-
ErrorHighlight.spot(node)
246+
ErrorHighlight.spot(found) if found
239247
end
240248

241249
# Translate an error location returned by ErrorHighlight to the correct

actionview/test/template/render_test.rb

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -209,51 +209,49 @@ def test_render_outside_path
209209
end
210210
end
211211

212-
if ActionView::Template::SOURCE_MAPPING_SUPPORTED
213-
def test_render_template_with_syntax_error
214-
e = assert_raises(ActionView::Template::Error) { @view.render(template: "test/syntax_error") }
215-
assert_match %r!syntax!, e.message
216-
assert_equal "1: <%= foo(", e.annotated_source_code[0].strip
217-
end
212+
def test_render_template_with_syntax_error
213+
e = assert_raises(ActionView::Template::Error) { silence_warnings { @view.render(template: "test/syntax_error") } }
214+
assert_match %r!syntax!, e.message
215+
assert_equal "1: <%= foo(", e.annotated_source_code[0].strip
216+
end
218217

219-
def test_render_runtime_error
220-
ex = assert_raises(ActionView::Template::Error) {
221-
@view.render(template: "test/runtime_error")
222-
}
223-
erb_btl = ex.backtrace_locations.first
218+
def test_render_runtime_error
219+
ex = assert_raises(ActionView::Template::Error) {
220+
@view.render(template: "test/runtime_error")
221+
}
222+
erb_btl = ex.backtrace_locations.first
224223

225-
# Get the spot information from ErrorHighlight
226-
translating_frame = ActionDispatch::ExceptionWrapper::SourceMapLocation.new(erb_btl, ex.template)
227-
translated_spot = translating_frame.spot(ex.cause)
224+
# Get the spot information from ErrorHighlight
225+
translating_frame = ActionDispatch::ExceptionWrapper::SourceMapLocation.new(erb_btl, ex.template)
226+
translated_spot = translating_frame.spot(ex.cause)
228227

229-
assert_equal 6, translated_spot[:first_column]
230-
end
228+
assert_equal 6, translated_spot[:first_column]
229+
end
231230

232-
def test_render_location_conditional_append
233-
ex = assert_raises(ActionView::Template::Error) {
234-
@view.render(template: "test/unparseable_runtime_error")
235-
}
236-
erb_btl = ex.backtrace_locations.first
231+
def test_render_location_conditional_append
232+
ex = assert_raises(ActionView::Template::Error) {
233+
@view.render(template: "test/unparseable_runtime_error")
234+
}
235+
erb_btl = ex.backtrace_locations.first
237236

238-
# Get the spot information from ErrorHighlight
239-
translating_frame = ActionDispatch::ExceptionWrapper::SourceMapLocation.new(erb_btl, ex.template)
240-
translated_spot = translating_frame.spot(ex.cause)
237+
# Get the spot information from ErrorHighlight
238+
translating_frame = ActionDispatch::ExceptionWrapper::SourceMapLocation.new(erb_btl, ex.template)
239+
translated_spot = translating_frame.spot(ex.cause)
241240

242-
assert_equal 8, translated_spot[:first_column]
243-
end
241+
assert_equal 8, translated_spot[:first_column]
242+
end
244243

245-
def test_render_location_conditional_append_2
246-
ex = assert_raises(ActionView::Template::Error) {
247-
@view.render(template: "test/unparseable_runtime_error_2")
248-
}
249-
erb_btl = ex.backtrace_locations.first
244+
def test_render_location_conditional_append_2
245+
ex = assert_raises(ActionView::Template::Error) {
246+
@view.render(template: "test/unparseable_runtime_error_2")
247+
}
248+
erb_btl = ex.backtrace_locations.first
250249

251-
# Get the spot information from ErrorHighlight
252-
translating_frame = ActionDispatch::ExceptionWrapper::SourceMapLocation.new(erb_btl, ex.template)
253-
translated_spot = translating_frame.spot(ex.cause)
250+
# Get the spot information from ErrorHighlight
251+
translating_frame = ActionDispatch::ExceptionWrapper::SourceMapLocation.new(erb_btl, ex.template)
252+
translated_spot = translating_frame.spot(ex.cause)
254253

255-
assert_instance_of Integer, translated_spot[:first_column]
256-
end
254+
assert_instance_of Integer, translated_spot[:first_column]
257255
end
258256

259257
def test_render_partial

0 commit comments

Comments
 (0)