From 6a71868d4c159c8089f0c70f17bfcf2abc68173d Mon Sep 17 00:00:00 2001 From: Lauren Tan Date: Tue, 5 Sep 2023 12:35:18 -0400 Subject: [PATCH] [sprout] Convert for-of-mutate to sprout --- .../tests/fixtures/for-of-mutate.tsx} | 0 .../fixtures/compiler/for-of-mutate.expect.md | 32 +++++++++++++++---- .../fixtures/compiler/for-of-mutate.tsx | 18 +++++++++++ .../packages/sprout/src/SproutTodoFilter.ts | 1 - .../packages/sprout/src/shared-runtime.ts | 4 ++- 5 files changed, 47 insertions(+), 8 deletions(-) rename compiler/{packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/for-of-mutate.js => crates/forget_hermes_parser/tests/fixtures/for-of-mutate.tsx} (100%) create mode 100644 compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/for-of-mutate.tsx diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/for-of-mutate.js b/compiler/crates/forget_hermes_parser/tests/fixtures/for-of-mutate.tsx similarity index 100% rename from compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/for-of-mutate.js rename to compiler/crates/forget_hermes_parser/tests/fixtures/for-of-mutate.tsx diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/for-of-mutate.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/for-of-mutate.expect.md index 412f474e84..c8289545ae 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/for-of-mutate.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/for-of-mutate.expect.md @@ -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(
{mutate(item)}
); + results.push( +
{Stringify(mutateObject(item))}
+ ); } return
{results}
; } +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(
{mutate(item)}
); + results.push( +
{Stringify(mutateObject(item))}
+ ); } $[0] = results; } else { @@ -40,5 +54,11 @@ function Component(props) { return t0; } +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [], + isComponent: true, +}; + ``` \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/for-of-mutate.tsx b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/for-of-mutate.tsx new file mode 100644 index 0000000000..d2bb3826c2 --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/for-of-mutate.tsx @@ -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( +
{Stringify(mutate(item))}
+ ); + } + return
{results}
; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [], + isComponent: true, +}; diff --git a/compiler/packages/sprout/src/SproutTodoFilter.ts b/compiler/packages/sprout/src/SproutTodoFilter.ts index ca6b11afbf..1f4d4fb2d4 100644 --- a/compiler/packages/sprout/src/SproutTodoFilter.ts +++ b/compiler/packages/sprout/src/SproutTodoFilter.ts @@ -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", diff --git a/compiler/packages/sprout/src/shared-runtime.ts b/compiler/packages/sprout/src/shared-runtime.ts index 00dccb8714..c62335a454 100644 --- a/compiler/packages/sprout/src/shared-runtime.ts +++ b/compiler/packages/sprout/src/shared-runtime.ts @@ -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 }; }