mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
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:
@@ -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();
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user