File tree Expand file tree Collapse file tree 1 file changed +23
-1
lines changed Expand file tree Collapse file tree 1 file changed +23
-1
lines changed Original file line number Diff line number Diff line change @@ -51,14 +51,36 @@ def ast_inspect(node)
51
51
"(#{ parts . join ( " " ) } )"
52
52
end
53
53
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
+
54
74
def assert_desugars ( expected , source )
55
75
ast = YARP . parse ( source ) . value . accept ( DesugarVisitor . new )
56
76
assert_equal expected , ast_inspect ( ast . statements . body . last )
77
+
78
+ ast . accept ( EnsureEveryNodeOnceInAST . new )
57
79
end
58
80
59
81
def assert_not_desugared ( source , reason )
60
82
ast = YARP . parse ( source ) . value
61
- assert_equal_nodes ( ast , ast . accept ( YARP :: DesugarVisitor . new ) )
83
+ assert_equal_nodes ( ast , ast . accept ( DesugarVisitor . new ) )
62
84
end
63
85
end
64
86
end
You can’t perform that action at this time.
0 commit comments