More tests for noAlias
Adds another test for `noAlias` to show that if part of the receiver could escape that we still memoize it.
Joe Savona committed
Sep 18, 2023 at 14:25 UTC
0810175bba99435e43181f2d38b0e82aa11a650f
4 files changed
+67
-1
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/array-map-captures-receiver-noAlias.expect.md
new
+50
@@ -0,0 +1,50 @@
1
+
2
+## Input
3
+
4
+```javascript
5
+// @enableNoAliasOptimizations
6
+function Component(props) {
7
+ // This item is part of the receiver, should be memoized
8
+ const item = { a: props.a };
9
+ const items = [item];
10
+ const mapped = items.map((item) => item);
11
+ return mapped;
12
+}
13
+
14
+export const FIXTURE_ENTRYPOINT = {
15
+ fn: Component,
16
+ params: [{ a: { id: 42 } }],
17
+ isComponent: false,
18
+};
19
+
20
+```
21
+
22
+## Code
23
+
24
+```javascript
25
+import { unstable_useMemoCache as useMemoCache } from "react"; // @enableNoAliasOptimizations
26
+function Component(props) {
27
+ const $ = useMemoCache(2);
28
+ const c_0 = $[0] !== props.a;
29
+ let t0;
30
+ if (c_0) {
31
+ const item = { a: props.a };
32
+ const items = [item];
33
+ t0 = items.map((item_0) => item_0);
34
+ $[0] = props.a;
35
+ $[1] = t0;
36
+ } else {
37
+ t0 = $[1];
38
+ }
39
+ const mapped = t0;
40
+ return mapped;
41
+}
42
+
43
+export const FIXTURE_ENTRYPOINT = {
44
+ fn: Component,
45
+ params: [{ a: { id: 42 } }],
46
+ isComponent: false,
47
+};
48
+
49
+```
50
+
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/array-map-captures-receiver-noAlias.js
new
+14
@@ -0,0 +1,14 @@
1
+// @enableNoAliasOptimizations
2
+function Component(props) {
3
+ // This item is part of the receiver, should be memoized
4
+ const item = { a: props.a };
5
+ const items = [item];
6
+ const mapped = items.map((item) => item);
7
+ return mapped;
8
+}
9
+
10
+export const FIXTURE_ENTRYPOINT = {
11
+ fn: Component,
12
+ params: [{ a: { id: 42 } }],
13
+ isComponent: false,
14
+};
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/array-map-noAlias-escaping-function.expect.md
+2
-1
@@ -2,6 +2,7 @@
2
## Input
3
4
```javascript
5
+// @enableNoAliasOptimizations
6
function Component(props) {
7
const f = (item) => item;
8
const x = [...props.items].map(f); // `f` doesn't escape here...
@@ -19,7 +20,7 @@ export const FIXTURE_ENTRYPOINT = {
20
## Code
21
22
```javascript
22
-import { unstable_useMemoCache as useMemoCache } from "react";
23
+import { unstable_useMemoCache as useMemoCache } from "react"; // @enableNoAliasOptimizations
24
function Component(props) {
25
const $ = useMemoCache(5);
26
let t0;
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/array-map-noAlias-escaping-function.js
+1
@@ -1,3 +1,4 @@
1
+// @enableNoAliasOptimizations
2
function Component(props) {
3
const f = (item) => item;
4
const x = [...props.items].map(f); // `f` doesn't escape here...