@samitouri / QOS-React-2 / commits / 6a71868d4c

[sprout] Convert for-of-mutate to sprout

Lauren Tan committed Sep 5, 2023 at 12:35 UTC 6a71868d4c159c8089f0c70f17bfcf2abc68173d
5 files changed +47 -8
compiler/crates/forget_hermes_parser/tests/fixtures/for-of-mutate.tsx renamed
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/for-of-mutate.expect.md
+26 -6
@@ -2,29 +2,43 @@
2 ## Input
3
4 ```javascript
5 -function Component(props) {
6 - const collection = [makeObject()];
5 +import { makeObject_Primitives, mutateObject, Stringify } from "shared-runtime";
6 +
7 +function Component(_props) {
8 + const collection = [makeObject_Primitives()];
9 const results = [];
10 for (const item of collection) {
9 - results.push(<div>{mutate(item)}</div>);
11 + results.push(
12 + <div key={Stringify(item)}>{Stringify(mutateObject(item))}</div>
13 + );
14 }
15 return <div>{results}</div>;
16 }
17
18 +export const FIXTURE_ENTRYPOINT = {
19 + fn: Component,
20 + params: [],
21 + isComponent: true,
22 +};
23 +
24 ```
25
26 ## Code
27
28 ```javascript
29 import { unstable_useMemoCache as useMemoCache } from "react";
20 -function Component(props) {
30 +import { makeObject_Primitives, mutateObject, Stringify } from "shared-runtime";
31 +
32 +function Component(_props) {
33 const $ = useMemoCache(2);
34 let results;
35 if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
24 - const collection = [makeObject()];
36 + const collection = [makeObject_Primitives()];
37 results = [];
38 for (const item of collection) {
27 - results.push(<div>{mutate(item)}</div>);
39 + results.push(
40 + <div key={Stringify(item)}>{Stringify(mutateObject(item))}</div>
41 + );
42 }
43 $[0] = results;
44 } else {
@@ -40,5 +54,11 @@ function Component(props) {
54 return t0;
55 }
56
57 +export const FIXTURE_ENTRYPOINT = {
58 + fn: Component,
59 + params: [],
60 + isComponent: true,
61 +};
62 +
63 ```
64
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/for-of-mutate.tsx new
+18
@@ -0,0 +1,18 @@
1 +import { makeObject_Primitives, mutate, Stringify } from "shared-runtime";
2 +
3 +function Component(_props) {
4 + const collection = [makeObject_Primitives()];
5 + const results = [];
6 + for (const item of collection) {
7 + results.push(
8 + <div key={Stringify(item)}>{Stringify(mutate(item))}</div>
9 + );
10 + }
11 + return <div>{results}</div>;
12 +}
13 +
14 +export const FIXTURE_ENTRYPOINT = {
15 + fn: Component,
16 + params: [],
17 + isComponent: true,
18 +};
compiler/packages/sprout/src/SproutTodoFilter.ts
-1
@@ -266,7 +266,6 @@ const skipFilter = new Set([
266 "do-while-conditional-break",
267 "do-while-early-unconditional-break",
268 "fbt-params-complex-param-value",
269 - "for-of-mutate",
269 "function-expression-captures-value-later-frozen-jsx",
270 "function-expression-maybe-mutates-hook-return-value",
271 "function-expression-with-store-to-parameter",
compiler/packages/sprout/src/shared-runtime.ts
+3 -1
@@ -21,6 +21,8 @@ import React from "react";
21 * ```
22 */
23
24 +export type StringKeyedObject = { [key: string]: unknown };
25 +
26 export const CONST_STRING0 = "global string 0";
27 export const CONST_STRING1 = "global string 1";
28 export const CONST_STRING2 = "global string 2";
@@ -59,7 +61,7 @@ export function shallowCopy(obj: Object): object {
61 return Object.assign({}, obj);
62 }
63
62 -export function makeObject_Primitives(): object {
64 +export function makeObject_Primitives(): StringKeyedObject {
65 return { a: 0, b: "value1", c: true };
66 }
67