Skip to content

Commit 6835136

Browse files
committed
Fix visitor in desugar test
* The #visit method is no longer called for every node since 2e6baa3. * As a consequence EnsureEveryNodeOnceInAST was only visiting ProgramNode for `visitor.visit(ast)` and no nodes at all for `ast.accept(visitor)`.
1 parent 570d17a commit 6835136

File tree

1 file changed

+8
-14
lines changed

1 file changed

+8
-14
lines changed

test/prism/desugar_compiler_test.rb

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,28 +54,22 @@ def ast_inspect(node)
5454
# Ensure every node is only present once in the AST.
5555
# If the same node is present twice it would most likely indicate it is executed twice, which is invalid semantically.
5656
# 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
57+
def ensure_every_node_once_in_ast(node, all_nodes = {}.compare_by_identity)
58+
if all_nodes.include?(node)
59+
raise "#{node.inspect} is present multiple times in the desugared AST and likely executed multiple times"
60+
else
61+
all_nodes[node] = true
6062
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)
63+
node.child_nodes.each do |child|
64+
ensure_every_node_once_in_ast(child, all_nodes) unless child.nil?
7165
end
7266
end
7367

7468
def assert_desugars(expected, source)
7569
ast = Prism.parse(source).value.accept(DesugarCompiler.new)
7670
assert_equal expected, ast_inspect(ast.statements.body.last)
7771

78-
ast.accept(EnsureEveryNodeOnceInAST.new)
72+
ensure_every_node_once_in_ast(ast)
7973
end
8074

8175
def assert_not_desugared(source, reason)

0 commit comments

Comments
 (0)