diff --git a/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Options.ts b/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Options.ts index 86fcea9d39..af67ff8b41 100644 --- a/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Options.ts +++ b/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Options.ts @@ -173,7 +173,7 @@ export type Logger = { export const defaultOptions: PluginOptions = { compilationMode: "infer", - panicThreshold: "CRITICAL_ERRORS", + panicThreshold: "NONE", environment: {}, logger: null, gating: null, diff --git a/compiler/packages/babel-plugin-react-forget/src/HIR/Environment.ts b/compiler/packages/babel-plugin-react-forget/src/HIR/Environment.ts index 676e9ecd0b..dfa8aee28b 100644 --- a/compiler/packages/babel-plugin-react-forget/src/HIR/Environment.ts +++ b/compiler/packages/babel-plugin-react-forget/src/HIR/Environment.ts @@ -156,7 +156,7 @@ const EnvironmentConfigSchema = z.object({ * that the memoization was preserved. This can be useful for determining where referential equalities * may change under Forget. */ - validatePreserveExistingMemoizationGuarantees: z.boolean().default(false), + validatePreserveExistingMemoizationGuarantees: z.boolean().default(true), // 🌲 enableForest: z.boolean().default(false), @@ -192,7 +192,7 @@ const EnvironmentConfigSchema = z.object({ * Validates that setState is not unconditionally called during render, as it can lead to * infinite loops. */ - validateNoSetStateInRender: z.boolean().default(false), + validateNoSetStateInRender: z.boolean().default(true), /** * Validates that the dependencies of all effect hooks are memoized. This helps ensure @@ -214,7 +214,7 @@ const EnvironmentConfigSchema = z.object({ * in this allowlist. You can enable this validation with no additional allowlisted calls by setting * this option to the empty array. */ - validateNoCapitalizedCalls: z.nullable(z.array(z.string())).default(null), + validateNoCapitalizedCalls: z.nullable(z.array(z.string())).default([]), /* * When enabled, the compiler assumes that hooks follow the Rules of React: @@ -223,7 +223,7 @@ const EnvironmentConfigSchema = z.object({ * - Hooks may memoize the result they return, thus the return value is * assumed frozen. */ - enableAssumeHooksFollowRulesOfReact: z.boolean().default(false), + enableAssumeHooksFollowRulesOfReact: z.boolean().default(true), /** * When enabled, the compiler assumes that any values are not subsequently @@ -231,7 +231,7 @@ const EnvironmentConfigSchema = z.object({ * if a value `x` is referenced inside a function expression passed to `useEffect`, * then this flag will assume that `x` is not subusequently modified. */ - enableTransitivelyFreezeFunctionExpressions: z.boolean().default(false), + enableTransitivelyFreezeFunctionExpressions: z.boolean().default(true), /* * When enabled, removes *all* memoization from the function: this includes diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/conditional-early-return.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/conditional-early-return.expect.md index b39f2c8472..940da08261 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/conditional-early-return.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/conditional-early-return.expect.md @@ -76,12 +76,12 @@ function ComponentA(props) { let t0; if ($[0] !== props) { t0 = Symbol.for("react.early_return_sentinel"); - bb7: { + bb8: { a_DEBUG = []; a_DEBUG.push(props.a); if (props.b) { t0 = null; - break bb7; + break bb8; } a_DEBUG.push(props.d); @@ -130,13 +130,13 @@ function ComponentC(props) { let t0; if ($[0] !== props) { t0 = Symbol.for("react.early_return_sentinel"); - bb7: { + bb8: { a = []; a.push(props.a); if (props.b) { a.push(props.c); t0 = null; - break bb7; + break bb8; } a.push(props.d); @@ -163,13 +163,13 @@ function ComponentD(props) { let t0; if ($[0] !== props) { t0 = Symbol.for("react.early_return_sentinel"); - bb7: { + bb8: { a = []; a.push(props.a); if (props.b) { a.push(props.c); t0 = a; - break bb7; + break bb8; } a.push(props.d); diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/early-return-nested-early-return-within-reactive-scope.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/early-return-nested-early-return-within-reactive-scope.expect.md index 4a55cb243e..7f7c6d0e1f 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/early-return-nested-early-return-within-reactive-scope.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/early-return-nested-early-return-within-reactive-scope.expect.md @@ -35,7 +35,7 @@ function Component(props) { let t0; if ($[0] !== props) { t0 = Symbol.for("react.early_return_sentinel"); - bb11: { + bb12: { const x = []; if (props.cond) { x.push(props.a); @@ -51,11 +51,11 @@ function Component(props) { const y = t1; x.push(y); t0 = x; - break bb11; + break bb12; } t0 = x; - break bb11; + break bb12; } else { let t1; if ($[4] === Symbol.for("react.memo_cache_sentinel")) { @@ -65,7 +65,7 @@ function Component(props) { t1 = $[4]; } t0 = t1; - break bb11; + break bb12; } } $[0] = props; diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/early-return-no-declarations-reassignments-dependencies.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/early-return-no-declarations-reassignments-dependencies.expect.md index 50e4da268a..ba75dfee38 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/early-return-no-declarations-reassignments-dependencies.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/early-return-no-declarations-reassignments-dependencies.expect.md @@ -73,12 +73,12 @@ function Component(props) { let t0; if ($[0] === Symbol.for("react.memo_cache_sentinel")) { t0 = Symbol.for("react.early_return_sentinel"); - bb8: { + bb9: { const x = []; if (ENABLE_FEATURE) { x.push(42); t0 = x; - break bb8; + break bb9; } else { console.log("fallthrough"); } diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/early-return-within-reactive-scope.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/early-return-within-reactive-scope.expect.md index 30cdc84b95..e138ee0a80 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/early-return-within-reactive-scope.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/early-return-within-reactive-scope.expect.md @@ -49,12 +49,12 @@ function Component(props) { let t0; if ($[0] !== props) { t0 = Symbol.for("react.early_return_sentinel"); - bb8: { + bb9: { const x = []; if (props.cond) { x.push(props.a); t0 = x; - break bb8; + break bb9; } else { let t1; if ($[2] !== props.b) { @@ -65,7 +65,7 @@ function Component(props) { t1 = $[3]; } t0 = t1; - break bb8; + break bb9; } } $[0] = props; 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 2f1ac586e1..aca378272e 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,19 +2,13 @@ ## Input ```javascript -import { - makeObject_Primitives, - mutateAndReturn, - Stringify, -} from "shared-runtime"; +import { makeObject_Primitives, mutateAndReturn, toJSON } from "shared-runtime"; function Component(_props) { const collection = [makeObject_Primitives()]; const results = []; for (const item of collection) { - results.push( -
{Stringify(mutateAndReturn(item))}
- ); + results.push(
{toJSON(mutateAndReturn(item))}
); } return
{results}
; } @@ -31,11 +25,7 @@ export const FIXTURE_ENTRYPOINT = { ```javascript import { unstable_useMemoCache as useMemoCache } from "react"; -import { - makeObject_Primitives, - mutateAndReturn, - Stringify, -} from "shared-runtime"; +import { makeObject_Primitives, mutateAndReturn, toJSON } from "shared-runtime"; function Component(_props) { const $ = useMemoCache(1); @@ -45,7 +35,7 @@ function Component(_props) { const results = []; for (const item of collection) { results.push( -
{Stringify(mutateAndReturn(item))}
+
{toJSON(mutateAndReturn(item))}
); } @@ -66,4 +56,4 @@ export const FIXTURE_ENTRYPOINT = { ``` ### Eval output -(kind: ok)
{"a":0,"b":"value1","c":true,"wat0":"joe"}
\ No newline at end of file +(kind: ok)
{"a":0,"b":"value1","c":true,"wat0":"joe"}
\ 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 index 6c5ad7dc66..b4466dbe6a 100644 --- 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 @@ -1,16 +1,10 @@ -import { - makeObject_Primitives, - mutateAndReturn, - Stringify, -} from "shared-runtime"; +import { makeObject_Primitives, mutateAndReturn, toJSON } from "shared-runtime"; function Component(_props) { const collection = [makeObject_Primitives()]; const results = []; for (const item of collection) { - results.push( -
{Stringify(mutateAndReturn(item))}
- ); + results.push(
{toJSON(mutateAndReturn(item))}
); } return
{results}
; } diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/hooks-with-prefix.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/hooks-with-prefix.expect.md index b0e29c337d..f3cecb4506 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/hooks-with-prefix.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/hooks-with-prefix.expect.md @@ -46,39 +46,47 @@ const React$useMemo = React.useMemo; const Internal$Reassigned$useHook = useHook; function Component() { - const $ = useMemoCache(6); + const $ = useMemoCache(8); const [state] = React$useState(0); const object = Internal$Reassigned$useHook(); - const json = JSON.stringify(object); let t0; - let t1; - if ($[0] !== state) { - t0 = makeArray(state); - const doubledArray = t0; - - t1 = doubledArray.join(""); - $[0] = state; - $[1] = t1; - $[2] = t0; + if ($[0] !== object) { + t0 = JSON.stringify(object); + $[0] = object; + $[1] = t0; } else { - t1 = $[1]; - t0 = $[2]; + t0 = $[1]; } + const json = t0; + let t1; let t2; - if ($[3] !== t1 || $[4] !== json) { - t2 = ( + if ($[2] !== state) { + t1 = makeArray(state); + const doubledArray = t1; + + t2 = doubledArray.join(""); + $[2] = state; + $[3] = t2; + $[4] = t1; + } else { + t2 = $[3]; + t1 = $[4]; + } + let t3; + if ($[5] !== t2 || $[6] !== json) { + t3 = (
- {t1} + {t2} {json}
); - $[3] = t1; - $[4] = json; $[5] = t2; + $[6] = json; + $[7] = t3; } else { - t2 = $[5]; + t3 = $[7]; } - return t2; + return t3; } export const FIXTURE_ENTRYPOINT = { diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/optional-member-expression.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/optional-member-expression.expect.md index 779bc76b45..a96195e634 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/optional-member-expression.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/optional-member-expression.expect.md @@ -15,8 +15,18 @@ function Foo(props) { ## Code ```javascript +import { unstable_useMemoCache as useMemoCache } from "react"; function Foo(props) { - const x = bar(props.a); + const $ = useMemoCache(2); + let t0; + if ($[0] !== props.a) { + t0 = bar(props.a); + $[0] = props.a; + $[1] = t0; + } else { + t0 = $[1]; + } + const x = t0; const y = x?.b; const z = useBar(y); diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/partial-early-return-within-reactive-scope.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/partial-early-return-within-reactive-scope.expect.md index b9b7a31440..ee267cf72a 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/partial-early-return-within-reactive-scope.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/partial-early-return-within-reactive-scope.expect.md @@ -35,12 +35,12 @@ function Component(props) { let t0; if ($[0] !== props) { t0 = Symbol.for("react.early_return_sentinel"); - bb11: { + bb12: { const x = []; if (props.cond) { x.push(props.a); t0 = x; - break bb11; + break bb12; } else { let t1; if ($[3] === Symbol.for("react.memo_cache_sentinel")) { @@ -52,7 +52,7 @@ function Component(props) { y = t1; if (props.b) { t0 = undefined; - break bb11; + break bb12; } } } diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reduce-reactive-cond-deps-return-in-scope.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reduce-reactive-cond-deps-return-in-scope.expect.md index a15917c97c..1c60214c33 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reduce-reactive-cond-deps-return-in-scope.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reduce-reactive-cond-deps-return-in-scope.expect.md @@ -35,11 +35,11 @@ function useFoo(t0) { let t1; if ($[0] !== objIsNull || $[1] !== obj) { t1 = Symbol.for("react.early_return_sentinel"); - bb8: { + bb9: { x = []; if (objIsNull) { t1 = undefined; - break bb8; + break bb9; } else { x.push(obj.a); } diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reduce-reactive-deps/jump-poisoned/reduce-if-nonexhaustive-poisoned-deps.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reduce-reactive-deps/jump-poisoned/reduce-if-nonexhaustive-poisoned-deps.expect.md index 2d1c186cf8..e91d83d061 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reduce-reactive-deps/jump-poisoned/reduce-if-nonexhaustive-poisoned-deps.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reduce-reactive-deps/jump-poisoned/reduce-if-nonexhaustive-poisoned-deps.expect.md @@ -46,12 +46,12 @@ function useFoo(t0) { let t1; if ($[0] !== cond || $[1] !== hasAB || $[2] !== input) { t1 = Symbol.for("react.early_return_sentinel"); - bb10: { + bb11: { x = []; if (cond) { if (!hasAB) { t1 = null; - break bb10; + break bb11; } let t2; if ($[5] !== input.a.b) { diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reduce-reactive-deps/jump-poisoned/reduce-if-nonexhaustive-poisoned-deps1.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reduce-reactive-deps/jump-poisoned/reduce-if-nonexhaustive-poisoned-deps1.expect.md index 4dfb2abd6e..3a3efe7066 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reduce-reactive-deps/jump-poisoned/reduce-if-nonexhaustive-poisoned-deps1.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reduce-reactive-deps/jump-poisoned/reduce-if-nonexhaustive-poisoned-deps1.expect.md @@ -47,12 +47,12 @@ function useFoo(t0) { let t1; if ($[0] !== cond || $[1] !== hasAB || $[2] !== input) { t1 = Symbol.for("react.early_return_sentinel"); - bb11: { + bb12: { x = []; if (cond) { if (!hasAB) { t1 = null; - break bb11; + break bb12; } else { let t2; if ($[5] !== input.a.b) { diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reduce-reactive-deps/jump-poisoned/return-in-scope.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reduce-reactive-deps/jump-poisoned/return-in-scope.expect.md index a49308233a..34d6e79080 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reduce-reactive-deps/jump-poisoned/return-in-scope.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reduce-reactive-deps/jump-poisoned/return-in-scope.expect.md @@ -37,11 +37,11 @@ function useFoo(t0) { let t1; if ($[0] !== objIsNull || $[1] !== obj) { t1 = Symbol.for("react.early_return_sentinel"); - bb7: { + bb8: { x = []; if (objIsNull) { t1 = undefined; - break bb7; + break bb8; } x.push(obj.b); diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reduce-reactive-deps/jump-poisoned/return-poisons-outer-scope.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reduce-reactive-deps/jump-poisoned/return-poisons-outer-scope.expect.md index 1a3e7471cc..ddfca55bbb 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reduce-reactive-deps/jump-poisoned/return-poisons-outer-scope.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reduce-reactive-deps/jump-poisoned/return-poisons-outer-scope.expect.md @@ -43,11 +43,11 @@ function useFoo(t0) { let t1; if ($[0] !== cond || $[1] !== input) { t1 = Symbol.for("react.early_return_sentinel"); - bb7: { + bb8: { x = []; if (cond) { t1 = null; - break bb7; + break bb8; } let t2; if ($[4] !== input.a.b) { diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reduce-reactive-deps/jump-unpoisoned/reduce-if-exhaustive-nonpoisoned-deps.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reduce-reactive-deps/jump-unpoisoned/reduce-if-exhaustive-nonpoisoned-deps.expect.md index b8feeb03cc..00b516600b 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reduce-reactive-deps/jump-unpoisoned/reduce-if-exhaustive-nonpoisoned-deps.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reduce-reactive-deps/jump-unpoisoned/reduce-if-exhaustive-nonpoisoned-deps.expect.md @@ -37,7 +37,7 @@ function useFoo(t0) { let t1; if ($[0] !== hasAB || $[1] !== input.a || $[2] !== returnNull) { t1 = Symbol.for("react.early_return_sentinel"); - bb10: { + bb11: { x = []; if (!hasAB) { let t2; @@ -51,7 +51,7 @@ function useFoo(t0) { x.push(t2); if (!returnNull) { t1 = null; - break bb10; + break bb11; } } else { let t2; diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reduce-reactive-deps/jump-unpoisoned/reduce-if-exhaustive-nonpoisoned-deps1.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reduce-reactive-deps/jump-unpoisoned/reduce-if-exhaustive-nonpoisoned-deps1.expect.md index 3ba36fc1ed..a003b16bd0 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reduce-reactive-deps/jump-unpoisoned/reduce-if-exhaustive-nonpoisoned-deps1.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reduce-reactive-deps/jump-unpoisoned/reduce-if-exhaustive-nonpoisoned-deps1.expect.md @@ -47,7 +47,7 @@ function useFoo(t0) { let t1; if ($[0] !== cond1 || $[1] !== cond2 || $[2] !== input.a.b) { t1 = Symbol.for("react.early_return_sentinel"); - bb11: { + bb12: { x = []; if (cond1) { if (!cond2) { @@ -61,7 +61,7 @@ function useFoo(t0) { } x.push(t2); t1 = null; - break bb11; + break bb12; } else { let t2; if ($[7] !== input.a.b) { diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reduce-reactive-deps/reduce-if-exhaustive-poisoned-deps.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reduce-reactive-deps/reduce-if-exhaustive-poisoned-deps.expect.md index df9e6017c9..1a628635a7 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reduce-reactive-deps/reduce-if-exhaustive-poisoned-deps.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/reduce-reactive-deps/reduce-if-exhaustive-poisoned-deps.expect.md @@ -38,7 +38,7 @@ function useFoo(t0) { let t1; if ($[0] !== inputHasABC || $[1] !== input.a || $[2] !== inputHasAB) { t1 = Symbol.for("react.early_return_sentinel"); - bb10: { + bb11: { x = []; if (!inputHasABC) { let t2; @@ -52,7 +52,7 @@ function useFoo(t0) { x.push(t2); if (!inputHasAB) { t1 = null; - break bb10; + break bb11; } let t3; if ($[7] !== input.a.b) { diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/repro-no-declarations-in-reactive-scope-with-early-return.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/repro-no-declarations-in-reactive-scope-with-early-return.expect.md index 93e93611f9..fd4c44c029 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/repro-no-declarations-in-reactive-scope-with-early-return.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/repro-no-declarations-in-reactive-scope-with-early-return.expect.md @@ -46,7 +46,7 @@ function Component() { let t2; if ($[0] !== items) { t2 = Symbol.for("react.early_return_sentinel"); - bb14: { + bb15: { let t3; if ($[4] === Symbol.for("react.memo_cache_sentinel")) { t3 = (t4) => { @@ -72,7 +72,7 @@ function Component() { t4 = $[5]; } t2 = t4; - break bb14; + break bb15; } let t4; if ($[6] === Symbol.for("react.memo_cache_sentinel")) { diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/repro-no-value-for-temporary-reactive-scope-with-early-return.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/repro-no-value-for-temporary-reactive-scope-with-early-return.expect.md index a7f7846af1..40728be577 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/repro-no-value-for-temporary-reactive-scope-with-early-return.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/repro-no-value-for-temporary-reactive-scope-with-early-return.expect.md @@ -43,12 +43,12 @@ function Component(props) { let t1; if ($[0] === Symbol.for("react.memo_cache_sentinel")) { t1 = Symbol.for("react.early_return_sentinel"); - bb7: { + bb8: { const object = makeObject_Primitives(); const cond = makeObject_Primitives(); if (!cond) { t1 = null; - break bb7; + break bb8; } t0 = ( diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/repro-renaming-conflicting-decls.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/repro-renaming-conflicting-decls.expect.md index 43b3a00969..05b1809f88 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/repro-renaming-conflicting-decls.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/repro-renaming-conflicting-decls.expect.md @@ -51,12 +51,12 @@ function Component(props) { let t2; if ($[0] !== props) { t2 = Symbol.for("react.early_return_sentinel"); - bb10: { + bb11: { t0 = toJSON(props); const propsString = t0; if (propsString.length <= 2) { t2 = null; - break bb10; + break bb11; } t1 = identity(propsString); diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/try-catch-try-value-modified-in-catch.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/try-catch-try-value-modified-in-catch.expect.md index d23a752b59..a5d87524bd 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/try-catch-try-value-modified-in-catch.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/try-catch-try-value-modified-in-catch.expect.md @@ -34,7 +34,7 @@ function Component(props) { let t0; if ($[0] !== props.y || $[1] !== props.e) { t0 = Symbol.for("react.early_return_sentinel"); - bb18: { + bb19: { try { const y = []; y.push(props.y); @@ -43,11 +43,11 @@ function Component(props) { const e = t1; e.push(props.e); t0 = e; - break bb18; + break bb19; } t0 = null; - break bb18; + break bb19; } $[0] = props.y; $[1] = props.e; diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/try-catch-with-catch-param.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/try-catch-with-catch-param.expect.md index 1a66a7a521..19d95b7b8b 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/try-catch-with-catch-param.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/try-catch-with-catch-param.expect.md @@ -35,7 +35,7 @@ function Component(props) { let t0; if ($[0] === Symbol.for("react.memo_cache_sentinel")) { t0 = Symbol.for("react.early_return_sentinel"); - bb11: { + bb12: { const x = []; try { throwInput(x); @@ -43,11 +43,11 @@ function Component(props) { const e = t1; e.push(null); t0 = e; - break bb11; + break bb12; } t0 = x; - break bb11; + break bb12; } $[0] = t0; } else { diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/try-catch-with-return.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/try-catch-with-return.expect.md index ec26e252b7..ab4d2aea2c 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/try-catch-with-return.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/try-catch-with-return.expect.md @@ -37,19 +37,19 @@ function Component(props) { let t0; if ($[0] === Symbol.for("react.memo_cache_sentinel")) { t0 = Symbol.for("react.early_return_sentinel"); - bb25: { + bb26: { x = []; try { const y = shallowCopy({}); if (y == null) { t0 = undefined; - break bb25; + break bb26; } x.push(throwInput(y)); } catch { t0 = null; - break bb25; + break bb26; } } $[0] = x; diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/useCallback-call-second-function-which-captures-maybe-mutable-value-dont-preserve-memoization.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/useCallback-call-second-function-which-captures-maybe-mutable-value-dont-preserve-memoization.expect.md index f3bbcc346b..279cb7fb69 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/useCallback-call-second-function-which-captures-maybe-mutable-value-dont-preserve-memoization.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/useCallback-call-second-function-which-captures-maybe-mutable-value-dont-preserve-memoization.expect.md @@ -2,7 +2,7 @@ ## Input ```javascript -// @enablePreserveExistingMemoizationGuarantees:false +// @enablePreserveExistingMemoizationGuarantees:false @enableTransitivelyFreezeFunctionExpressions:false import { useCallback } from "react"; import { identity, @@ -39,7 +39,7 @@ export const FIXTURE_ENTRYPOINT = { ## Code ```javascript -// @enablePreserveExistingMemoizationGuarantees:false +// @enablePreserveExistingMemoizationGuarantees:false @enableTransitivelyFreezeFunctionExpressions:false import { useCallback, unstable_useMemoCache as useMemoCache } from "react"; import { identity, diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/useCallback-call-second-function-which-captures-maybe-mutable-value-dont-preserve-memoization.js b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/useCallback-call-second-function-which-captures-maybe-mutable-value-dont-preserve-memoization.js index 5f6716fb19..86d4af6677 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/useCallback-call-second-function-which-captures-maybe-mutable-value-dont-preserve-memoization.js +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/useCallback-call-second-function-which-captures-maybe-mutable-value-dont-preserve-memoization.js @@ -1,4 +1,4 @@ -// @enablePreserveExistingMemoizationGuarantees:false +// @enablePreserveExistingMemoizationGuarantees:false @enableTransitivelyFreezeFunctionExpressions:false import { useCallback } from "react"; import { identity, diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/useEffect-nested-lambdas.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/useEffect-nested-lambdas.expect.md index bff4dcb103..e170424ff3 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/useEffect-nested-lambdas.expect.md +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/useEffect-nested-lambdas.expect.md @@ -2,6 +2,8 @@ ## Input ```javascript +// @enableTransitivelyFreezeFunctionExpressions:false + function Component(props) { const item = useMutable(props.itemId); const dispatch = useDispatch(); @@ -30,9 +32,10 @@ function Component(props) { ## Code ```javascript -import { unstable_useMemoCache as useMemoCache } from "react"; +import { unstable_useMemoCache as useMemoCache } from "react"; // @enableTransitivelyFreezeFunctionExpressions:false + function Component(props) { - const $ = useMemoCache(3); + const $ = useMemoCache(9); const item = useMutable(props.itemId); const dispatch = useDispatch(); useFreeze(dispatch); @@ -47,25 +50,42 @@ function Component(props) { t0 = $[1]; } const exit = t0; - - useEffect(() => { - const cleanup = GlobalEventEmitter.addListener("onInput", () => { - if (item.value) { - exit(); - } - }); - return () => cleanup.remove(); - }, [exit, item]); + let t1; + if ($[2] !== item.value || $[3] !== exit) { + t1 = () => { + const cleanup = GlobalEventEmitter.addListener("onInput", () => { + if (item.value) { + exit(); + } + }); + return () => cleanup.remove(); + }; + $[2] = item.value; + $[3] = exit; + $[4] = t1; + } else { + t1 = $[4]; + } + let t2; + if ($[5] !== exit || $[6] !== item) { + t2 = [exit, item]; + $[5] = exit; + $[6] = item; + $[7] = t2; + } else { + t2 = $[7]; + } + useEffect(t1, t2); maybeMutate(item); - let t1; - if ($[2] === Symbol.for("react.memo_cache_sentinel")) { - t1 =
; - $[2] = t1; + let t3; + if ($[8] === Symbol.for("react.memo_cache_sentinel")) { + t3 =
; + $[8] = t3; } else { - t1 = $[2]; + t3 = $[8]; } - return t1; + return t3; } ``` diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/useEffect-nested-lambdas.js b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/useEffect-nested-lambdas.js index 4a1439a287..2da7e390e1 100644 --- a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/useEffect-nested-lambdas.js +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/useEffect-nested-lambdas.js @@ -1,3 +1,5 @@ +// @enableTransitivelyFreezeFunctionExpressions:false + function Component(props) { const item = useMutable(props.itemId); const dispatch = useDispatch(); diff --git a/compiler/packages/snap/src/compiler.ts b/compiler/packages/snap/src/compiler.ts index 6665c16c2b..3d881ba5fb 100644 --- a/compiler/packages/snap/src/compiler.ts +++ b/compiler/packages/snap/src/compiler.ts @@ -41,6 +41,8 @@ function makePluginOptions( let enableUseMemoCachePolyfill = false; let panicThreshold: PanicThresholdOptions = "ALL_ERRORS"; let hookPattern: string | null = null; + // TODO(@mofeiZ) rewrite snap fixtures to @validatePreserveExistingMemo:false + let validatePreserveExistingMemoizationGuarantees = false; if (firstLine.indexOf("@compilationMode(annotation)") !== -1) { assert( @@ -113,6 +115,10 @@ function makePluginOptions( ignoreUseNoForget = true; } + if (firstLine.includes("@validatePreserveExistingMemoizationGuarantees")) { + validatePreserveExistingMemoizationGuarantees = true; + } + const hookPatternMatch = /@hookPattern:"([^"]+)"/.exec(firstLine); if ( hookPatternMatch && @@ -164,6 +170,7 @@ function makePluginOptions( enableEmitHookGuards, assertValidMutableRanges: true, hookPattern, + validatePreserveExistingMemoizationGuarantees, }, compilationMode, logger: null, @@ -173,7 +180,7 @@ function makePluginOptions( enableUseMemoCachePolyfill, eslintSuppressionRules, flowSuppressions, - ignoreUseNoForget + ignoreUseNoForget, }; } @@ -211,18 +218,18 @@ function getEvaluatorPresets( presets.push( language === "typescript" ? [ - "@babel/preset-typescript", - { - /** - * onlyRemoveTypeImports needs to be set as fbt imports - * would otherwise be removed by this pass. - * https://github.com/facebook/fbt/issues/49 - * https://github.com/facebook/sfbt/issues/72 - * https://dev.to/retyui/how-to-add-support-typescript-for-fbt-an-internationalization-framework-3lo0 - */ - onlyRemoveTypeImports: true, - }, - ] + "@babel/preset-typescript", + { + /** + * onlyRemoveTypeImports needs to be set as fbt imports + * would otherwise be removed by this pass. + * https://github.com/facebook/fbt/issues/49 + * https://github.com/facebook/sfbt/issues/72 + * https://dev.to/retyui/how-to-add-support-typescript-for-fbt-an-internationalization-framework-3lo0 + */ + onlyRemoveTypeImports: true, + }, + ] : "@babel/preset-flow" );