Retain locations when using babel cloneNode in lambdas
It turns out the third parameter to `cloneNode` is ["If the third parameter is true, the cloned nodes exclude location properties."](https://github.com/babel/babel/blob/c060e5e3d538a174a017edb7e75b07e80ccb55b7/packages/babel-types/src/clone/cloneNode.ts#L35-L39) strips away locations if its true, so to fix simply change this to false
Lauren Tan committed
Apr 27, 2023 at 16:25 UTC
d2f33dd524802245ce111917e4fd764b2657042f
25 files changed
+105
-67
compiler/forget/src/ReactiveScopes/CodegenReactiveFunction.ts
+1
-1
@@ -895,7 +895,7 @@ function codegenInstructionValue(
895
break;
896
}
897
case "FunctionExpression": {
898
- value = t.cloneNode(instrValue.expr, true, true);
898
+ value = t.cloneNode(instrValue.expr, true, false);
899
break;
900
}
901
case "TaggedTemplateExpression": {
compiler/forget/src/__tests__/fixtures/compiler/_bug.capturing-reference-changes-type.expect.md
-1
@@ -23,7 +23,6 @@ function component(a) {
23
(function () {
24
y = x;
25
})();
26
-
26
mutate(1);
27
return 1;
28
}
compiler/forget/src/__tests__/fixtures/compiler/_bug.lambda-reassign-primitive.expect.md
-1
@@ -31,7 +31,6 @@ function Component() {
31
const fn = function () {
32
x = x + 1;
33
};
34
-
34
fn();
35
return 40;
36
}
compiler/forget/src/__tests__/fixtures/compiler/_bug.lambda-reassign-shadowed-primitive.expect.md
-1
@@ -34,7 +34,6 @@ function Component() {
34
const fn = function () {
35
x_0 = 42;
36
};
37
-
37
fn();
38
return x;
39
}
compiler/forget/src/__tests__/fixtures/compiler/capture-param-mutate.expect.md
+3
-1
@@ -58,7 +58,9 @@ function getNativeLogFunction(level) {
58
} else {
59
str = Array.prototype.map
60
.call(arguments, function (arg) {
61
- return inspect(arg, { depth: 10 });
61
+ return inspect(arg, {
62
+ depth: 10,
63
+ });
64
})
65
.join(", ");
66
}
compiler/forget/src/__tests__/fixtures/compiler/capturing-fun-alias-captured-mutate-2.expect.md
-1
@@ -33,7 +33,6 @@ function component(foo, bar) {
33
let b = x;
34
a.x = b;
35
})();
36
-
36
mutate(y);
37
$[0] = foo;
38
$[1] = bar;
compiler/forget/src/__tests__/fixtures/compiler/capturing-fun-alias-captured-mutate-arr-2.expect.md
-1
@@ -33,7 +33,6 @@ function component(foo, bar) {
33
let b = x;
34
a.x = b;
35
})();
36
-
36
mutate(y);
37
$[0] = foo;
38
$[1] = bar;
compiler/forget/src/__tests__/fixtures/compiler/capturing-func-alias-captured-mutate-arr.expect.md
-1
@@ -33,7 +33,6 @@ function component(foo, bar) {
33
let b = x;
34
a.x = b;
35
})();
36
-
36
mutate(y);
37
$[0] = foo;
38
$[1] = bar;
compiler/forget/src/__tests__/fixtures/compiler/capturing-func-alias-captured-mutate.expect.md
-1
@@ -33,7 +33,6 @@ function component(foo, bar) {
33
let b = x;
34
a.x = b;
35
})();
36
-
36
mutate(y);
37
$[0] = foo;
38
$[1] = bar;
compiler/forget/src/__tests__/fixtures/compiler/capturing-func-alias-computed-mutate.expect.md
-1
@@ -28,7 +28,6 @@ function component(a) {
28
(function () {
29
y["x"] = x;
30
})();
31
-
31
mutate(y);
32
$[0] = a;
33
$[1] = y;
compiler/forget/src/__tests__/fixtures/compiler/capturing-func-alias-mutate.expect.md
-1
@@ -28,7 +28,6 @@ function component(a) {
28
(function () {
29
y.x = x;
30
})();
31
-
31
mutate(y);
32
$[0] = a;
33
$[1] = y;
compiler/forget/src/__tests__/fixtures/compiler/capturing-func-alias-receiver-computed-mutate.expect.md
-1
@@ -30,7 +30,6 @@ function component(a) {
30
let a_0 = y;
31
a_0["x"] = x;
32
})();
33
-
33
mutate(y);
34
$[0] = a;
35
$[1] = y;
compiler/forget/src/__tests__/fixtures/compiler/capturing-func-alias-receiver-mutate.expect.md
-1
@@ -30,7 +30,6 @@ function component(a) {
30
let a_0 = y;
31
a_0.x = x;
32
})();
33
-
33
mutate(y);
34
$[0] = a;
35
$[1] = y;
compiler/forget/src/__tests__/fixtures/compiler/capturing-func-mutate-2.expect.md
-1
@@ -40,7 +40,6 @@ function component(a, b) {
40
z.a = 2;
41
y.b;
42
};
43
-
43
x();
44
$[2] = a;
45
$[3] = y.b;
compiler/forget/src/__tests__/fixtures/compiler/capturing-func-mutate-nested.expect.md
-1
@@ -26,7 +26,6 @@ function component(a) {
26
x = function () {
27
y.b.a = 2;
28
};
29
-
29
x();
30
$[0] = a;
31
$[1] = x;
compiler/forget/src/__tests__/fixtures/compiler/capturing-func-mutate.expect.md
-1
@@ -40,7 +40,6 @@ function component(a, b) {
40
z.a = 2;
41
y.b;
42
};
43
-
43
x();
44
$[0] = a;
45
$[1] = b;
compiler/forget/src/__tests__/fixtures/compiler/capturing-func-simple-alias.expect.md
-1
@@ -28,7 +28,6 @@ function component(a) {
28
(function () {
29
y = x;
30
})();
31
-
31
mutate(y);
32
$[0] = a;
33
$[1] = y;
compiler/forget/src/__tests__/fixtures/compiler/capturing-function-capture-ref-before-rename.expect.md
-1
@@ -37,7 +37,6 @@ function component(a, b) {
37
} else {
38
z = $[1];
39
}
40
-
40
let y = z;
41
const c_2 = $[2] !== b;
42
let t0;
compiler/forget/src/__tests__/fixtures/compiler/capturing-function-decl.expect.md
-1
@@ -26,7 +26,6 @@ function component(a) {
26
const x = function x() {
27
t.foo();
28
};
29
-
29
x(t);
30
$[0] = a;
31
$[1] = t;
compiler/forget/src/__tests__/fixtures/compiler/error.lambda-with-fbt.expect.md
deleted
-45
@@ -1,45 +0,0 @@
1
-
2
-## Input
3
-
4
-```javascript
5
-import { fbt } from "fbt";
6
-
7
-function Component() {
8
- const buttonLabel = () => {
9
- if (!someCondition) {
10
- return <fbt desc="My label">{"Purchase as a gift"}</fbt>;
11
- } else if (
12
- !iconOnly &&
13
- showPrice &&
14
- item?.current_gift_offer?.price?.formatted != null
15
- ) {
16
- return (
17
- <fbt desc="Gift button's label">
18
- {"Gift | "}
19
- <fbt:param name="price">
20
- {item?.current_gift_offer?.price?.formatted}
21
- </fbt:param>
22
- </fbt>
23
- );
24
- } else if (!iconOnly && !showPrice) {
25
- return <fbt desc="Gift button's label">{"Gift"}</fbt>;
26
- }
27
- };
28
-
29
- return (
30
- <View>
31
- <Button text={buttonLabel()} />
32
- </View>
33
- );
34
-}
35
-
36
-```
37
-
38
-
39
-## Error
40
-
41
-```
42
-Cannot read properties of null (reading 'end')
43
-```
44
-
45
-
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/compiler/function-declaration-simple.expect.md
-1
@@ -33,7 +33,6 @@ function component(a) {
33
t0 = $[2];
34
}
35
const x = t0;
36
-
36
x(t);
37
$[0] = a;
38
$[1] = t;
compiler/forget/src/__tests__/fixtures/compiler/lambda-capture-returned-alias.expect.md
-1
@@ -54,7 +54,6 @@ function CaptureNotMutate(props) {
54
const arr = { element };
55
return arr[idx];
56
};
57
-
57
aliasedElement = fn();
58
mutate(aliasedElement);
59
$[2] = props.el;
compiler/forget/src/__tests__/fixtures/compiler/lambda-mutate-shadowed-object.expect.md
-1
@@ -35,7 +35,6 @@ function Component() {
35
const fn = function () {
36
mutate(x_0);
37
};
38
-
38
fn();
39
return x;
40
}
compiler/forget/src/__tests__/fixtures/compiler/lambda-with-fbt.expect.md
new
+101
@@ -0,0 +1,101 @@
1
+
2
+## Input
3
+
4
+```javascript
5
+import { fbt } from "fbt";
6
+
7
+function Component() {
8
+ const buttonLabel = () => {
9
+ if (!someCondition) {
10
+ return <fbt desc="My label">{"Purchase as a gift"}</fbt>;
11
+ } else if (
12
+ !iconOnly &&
13
+ showPrice &&
14
+ item?.current_gift_offer?.price?.formatted != null
15
+ ) {
16
+ return (
17
+ <fbt desc="Gift button's label">
18
+ {"Gift | "}
19
+ <fbt:param name="price">
20
+ {item?.current_gift_offer?.price?.formatted}
21
+ </fbt:param>
22
+ </fbt>
23
+ );
24
+ } else if (!iconOnly && !showPrice) {
25
+ return <fbt desc="Gift button's label">{"Gift"}</fbt>;
26
+ }
27
+ };
28
+
29
+ return (
30
+ <View>
31
+ <Button text={buttonLabel()} />
32
+ </View>
33
+ );
34
+}
35
+
36
+```
37
+
38
+## Code
39
+
40
+```javascript
41
+import { unstable_useMemoCache as useMemoCache } from "react";
42
+import { fbt } from "fbt";
43
+
44
+function Component() {
45
+ const $ = useMemoCache(4);
46
+ let t0;
47
+ if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
48
+ t0 = () => {
49
+ if (!someCondition) {
50
+ return fbt._("Purchase as a gift", null, { hk: "1gHj4g" });
51
+ } else if (
52
+ !iconOnly &&
53
+ showPrice &&
54
+ item?.current_gift_offer?.price?.formatted != null
55
+ ) {
56
+ return fbt._(
57
+ "Gift | {price}",
58
+ [
59
+ fbt._param(
60
+ "price",
61
+
62
+ item?.current_gift_offer?.price?.formatted
63
+ ),
64
+ ],
65
+ { hk: "3GTnGE" }
66
+ );
67
+ } else if (!iconOnly && !showPrice) {
68
+ return fbt._("Gift", null, { hk: "3fqfrk" });
69
+ }
70
+ };
71
+ $[0] = t0;
72
+ } else {
73
+ t0 = $[0];
74
+ }
75
+ const buttonLabel = t0;
76
+ let t1;
77
+ if ($[1] === Symbol.for("react.memo_cache_sentinel")) {
78
+ t1 = buttonLabel();
79
+ $[1] = t1;
80
+ } else {
81
+ t1 = $[1];
82
+ }
83
+ let t2;
84
+ if ($[2] === Symbol.for("react.memo_cache_sentinel")) {
85
+ t2 = <Button text={t1} />;
86
+ $[2] = t2;
87
+ } else {
88
+ t2 = $[2];
89
+ }
90
+ let t3;
91
+ if ($[3] === Symbol.for("react.memo_cache_sentinel")) {
92
+ t3 = <View>{t2}</View>;
93
+ $[3] = t3;
94
+ } else {
95
+ t3 = $[3];
96
+ }
97
+ return t3;
98
+}
99
+
100
+```
101
+
\ No newline at end of file