Fix infinite loop in BabelPlugin

After some painful debugging I isolated the infinite loop when attempting to use 
the BabelPlugin in hir-test rather than manually parsing and traversing it. The 
issue is that in the BabelPlugin we were replacing the original 
FunctionDeclaration with a new one, which would add it to Babel's traversal 
queue. This would effectively create an infinite loop where we would try to 
optimize a function that was already compiled by Forget (aside: _should_ running 
the compiler multiple times on code work?). 

To get around this we can just call the handy `skip` method on the new 
FunctionDeclaration to tell Babel to stop traversing it. I'm also moving the 
scope check here because I'll remove it from hir-test in a later commit.
This commit is contained in:
Lauren Tan
2023-01-19 10:10:56 -05:00
parent f42c51b971
commit 717719b8ea
+7
View File
@@ -24,8 +24,15 @@ export default function (babel: typeof BabelCore): PluginObj {
visitor: {
FunctionDeclaration: {
enter(fn, pass) {
if (fn.scope.getProgramParent() !== fn.scope.parent) {
return;
}
const ast = compile(fn);
// We are generating a new FunctionDeclaration node, so we must skip over it or this
// traversal will loop infinitely.
fn.replaceWith(ast);
fn.skip();
},
},
},