Skip to content

Commit

Permalink
Ensure node are present only once in the desugared AST
Browse files Browse the repository at this point in the history
  • Loading branch information
eregon committed Aug 30, 2023
1 parent e9ff318 commit 45c1aa5
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions test/yarp/desugar_visitor_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,24 @@ def ast_inspect(node)
def assert_desugars(expected, source)
ast = YARP.parse(source).value.accept(YARP::DesugarVisitor.new)
assert_equal expected, ast_inspect(ast.statements.body.last)

# Ensure every node is only present once in the AST.
# If the same node is present twice it would most likely indicate it is executed twice, which is invalid semantically.
# This also acts as a sanity check that Node#child_nodes returns only nodes or nil (which caught a couple bugs).
all_nodes = {}.compare_by_identity
visitor = Class.new(YARP::Visitor) do
define_method :visit do |node|
if node
if all_nodes.include?(node)
raise "#{node.inspect} is present multiple times in the desugared AST and likely executed multiple times"
else
all_nodes[node] = true
end
end
super(node)
end
end
ast.accept(visitor.new)
end

def assert_not_desugared(source, reason)
Expand Down

0 comments on commit 45c1aa5

Please sign in to comment.