Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions lib/refract/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,20 @@ def self.visit(node_class, &)
)
end

# Prism represents a syntax error as MissingNode up to prism 1.9 and as
# ErrorRecoveryNode after it (ruby/ruby@708656c7, the prism ruby-head
# bundles). ErrorRecoveryNode adds an optional `unexpected` child, the stray
# node found in place of the missing one; like Prism's own translators we
# keep the node a leaf, since source with a syntax error can't be emitted
# either way. The original stays reachable through `prism_node`.
if Prism.const_defined?(:ErrorRecoveryNode)
visit Prism::ErrorRecoveryNode do |node|
MissingNode.new(
prism_node: node,
)
end
end

visit Prism::FalseNode do |node|
FalseNode.new(
prism_node: node,
Expand Down Expand Up @@ -842,10 +856,13 @@ def self.visit(node_class, &)
)
end

visit Prism::MissingNode do |node|
MissingNode.new(
prism_node: node,
)
# Prism <= 1.9 has MissingNode; later prism replaces it with ErrorRecoveryNode (above).
if Prism.const_defined?(:MissingNode)
visit Prism::MissingNode do |node|
MissingNode.new(
prism_node: node,
)
end
end

visit Prism::ModuleNode do |node|
Expand Down
14 changes: 14 additions & 0 deletions test/refract.test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1568,3 +1568,17 @@ class A
# shareable_constant_value: none
RUBY
end

test "a syntax error converts to a missing node" do
# MissingNode in prism <= 1.9, ErrorRecoveryNode after it (ruby-head).
node = Refract::Converter.new.visit(Prism.parse("x = ").value).statements.body.first

assert((node in Refract::LocalVariableWriteNode[name: :x, value: Refract::MissingNode]))
end

test "the converter handles whichever syntax error node prism defines" do
converter = Refract::Converter.new

assert_equal converter.respond_to?(:visit_missing_node), Prism.const_defined?(:MissingNode)
assert_equal converter.respond_to?(:visit_error_recovery_node), Prism.const_defined?(:ErrorRecoveryNode)
end
Loading