[sprout] Convert for-of-mutate to sprout

This commit is contained in:
Lauren Tan
2023-09-05 12:35:18 -04:00
parent 375e2d8f33
commit 6a71868d4c
5 changed files with 47 additions and 8 deletions
@@ -2,29 +2,43 @@
## Input
```javascript
function Component(props) {
const collection = [makeObject()];
import { makeObject_Primitives, mutateObject, Stringify } from "shared-runtime";
function Component(_props) {
const collection = [makeObject_Primitives()];
const results = [];
for (const item of collection) {
results.push(<div>{mutate(item)}</div>);
results.push(
<div key={Stringify(item)}>{Stringify(mutateObject(item))}</div>
);
}
return <div>{results}</div>;
}
export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [],
isComponent: true,
};
```
## Code
```javascript
import { unstable_useMemoCache as useMemoCache } from "react";
function Component(props) {
import { makeObject_Primitives, mutateObject, Stringify } from "shared-runtime";
function Component(_props) {
const $ = useMemoCache(2);
let results;
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
const collection = [makeObject()];
const collection = [makeObject_Primitives()];
results = [];
for (const item of collection) {
results.push(<div>{mutate(item)}</div>);
results.push(
<div key={Stringify(item)}>{Stringify(mutateObject(item))}</div>
);
}
$[0] = results;
} else {
@@ -40,5 +54,11 @@ function Component(props) {
return t0;
}
export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [],
isComponent: true,
};
```
@@ -0,0 +1,18 @@
import { makeObject_Primitives, mutate, Stringify } from "shared-runtime";
function Component(_props) {
const collection = [makeObject_Primitives()];
const results = [];
for (const item of collection) {
results.push(
<div key={Stringify(item)}>{Stringify(mutate(item))}</div>
);
}
return <div>{results}</div>;
}
export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [],
isComponent: true,
};
@@ -266,7 +266,6 @@ const skipFilter = new Set([
"do-while-conditional-break",
"do-while-early-unconditional-break",
"fbt-params-complex-param-value",
"for-of-mutate",
"function-expression-captures-value-later-frozen-jsx",
"function-expression-maybe-mutates-hook-return-value",
"function-expression-with-store-to-parameter",
@@ -21,6 +21,8 @@ import React from "react";
* ```
*/
export type StringKeyedObject = { [key: string]: unknown };
export const CONST_STRING0 = "global string 0";
export const CONST_STRING1 = "global string 1";
export const CONST_STRING2 = "global string 2";
@@ -59,7 +61,7 @@ export function shallowCopy(obj: Object): object {
return Object.assign({}, obj);
}
export function makeObject_Primitives(): object {
export function makeObject_Primitives(): StringKeyedObject {
return { a: 0, b: "value1", c: true };
}