Skip to content

Commit 7b090bc

Browse files
committed
Ensure node are present only once in the desugared AST
1 parent 9a66737 commit 7b090bc

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

test/yarp/desugar_visitor_test.rb

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,36 @@ def ast_inspect(node)
5151
"(#{parts.join(" ")})"
5252
end
5353

54+
# Ensure every node is only present once in the AST.
55+
# If the same node is present twice it would most likely indicate it is executed twice, which is invalid semantically.
56+
# This also acts as a sanity check that Node#child_nodes returns only nodes or nil (which caught a couple bugs).
57+
class EnsureEveryNodeOnceInAST < Visitor
58+
def initialize
59+
@all_nodes = {}.compare_by_identity
60+
end
61+
62+
def visit(node)
63+
if node
64+
if @all_nodes.include?(node)
65+
raise "#{node.inspect} is present multiple times in the desugared AST and likely executed multiple times"
66+
else
67+
@all_nodes[node] = true
68+
end
69+
end
70+
super(node)
71+
end
72+
end
73+
5474
def assert_desugars(expected, source)
5575
ast = YARP.parse(source).value.accept(DesugarVisitor.new)
5676
assert_equal expected, ast_inspect(ast.statements.body.last)
77+
78+
ast.accept(EnsureEveryNodeOnceInAST.new)
5779
end
5880

5981
def assert_not_desugared(source, reason)
6082
ast = YARP.parse(source).value
61-
assert_equal_nodes(ast, ast.accept(YARP::DesugarVisitor.new))
83+
assert_equal_nodes(ast, ast.accept(DesugarVisitor.new))
6284
end
6385
end
6486
end

0 commit comments

Comments
 (0)