From 1cccd6a81574ea28cd5fac5da73955e9547e0fd6 Mon Sep 17 00:00:00 2001 From: Sathya Gunasekaran Date: Thu, 27 Apr 2023 17:35:30 +0100 Subject: [PATCH] [hir] Clone babel nodes rather than reusing them There's state such as scope info sticking on to these nodes so reusing them can cause issues if a pass later on just uses scopes these directly. --- compiler/forget/src/ReactiveScopes/CodegenReactiveFunction.ts | 2 +- .../compiler/_bug.capturing-reference-changes-type.expect.md | 1 + .../compiler/_bug.lambda-reassign-primitive.expect.md | 1 + .../_bug.lambda-reassign-shadowed-primitive.expect.md | 1 + .../fixtures/compiler/capture-param-mutate.expect.md | 4 +--- .../compiler/capturing-fun-alias-captured-mutate-2.expect.md | 1 + .../capturing-fun-alias-captured-mutate-arr-2.expect.md | 1 + .../capturing-func-alias-captured-mutate-arr.expect.md | 1 + .../compiler/capturing-func-alias-captured-mutate.expect.md | 1 + .../compiler/capturing-func-alias-computed-mutate.expect.md | 1 + .../fixtures/compiler/capturing-func-alias-mutate.expect.md | 1 + .../capturing-func-alias-receiver-computed-mutate.expect.md | 1 + .../compiler/capturing-func-alias-receiver-mutate.expect.md | 1 + .../fixtures/compiler/capturing-func-mutate-2.expect.md | 1 + .../fixtures/compiler/capturing-func-mutate-nested.expect.md | 1 + .../fixtures/compiler/capturing-func-mutate.expect.md | 1 + .../fixtures/compiler/capturing-func-simple-alias.expect.md | 1 + .../capturing-function-capture-ref-before-rename.expect.md | 1 + .../fixtures/compiler/capturing-function-decl.expect.md | 1 + .../fixtures/compiler/function-declaration-simple.expect.md | 1 + .../fixtures/compiler/lambda-capture-returned-alias.expect.md | 1 + .../fixtures/compiler/lambda-mutate-shadowed-object.expect.md | 1 + 22 files changed, 22 insertions(+), 4 deletions(-) diff --git a/compiler/forget/src/ReactiveScopes/CodegenReactiveFunction.ts b/compiler/forget/src/ReactiveScopes/CodegenReactiveFunction.ts index 0ec506b548..99ca023b65 100644 --- a/compiler/forget/src/ReactiveScopes/CodegenReactiveFunction.ts +++ b/compiler/forget/src/ReactiveScopes/CodegenReactiveFunction.ts @@ -895,7 +895,7 @@ function codegenInstructionValue( break; } case "FunctionExpression": { - value = instrValue.expr; + value = t.cloneNode(instrValue.expr, true, true); break; } case "TaggedTemplateExpression": { diff --git a/compiler/forget/src/__tests__/fixtures/compiler/_bug.capturing-reference-changes-type.expect.md b/compiler/forget/src/__tests__/fixtures/compiler/_bug.capturing-reference-changes-type.expect.md index 5e73cf6a42..f33bdbb8ac 100644 --- a/compiler/forget/src/__tests__/fixtures/compiler/_bug.capturing-reference-changes-type.expect.md +++ b/compiler/forget/src/__tests__/fixtures/compiler/_bug.capturing-reference-changes-type.expect.md @@ -23,6 +23,7 @@ function component(a) { (function () { y = x; })(); + mutate(1); return 1; } diff --git a/compiler/forget/src/__tests__/fixtures/compiler/_bug.lambda-reassign-primitive.expect.md b/compiler/forget/src/__tests__/fixtures/compiler/_bug.lambda-reassign-primitive.expect.md index 484d364e08..0076462dc4 100644 --- a/compiler/forget/src/__tests__/fixtures/compiler/_bug.lambda-reassign-primitive.expect.md +++ b/compiler/forget/src/__tests__/fixtures/compiler/_bug.lambda-reassign-primitive.expect.md @@ -31,6 +31,7 @@ function Component() { const fn = function () { x = x + 1; }; + fn(); return 40; } diff --git a/compiler/forget/src/__tests__/fixtures/compiler/_bug.lambda-reassign-shadowed-primitive.expect.md b/compiler/forget/src/__tests__/fixtures/compiler/_bug.lambda-reassign-shadowed-primitive.expect.md index 3e5a2725d7..8bf43cc581 100644 --- a/compiler/forget/src/__tests__/fixtures/compiler/_bug.lambda-reassign-shadowed-primitive.expect.md +++ b/compiler/forget/src/__tests__/fixtures/compiler/_bug.lambda-reassign-shadowed-primitive.expect.md @@ -34,6 +34,7 @@ function Component() { const fn = function () { x_0 = 42; }; + fn(); return x; } diff --git a/compiler/forget/src/__tests__/fixtures/compiler/capture-param-mutate.expect.md b/compiler/forget/src/__tests__/fixtures/compiler/capture-param-mutate.expect.md index 87ca0c1427..9d3da37366 100644 --- a/compiler/forget/src/__tests__/fixtures/compiler/capture-param-mutate.expect.md +++ b/compiler/forget/src/__tests__/fixtures/compiler/capture-param-mutate.expect.md @@ -58,9 +58,7 @@ function getNativeLogFunction(level) { } else { str = Array.prototype.map .call(arguments, function (arg) { - return inspect(arg, { - depth: 10, - }); + return inspect(arg, { depth: 10 }); }) .join(", "); } diff --git a/compiler/forget/src/__tests__/fixtures/compiler/capturing-fun-alias-captured-mutate-2.expect.md b/compiler/forget/src/__tests__/fixtures/compiler/capturing-fun-alias-captured-mutate-2.expect.md index abcfecddab..d5cf3f872b 100644 --- a/compiler/forget/src/__tests__/fixtures/compiler/capturing-fun-alias-captured-mutate-2.expect.md +++ b/compiler/forget/src/__tests__/fixtures/compiler/capturing-fun-alias-captured-mutate-2.expect.md @@ -33,6 +33,7 @@ function component(foo, bar) { let b = x; a.x = b; })(); + mutate(y); $[0] = foo; $[1] = bar; diff --git a/compiler/forget/src/__tests__/fixtures/compiler/capturing-fun-alias-captured-mutate-arr-2.expect.md b/compiler/forget/src/__tests__/fixtures/compiler/capturing-fun-alias-captured-mutate-arr-2.expect.md index d542efe021..d47f2c868d 100644 --- a/compiler/forget/src/__tests__/fixtures/compiler/capturing-fun-alias-captured-mutate-arr-2.expect.md +++ b/compiler/forget/src/__tests__/fixtures/compiler/capturing-fun-alias-captured-mutate-arr-2.expect.md @@ -33,6 +33,7 @@ function component(foo, bar) { let b = x; a.x = b; })(); + mutate(y); $[0] = foo; $[1] = bar; diff --git a/compiler/forget/src/__tests__/fixtures/compiler/capturing-func-alias-captured-mutate-arr.expect.md b/compiler/forget/src/__tests__/fixtures/compiler/capturing-func-alias-captured-mutate-arr.expect.md index 447636ce63..f884b6821a 100644 --- a/compiler/forget/src/__tests__/fixtures/compiler/capturing-func-alias-captured-mutate-arr.expect.md +++ b/compiler/forget/src/__tests__/fixtures/compiler/capturing-func-alias-captured-mutate-arr.expect.md @@ -33,6 +33,7 @@ function component(foo, bar) { let b = x; a.x = b; })(); + mutate(y); $[0] = foo; $[1] = bar; diff --git a/compiler/forget/src/__tests__/fixtures/compiler/capturing-func-alias-captured-mutate.expect.md b/compiler/forget/src/__tests__/fixtures/compiler/capturing-func-alias-captured-mutate.expect.md index 7ce869a709..8cddac396c 100644 --- a/compiler/forget/src/__tests__/fixtures/compiler/capturing-func-alias-captured-mutate.expect.md +++ b/compiler/forget/src/__tests__/fixtures/compiler/capturing-func-alias-captured-mutate.expect.md @@ -33,6 +33,7 @@ function component(foo, bar) { let b = x; a.x = b; })(); + mutate(y); $[0] = foo; $[1] = bar; diff --git a/compiler/forget/src/__tests__/fixtures/compiler/capturing-func-alias-computed-mutate.expect.md b/compiler/forget/src/__tests__/fixtures/compiler/capturing-func-alias-computed-mutate.expect.md index 6f297a15b7..1292f4e7bb 100644 --- a/compiler/forget/src/__tests__/fixtures/compiler/capturing-func-alias-computed-mutate.expect.md +++ b/compiler/forget/src/__tests__/fixtures/compiler/capturing-func-alias-computed-mutate.expect.md @@ -28,6 +28,7 @@ function component(a) { (function () { y["x"] = x; })(); + mutate(y); $[0] = a; $[1] = y; diff --git a/compiler/forget/src/__tests__/fixtures/compiler/capturing-func-alias-mutate.expect.md b/compiler/forget/src/__tests__/fixtures/compiler/capturing-func-alias-mutate.expect.md index 2e5f129451..ea8ee02e09 100644 --- a/compiler/forget/src/__tests__/fixtures/compiler/capturing-func-alias-mutate.expect.md +++ b/compiler/forget/src/__tests__/fixtures/compiler/capturing-func-alias-mutate.expect.md @@ -28,6 +28,7 @@ function component(a) { (function () { y.x = x; })(); + mutate(y); $[0] = a; $[1] = y; diff --git a/compiler/forget/src/__tests__/fixtures/compiler/capturing-func-alias-receiver-computed-mutate.expect.md b/compiler/forget/src/__tests__/fixtures/compiler/capturing-func-alias-receiver-computed-mutate.expect.md index 184374b802..348eb4af84 100644 --- a/compiler/forget/src/__tests__/fixtures/compiler/capturing-func-alias-receiver-computed-mutate.expect.md +++ b/compiler/forget/src/__tests__/fixtures/compiler/capturing-func-alias-receiver-computed-mutate.expect.md @@ -30,6 +30,7 @@ function component(a) { let a_0 = y; a_0["x"] = x; })(); + mutate(y); $[0] = a; $[1] = y; diff --git a/compiler/forget/src/__tests__/fixtures/compiler/capturing-func-alias-receiver-mutate.expect.md b/compiler/forget/src/__tests__/fixtures/compiler/capturing-func-alias-receiver-mutate.expect.md index d63e7c46b7..443b9a92ee 100644 --- a/compiler/forget/src/__tests__/fixtures/compiler/capturing-func-alias-receiver-mutate.expect.md +++ b/compiler/forget/src/__tests__/fixtures/compiler/capturing-func-alias-receiver-mutate.expect.md @@ -30,6 +30,7 @@ function component(a) { let a_0 = y; a_0.x = x; })(); + mutate(y); $[0] = a; $[1] = y; diff --git a/compiler/forget/src/__tests__/fixtures/compiler/capturing-func-mutate-2.expect.md b/compiler/forget/src/__tests__/fixtures/compiler/capturing-func-mutate-2.expect.md index 6bb60c91d2..8f8985a7ee 100644 --- a/compiler/forget/src/__tests__/fixtures/compiler/capturing-func-mutate-2.expect.md +++ b/compiler/forget/src/__tests__/fixtures/compiler/capturing-func-mutate-2.expect.md @@ -40,6 +40,7 @@ function component(a, b) { z.a = 2; y.b; }; + x(); $[2] = a; $[3] = y.b; diff --git a/compiler/forget/src/__tests__/fixtures/compiler/capturing-func-mutate-nested.expect.md b/compiler/forget/src/__tests__/fixtures/compiler/capturing-func-mutate-nested.expect.md index b440fd42fa..693c6c54d4 100644 --- a/compiler/forget/src/__tests__/fixtures/compiler/capturing-func-mutate-nested.expect.md +++ b/compiler/forget/src/__tests__/fixtures/compiler/capturing-func-mutate-nested.expect.md @@ -26,6 +26,7 @@ function component(a) { x = function () { y.b.a = 2; }; + x(); $[0] = a; $[1] = x; diff --git a/compiler/forget/src/__tests__/fixtures/compiler/capturing-func-mutate.expect.md b/compiler/forget/src/__tests__/fixtures/compiler/capturing-func-mutate.expect.md index d4d249b3dc..740b0aaf4e 100644 --- a/compiler/forget/src/__tests__/fixtures/compiler/capturing-func-mutate.expect.md +++ b/compiler/forget/src/__tests__/fixtures/compiler/capturing-func-mutate.expect.md @@ -40,6 +40,7 @@ function component(a, b) { z.a = 2; y.b; }; + x(); $[0] = a; $[1] = b; diff --git a/compiler/forget/src/__tests__/fixtures/compiler/capturing-func-simple-alias.expect.md b/compiler/forget/src/__tests__/fixtures/compiler/capturing-func-simple-alias.expect.md index 881946930e..a4d53825ea 100644 --- a/compiler/forget/src/__tests__/fixtures/compiler/capturing-func-simple-alias.expect.md +++ b/compiler/forget/src/__tests__/fixtures/compiler/capturing-func-simple-alias.expect.md @@ -28,6 +28,7 @@ function component(a) { (function () { y = x; })(); + mutate(y); $[0] = a; $[1] = y; diff --git a/compiler/forget/src/__tests__/fixtures/compiler/capturing-function-capture-ref-before-rename.expect.md b/compiler/forget/src/__tests__/fixtures/compiler/capturing-function-capture-ref-before-rename.expect.md index edebe6eeef..98eca1f016 100644 --- a/compiler/forget/src/__tests__/fixtures/compiler/capturing-function-capture-ref-before-rename.expect.md +++ b/compiler/forget/src/__tests__/fixtures/compiler/capturing-function-capture-ref-before-rename.expect.md @@ -37,6 +37,7 @@ function component(a, b) { } else { z = $[1]; } + let y = z; const c_2 = $[2] !== b; let t0; diff --git a/compiler/forget/src/__tests__/fixtures/compiler/capturing-function-decl.expect.md b/compiler/forget/src/__tests__/fixtures/compiler/capturing-function-decl.expect.md index e1fcc401ff..2c46e3ae19 100644 --- a/compiler/forget/src/__tests__/fixtures/compiler/capturing-function-decl.expect.md +++ b/compiler/forget/src/__tests__/fixtures/compiler/capturing-function-decl.expect.md @@ -26,6 +26,7 @@ function component(a) { const x = function x() { t.foo(); }; + x(t); $[0] = a; $[1] = t; diff --git a/compiler/forget/src/__tests__/fixtures/compiler/function-declaration-simple.expect.md b/compiler/forget/src/__tests__/fixtures/compiler/function-declaration-simple.expect.md index 1cfe84085f..41c19d5c89 100644 --- a/compiler/forget/src/__tests__/fixtures/compiler/function-declaration-simple.expect.md +++ b/compiler/forget/src/__tests__/fixtures/compiler/function-declaration-simple.expect.md @@ -33,6 +33,7 @@ function component(a) { t0 = $[2]; } const x = t0; + x(t); $[0] = a; $[1] = t; diff --git a/compiler/forget/src/__tests__/fixtures/compiler/lambda-capture-returned-alias.expect.md b/compiler/forget/src/__tests__/fixtures/compiler/lambda-capture-returned-alias.expect.md index 8c0a5d7287..b811bbb873 100644 --- a/compiler/forget/src/__tests__/fixtures/compiler/lambda-capture-returned-alias.expect.md +++ b/compiler/forget/src/__tests__/fixtures/compiler/lambda-capture-returned-alias.expect.md @@ -54,6 +54,7 @@ function CaptureNotMutate(props) { const arr = { element }; return arr[idx]; }; + aliasedElement = fn(); mutate(aliasedElement); $[2] = props.el; diff --git a/compiler/forget/src/__tests__/fixtures/compiler/lambda-mutate-shadowed-object.expect.md b/compiler/forget/src/__tests__/fixtures/compiler/lambda-mutate-shadowed-object.expect.md index f023e622bf..49b3cc952a 100644 --- a/compiler/forget/src/__tests__/fixtures/compiler/lambda-mutate-shadowed-object.expect.md +++ b/compiler/forget/src/__tests__/fixtures/compiler/lambda-mutate-shadowed-object.expect.md @@ -35,6 +35,7 @@ function Component() { const fn = function () { mutate(x_0); }; + fn(); return x; }