From e6c1cd4b00649925ee9c35d5a002491e8edb796f Mon Sep 17 00:00:00 2001 From: Mike Vitousek Date: Sun, 30 Jun 2024 14:21:39 -0700 Subject: [PATCH] [compiler] Clone computation block in change detection mode Summary: In change-detection mode, we previously were spreading the contents of the computation block into the result twice. Other babel passes that cause in-place mutations of the AST would then be causing action at a distance and breaking the overall transform result. This pr creates clones of the nodes instead, so that mutations aren't reflected in both places where the block is used. [ghstack-poisoned] --- .../src/ReactiveScopes/CodegenReactiveFunction.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/CodegenReactiveFunction.ts b/compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/CodegenReactiveFunction.ts index 809f773ebda37..606d778e69301 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/CodegenReactiveFunction.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/CodegenReactiveFunction.ts @@ -673,7 +673,7 @@ function codegenReactiveScope( t.expressionStatement( t.callExpression(t.identifier(detectionFunction), [ t.identifier(loadName), - name, + t.cloneNode(name, true), t.stringLiteral(name.name), t.stringLiteral(cx.fnName), t.stringLiteral("cached"), @@ -684,8 +684,8 @@ function codegenReactiveScope( idempotenceDetectionStatements.push( t.expressionStatement( t.callExpression(t.identifier(detectionFunction), [ - slot, - name, + t.cloneNode(slot, true), + t.cloneNode(name, true), t.stringLiteral(name.name), t.stringLiteral(cx.fnName), t.stringLiteral("recomputed"), @@ -698,6 +698,7 @@ function codegenReactiveScope( ); } const condition = cx.synthesizeName("condition"); + const recomputationBlock = t.cloneNode(computationBlock, true); memoStatement = t.blockStatement([ ...computationBlock.body, t.variableDeclaration("let", [ @@ -714,7 +715,7 @@ function codegenReactiveScope( t.ifStatement( t.identifier(condition), t.blockStatement([ - ...computationBlock.body, + ...recomputationBlock.body, ...idempotenceDetectionStatements, ]) ),