Skip to content

Commit

Permalink
[compiler] Clone computation block in change detection mode
Browse files Browse the repository at this point in the history
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-source-id: b78def8d8d1b8f9978df0a231f64fdeda786a3a3
Pull Request resolved: #30148
  • Loading branch information
mvitousek committed Jul 1, 2024
1 parent 05cebff commit d6b1c48
Showing 1 changed file with 5 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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"),
Expand All @@ -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", [
Expand All @@ -714,7 +715,7 @@ function codegenReactiveScope(
t.ifStatement(
t.identifier(condition),
t.blockStatement([
...computationBlock.body,
...recomputationBlock.body,
...idempotenceDetectionStatements,
])
),
Expand Down

0 comments on commit d6b1c48

Please sign in to comment.