@samitouri / QOS-React-2 / commits / 717719b8ea

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.

Lauren Tan committed Jan 19, 2023 at 10:10 UTC 717719b8ea6dfb248b5cffa0224641f18de11d61
1 file changed +7
compiler/forget/src/Babel/BabelPlugin.ts
+7
@@ -24,8 +24,15 @@ export default function (babel: typeof BabelCore): PluginObj {
24 visitor: {
25 FunctionDeclaration: {
26 enter(fn, pass) {
27 + if (fn.scope.getProgramParent() !== fn.scope.parent) {
28 + return;
29 + }
30 const ast = compile(fn);
31 +
32 + // We are generating a new FunctionDeclaration node, so we must skip over it or this
33 + // traversal will loop infinitely.
34 fn.replaceWith(ast);
35 + fn.skip();
36 },
37 },
38 },