diff --git a/compiler/packages/babel-plugin-react-compiler/src/HIR/CollectHoistablePropertyLoads.ts b/compiler/packages/babel-plugin-react-compiler/src/HIR/CollectHoistablePropertyLoads.ts index 1e0ab77eff65b..212b050d2719b 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/HIR/CollectHoistablePropertyLoads.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/HIR/CollectHoistablePropertyLoads.ts @@ -212,12 +212,12 @@ class PropertyPathRegistry { } function addNonNullPropertyPath( - node: PropertyPathNode, + source: Identifier, + sourceNode: PropertyPathNode, instrId: InstructionId, knownImmutableIdentifiers: Set, result: Set, ): void { - const object = node.fullPath.identifier; /** * Since this runs *after* buildReactiveScopeTerminals, identifier mutable ranges * are not valid with respect to current instruction id numbering. @@ -229,14 +229,14 @@ function addNonNullPropertyPath( * See comment at top of function for why we track known immutable identifiers. */ const isMutableAtInstr = - object.mutableRange.end > object.mutableRange.start + 1 && - object.scope != null && - inRange({id: instrId}, object.scope.range); + source.mutableRange.end > source.mutableRange.start + 1 && + source.scope != null && + inRange({id: instrId}, source.scope.range); if ( !isMutableAtInstr || - knownImmutableIdentifiers.has(node.fullPath.identifier.id) + knownImmutableIdentifiers.has(sourceNode.fullPath.identifier.id) ) { - result.add(node); + result.add(sourceNode); } } @@ -299,9 +299,9 @@ function collectNonNullsInBlocks( identifier: instr.value.object.identifier, path: [], }; - const propertyNode = registry.getOrCreateProperty(source); addNonNullPropertyPath( - propertyNode, + instr.value.object.identifier, + registry.getOrCreateProperty(source), instr.id, knownImmutableIdentifiers, assumedNonNullObjects, @@ -314,6 +314,7 @@ function collectNonNullsInBlocks( const sourceNode = temporaries.get(source); if (sourceNode != null) { addNonNullPropertyPath( + instr.value.value.identifier, registry.getOrCreateProperty(sourceNode), instr.id, knownImmutableIdentifiers, @@ -328,6 +329,7 @@ function collectNonNullsInBlocks( const sourceNode = temporaries.get(source); if (sourceNode != null) { addNonNullPropertyPath( + instr.value.object.identifier, registry.getOrCreateProperty(sourceNode), instr.id, knownImmutableIdentifiers, diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/bug-try-catch-maybe-null-dependency.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/bug-try-catch-maybe-null-dependency.expect.md deleted file mode 100644 index ed2c4d7d3d293..0000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/bug-try-catch-maybe-null-dependency.expect.md +++ /dev/null @@ -1,57 +0,0 @@ - -## Input - -```javascript -import {identity} from 'shared-runtime'; - -function useFoo(maybeNullObject: {value: {inner: number}} | null) { - const y = []; - try { - y.push(identity(maybeNullObject.value.inner)); - } catch { - y.push('null'); - } - - return y; -} - -export const FIXTURE_ENTRYPOINT = { - fn: useFoo, - params: [null], - sequentialRenders: [null, {value: 2}, {value: 3}, null], -}; - -``` - -## Code - -```javascript -import { c as _c } from "react/compiler-runtime"; -import { identity } from "shared-runtime"; - -function useFoo(maybeNullObject) { - const $ = _c(2); - let y; - if ($[0] !== maybeNullObject.value.inner) { - y = []; - try { - y.push(identity(maybeNullObject.value.inner)); - } catch { - y.push("null"); - } - $[0] = maybeNullObject.value.inner; - $[1] = y; - } else { - y = $[1]; - } - return y; -} - -export const FIXTURE_ENTRYPOINT = { - fn: useFoo, - params: [null], - sequentialRenders: [null, { value: 2 }, { value: 3 }, null], -}; - -``` - \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/bug-try-catch-maybe-null-dependency.ts b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/bug-try-catch-maybe-null-dependency.ts deleted file mode 100644 index de7e2626f57b4..0000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/bug-try-catch-maybe-null-dependency.ts +++ /dev/null @@ -1,18 +0,0 @@ -import {identity} from 'shared-runtime'; - -function useFoo(maybeNullObject: {value: {inner: number}} | null) { - const y = []; - try { - y.push(identity(maybeNullObject.value.inner)); - } catch { - y.push('null'); - } - - return y; -} - -export const FIXTURE_ENTRYPOINT = { - fn: useFoo, - params: [null], - sequentialRenders: [null, {value: 2}, {value: 3}, null], -}; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/reduce-reactive-deps/infer-function-cond-access-not-hoisted.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/reduce-reactive-deps/infer-function-cond-access-not-hoisted.expect.md index ec3ecdebc5d2c..e37b8365a2faf 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/reduce-reactive-deps/infer-function-cond-access-not-hoisted.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/reduce-reactive-deps/infer-function-cond-access-not-hoisted.expect.md @@ -4,13 +4,18 @@ ```javascript // @enablePropagateDepsInHIR -import { Stringify } from "shared-runtime"; +import {Stringify} from 'shared-runtime'; function Foo({a, shouldReadA}) { - return { - if (shouldReadA) return a.b.c; - return null; - }} shouldInvokeFns={true} /> + return ( + { + if (shouldReadA) return a.b.c; + return null; + }} + shouldInvokeFns={true} + /> + ); } export const FIXTURE_ENTRYPOINT = { diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/reduce-reactive-deps/infer-function-cond-access-not-hoisted.tsx b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/reduce-reactive-deps/infer-function-cond-access-not-hoisted.tsx index 66600ac177464..5c71d57750c38 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/reduce-reactive-deps/infer-function-cond-access-not-hoisted.tsx +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/reduce-reactive-deps/infer-function-cond-access-not-hoisted.tsx @@ -1,12 +1,17 @@ // @enablePropagateDepsInHIR -import { Stringify } from "shared-runtime"; +import {Stringify} from 'shared-runtime'; function Foo({a, shouldReadA}) { - return { - if (shouldReadA) return a.b.c; - return null; - }} shouldInvokeFns={true} /> + return ( + { + if (shouldReadA) return a.b.c; + return null; + }} + shouldInvokeFns={true} + /> + ); } export const FIXTURE_ENTRYPOINT = { diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/reduce-reactive-deps/infer-function-uncond-access-hoisted.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/reduce-reactive-deps/infer-function-uncond-access-hoisted.expect.md index 4d6bd9d0bc917..03a4bf6382d53 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/reduce-reactive-deps/infer-function-uncond-access-hoisted.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/reduce-reactive-deps/infer-function-uncond-access-hoisted.expect.md @@ -4,7 +4,7 @@ ```javascript // @enablePropagateDepsInHIR -import { Stringify } from "shared-runtime"; +import {Stringify} from 'shared-runtime'; function useFoo(a) { return a.b.c} shouldInvokeFns={true} />; @@ -28,9 +28,9 @@ import { Stringify } from "shared-runtime"; function useFoo(a) { const $ = _c(2); let t0; - if ($[0] !== a.b.c) { + if ($[0] !== a) { t0 = a.b.c} shouldInvokeFns={true} />; - $[0] = a.b.c; + $[0] = a; $[1] = t0; } else { t0 = $[1]; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/reduce-reactive-deps/infer-function-uncond-access-hoisted.tsx b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/reduce-reactive-deps/infer-function-uncond-access-hoisted.tsx index 3b2a4362ad759..a02139c409bfe 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/reduce-reactive-deps/infer-function-uncond-access-hoisted.tsx +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/reduce-reactive-deps/infer-function-uncond-access-hoisted.tsx @@ -1,6 +1,6 @@ // @enablePropagateDepsInHIR -import { Stringify } from "shared-runtime"; +import {Stringify} from 'shared-runtime'; function useFoo(a) { return a.b.c} shouldInvokeFns={true} />; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/reduce-reactive-deps/infer-function-uncond-access-hoists-other-dep.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/reduce-reactive-deps/infer-function-uncond-access-hoists-other-dep.expect.md index 8a454c19353b8..f5575d8f5d39e 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/reduce-reactive-deps/infer-function-uncond-access-hoists-other-dep.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/reduce-reactive-deps/infer-function-uncond-access-hoists-other-dep.expect.md @@ -4,7 +4,7 @@ ```javascript // @enablePropagateDepsInHIR -import { identity, makeArray, Stringify, useIdentity } from "shared-runtime"; +import {identity, makeArray, Stringify, useIdentity} from 'shared-runtime'; function Foo({a, cond}) { // Assume fn will be uncond evaluated, so we can safely evaluate {a., @@ -21,7 +21,11 @@ function Foo({a, cond}) { export const FIXTURE_ENTRYPOINT = { fn: Foo, params: [{a: null, cond: true}], - sequentialRenders: [{a: null, cond: true}, {a: {b: {c: 4}}, cond: true}, {a: {b: {c: 4}}, cond: true}], + sequentialRenders: [ + {a: null, cond: true}, + {a: {b: {c: 4}}, cond: true}, + {a: {b: {c: 4}}, cond: true}, + ], }; ``` @@ -47,13 +51,13 @@ function Foo(t0) { const fn = t1; useIdentity(null); let x; - if ($[2] !== cond || $[3] !== a.b.c) { + if ($[2] !== cond || $[3] !== a) { x = makeArray(); if (cond) { x.push(identity(a.b.c)); } $[2] = cond; - $[3] = a.b.c; + $[3] = a; $[4] = x; } else { x = $[4]; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/reduce-reactive-deps/infer-function-uncond-access-hoists-other-dep.tsx b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/reduce-reactive-deps/infer-function-uncond-access-hoists-other-dep.tsx index cdd369199c28c..a9956ed8a5ffe 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/reduce-reactive-deps/infer-function-uncond-access-hoists-other-dep.tsx +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/reduce-reactive-deps/infer-function-uncond-access-hoists-other-dep.tsx @@ -1,6 +1,6 @@ // @enablePropagateDepsInHIR -import { identity, makeArray, Stringify, useIdentity } from "shared-runtime"; +import {identity, makeArray, Stringify, useIdentity} from 'shared-runtime'; function Foo({a, cond}) { // Assume fn will be uncond evaluated, so we can safely evaluate {a., @@ -17,5 +17,9 @@ function Foo({a, cond}) { export const FIXTURE_ENTRYPOINT = { fn: Foo, params: [{a: null, cond: true}], - sequentialRenders: [{a: null, cond: true}, {a: {b: {c: 4}}, cond: true}, {a: {b: {c: 4}}, cond: true}], + sequentialRenders: [ + {a: null, cond: true}, + {a: {b: {c: 4}}, cond: true}, + {a: {b: {c: 4}}, cond: true}, + ], }; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/reduce-reactive-deps/infer-function-uncond-optional-hoists-other-dep.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/reduce-reactive-deps/infer-function-uncond-optional-hoists-other-dep.expect.md index 9f54497592b0c..591e04de7ba15 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/reduce-reactive-deps/infer-function-uncond-optional-hoists-other-dep.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/reduce-reactive-deps/infer-function-uncond-optional-hoists-other-dep.expect.md @@ -4,7 +4,7 @@ ```javascript // @enablePropagateDepsInHIR -import { identity, makeArray, Stringify, useIdentity } from "shared-runtime"; +import {identity, makeArray, Stringify, useIdentity} from 'shared-runtime'; function Foo({a, cond}) { // Assume fn can be uncond evaluated, so we can safely evaluate a.b?.c. @@ -20,7 +20,11 @@ function Foo({a, cond}) { export const FIXTURE_ENTRYPOINT = { fn: Foo, params: [{a: null, cond: true}], - sequentialRenders: [{a: null, cond: true}, {a: {b: {c: {d: 5}}}, cond: true}, {a: {b: null}, cond: false}], + sequentialRenders: [ + {a: null, cond: true}, + {a: {b: {c: {d: 5}}}, cond: true}, + {a: {b: null}, cond: false}, + ], }; ``` diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/reduce-reactive-deps/infer-function-uncond-optional-hoists-other-dep.tsx b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/reduce-reactive-deps/infer-function-uncond-optional-hoists-other-dep.tsx index 646b29fa768f2..3b538de991dc0 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/reduce-reactive-deps/infer-function-uncond-optional-hoists-other-dep.tsx +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/reduce-reactive-deps/infer-function-uncond-optional-hoists-other-dep.tsx @@ -1,6 +1,6 @@ // @enablePropagateDepsInHIR -import { identity, makeArray, Stringify, useIdentity } from "shared-runtime"; +import {identity, makeArray, Stringify, useIdentity} from 'shared-runtime'; function Foo({a, cond}) { // Assume fn can be uncond evaluated, so we can safely evaluate a.b?.c. @@ -16,5 +16,9 @@ function Foo({a, cond}) { export const FIXTURE_ENTRYPOINT = { fn: Foo, params: [{a: null, cond: true}], - sequentialRenders: [{a: null, cond: true}, {a: {b: {c: {d: 5}}}, cond: true}, {a: {b: null}, cond: false}], + sequentialRenders: [ + {a: null, cond: true}, + {a: {b: {c: {d: 5}}}, cond: true}, + {a: {b: null}, cond: false}, + ], }; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/reduce-reactive-deps/todo-infer-function-uncond-optionals-hoisted.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/reduce-reactive-deps/todo-infer-function-uncond-optionals-hoisted.expect.md index a24039a85b22a..958f97d7331d5 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/reduce-reactive-deps/todo-infer-function-uncond-optionals-hoisted.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/reduce-reactive-deps/todo-infer-function-uncond-optionals-hoisted.expect.md @@ -4,7 +4,7 @@ ```javascript // @enablePropagateDepsInHIR -import { Stringify } from "shared-runtime"; +import {Stringify} from 'shared-runtime'; function useFoo(a) { return a.b?.c.d?.e} shouldInvokeFns={true} />; @@ -13,7 +13,13 @@ function useFoo(a) { export const FIXTURE_ENTRYPOINT = { fn: useFoo, params: [{a: null}], - sequentialRenders: [{a: null}, {a: {b: null}}, {a: {b: {c: {d: null}}}}, , {a: {b: {c: {d: {e: 4}}}}}], + sequentialRenders: [ + {a: null}, + {a: {b: null}}, + {a: {b: {c: {d: null}}}}, + , + {a: {b: {c: {d: {e: 4}}}}}, + ], }; ``` @@ -45,6 +51,7 @@ export const FIXTURE_ENTRYPOINT = { { a: null }, { a: { b: null } }, { a: { b: { c: { d: null } } } }, + , { a: { b: { c: { d: { e: 4 } } } } }, ], diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/reduce-reactive-deps/todo-infer-function-uncond-optionals-hoisted.tsx b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/reduce-reactive-deps/todo-infer-function-uncond-optionals-hoisted.tsx index 64458d6f11219..79be2ba6bd740 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/reduce-reactive-deps/todo-infer-function-uncond-optionals-hoisted.tsx +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/reduce-reactive-deps/todo-infer-function-uncond-optionals-hoisted.tsx @@ -1,6 +1,6 @@ // @enablePropagateDepsInHIR -import { Stringify } from "shared-runtime"; +import {Stringify} from 'shared-runtime'; function useFoo(a) { return a.b?.c.d?.e} shouldInvokeFns={true} />; @@ -9,5 +9,11 @@ function useFoo(a) { export const FIXTURE_ENTRYPOINT = { fn: useFoo, params: [{a: null}], - sequentialRenders: [{a: null}, {a: {b: null}}, {a: {b: {c: {d: null}}}}, , {a: {b: {c: {d: {e: 4}}}}}], + sequentialRenders: [ + {a: null}, + {a: {b: null}}, + {a: {b: {c: {d: null}}}}, + , + {a: {b: {c: {d: {e: 4}}}}}, + ], }; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/try-catch-maybe-null-dependency.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/try-catch-maybe-null-dependency.expect.md deleted file mode 100644 index 8674de709c3b0..0000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/try-catch-maybe-null-dependency.expect.md +++ /dev/null @@ -1,71 +0,0 @@ - -## Input - -```javascript -// @enablePropagateDepsInHIR -import {identity} from 'shared-runtime'; - -function useFoo(maybeNullObject: {value: {inner: number}} | null) { - const y = []; - try { - y.push(identity(maybeNullObject.value.inner)); - } catch { - y.push('null'); - } - - return y; -} - -export const FIXTURE_ENTRYPOINT = { - fn: useFoo, - params: [null], - sequentialRenders: [null, {value: 2}, {value: 3}, null], -}; - -``` - -## Code - -```javascript -import { c as _c } from "react/compiler-runtime"; // @enablePropagateDepsInHIR -import { identity } from "shared-runtime"; - -function useFoo(maybeNullObject) { - const $ = _c(4); - let y; - if ($[0] !== maybeNullObject) { - y = []; - try { - let t0; - if ($[2] !== maybeNullObject.value.inner) { - t0 = identity(maybeNullObject.value.inner); - $[2] = maybeNullObject.value.inner; - $[3] = t0; - } else { - t0 = $[3]; - } - y.push(t0); - } catch { - y.push("null"); - } - $[0] = maybeNullObject; - $[1] = y; - } else { - y = $[1]; - } - return y; -} - -export const FIXTURE_ENTRYPOINT = { - fn: useFoo, - params: [null], - sequentialRenders: [null, { value: 2 }, { value: 3 }, null], -}; - -``` - -### Eval output -(kind: ok) ["null"] -[null] -[null] -["null"] \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/try-catch-maybe-null-dependency.ts b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/try-catch-maybe-null-dependency.ts deleted file mode 100644 index 58bf5943d5b1b..0000000000000 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/try-catch-maybe-null-dependency.ts +++ /dev/null @@ -1,19 +0,0 @@ -// @enablePropagateDepsInHIR -import {identity} from 'shared-runtime'; - -function useFoo(maybeNullObject: {value: {inner: number}} | null) { - const y = []; - try { - y.push(identity(maybeNullObject.value.inner)); - } catch { - y.push('null'); - } - - return y; -} - -export const FIXTURE_ENTRYPOINT = { - fn: useFoo, - params: [null], - sequentialRenders: [null, {value: 2}, {value: 3}, null], -}; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/try-catch-try-value-modified-in-catch-escaping.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/try-catch-try-value-modified-in-catch-escaping.expect.md new file mode 100644 index 0000000000000..914001f3737bd --- /dev/null +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/try-catch-try-value-modified-in-catch-escaping.expect.md @@ -0,0 +1,63 @@ + +## Input + +```javascript +// @enablePropagateDepsInHIR +const {throwInput} = require('shared-runtime'); + +function Component(props) { + let x; + try { + const y = []; + y.push(props.y); + throwInput(y); + } catch (e) { + e.push(props.e); + x = e; + } + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{y: 'foo', e: 'bar'}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @enablePropagateDepsInHIR +const { throwInput } = require("shared-runtime"); + +function Component(props) { + const $ = _c(2); + let x; + if ($[0] !== props) { + try { + const y = []; + y.push(props.y); + throwInput(y); + } catch (t0) { + const e = t0; + e.push(props.e); + x = e; + } + $[0] = props; + $[1] = x; + } else { + x = $[1]; + } + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ y: "foo", e: "bar" }], +}; + +``` + +### Eval output +(kind: ok) ["foo","bar"] \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/try-catch-try-value-modified-in-catch-escaping.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/try-catch-try-value-modified-in-catch-escaping.js new file mode 100644 index 0000000000000..5a0864118ba8f --- /dev/null +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/try-catch-try-value-modified-in-catch-escaping.js @@ -0,0 +1,20 @@ +// @enablePropagateDepsInHIR +const {throwInput} = require('shared-runtime'); + +function Component(props) { + let x; + try { + const y = []; + y.push(props.y); + throwInput(y); + } catch (e) { + e.push(props.e); + x = e; + } + return x; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{y: 'foo', e: 'bar'}], +}; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/try-catch-try-value-modified-in-catch.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/try-catch-try-value-modified-in-catch.expect.md new file mode 100644 index 0000000000000..30ecdf6d59e9d --- /dev/null +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/try-catch-try-value-modified-in-catch.expect.md @@ -0,0 +1,69 @@ + +## Input + +```javascript +// @enablePropagateDepsInHIR +const {throwInput} = require('shared-runtime'); + +function Component(props) { + try { + const y = []; + y.push(props.y); + throwInput(y); + } catch (e) { + e.push(props.e); + return e; + } + return null; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{y: 'foo', e: 'bar'}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; // @enablePropagateDepsInHIR +const { throwInput } = require("shared-runtime"); + +function Component(props) { + const $ = _c(2); + let t0; + if ($[0] !== props) { + t0 = Symbol.for("react.early_return_sentinel"); + bb0: { + try { + const y = []; + y.push(props.y); + throwInput(y); + } catch (t1) { + const e = t1; + e.push(props.e); + t0 = e; + break bb0; + } + } + $[0] = props; + $[1] = t0; + } else { + t0 = $[1]; + } + if (t0 !== Symbol.for("react.early_return_sentinel")) { + return t0; + } + return null; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ y: "foo", e: "bar" }], +}; + +``` + +### Eval output +(kind: ok) ["foo","bar"] \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/try-catch-try-value-modified-in-catch.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/try-catch-try-value-modified-in-catch.js new file mode 100644 index 0000000000000..97d650453c175 --- /dev/null +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/propagate-scope-deps-hir-fork/try-catch-try-value-modified-in-catch.js @@ -0,0 +1,19 @@ +// @enablePropagateDepsInHIR +const {throwInput} = require('shared-runtime'); + +function Component(props) { + try { + const y = []; + y.push(props.y); + throwInput(y); + } catch (e) { + e.push(props.e); + return e; + } + return null; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{y: 'foo', e: 'bar'}], +}; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reduce-reactive-deps/bug-infer-function-cond-access-not-hoisted.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reduce-reactive-deps/bug-infer-function-cond-access-not-hoisted.expect.md index 16145a2e8e56f..4d45d3f3c6da6 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reduce-reactive-deps/bug-infer-function-cond-access-not-hoisted.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reduce-reactive-deps/bug-infer-function-cond-access-not-hoisted.expect.md @@ -2,13 +2,18 @@ ## Input ```javascript -import { Stringify } from "shared-runtime"; +import {Stringify} from 'shared-runtime'; function Foo({a, shouldReadA}) { - return { - if (shouldReadA) return a.b.c; - return null; - }} shouldInvokeFns={true} /> + return ( + { + if (shouldReadA) return a.b.c; + return null; + }} + shouldInvokeFns={true} + /> + ); } export const FIXTURE_ENTRYPOINT = { diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reduce-reactive-deps/bug-infer-function-cond-access-not-hoisted.tsx b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reduce-reactive-deps/bug-infer-function-cond-access-not-hoisted.tsx index 95f42e79250c5..e571ee7b953ec 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reduce-reactive-deps/bug-infer-function-cond-access-not-hoisted.tsx +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reduce-reactive-deps/bug-infer-function-cond-access-not-hoisted.tsx @@ -1,10 +1,15 @@ -import { Stringify } from "shared-runtime"; +import {Stringify} from 'shared-runtime'; function Foo({a, shouldReadA}) { - return { - if (shouldReadA) return a.b.c; - return null; - }} shouldInvokeFns={true} /> + return ( + { + if (shouldReadA) return a.b.c; + return null; + }} + shouldInvokeFns={true} + /> + ); } export const FIXTURE_ENTRYPOINT = { diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reduce-reactive-deps/infer-function-uncond-access-hoisted.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reduce-reactive-deps/infer-function-uncond-access-hoisted.expect.md index 79c6b8930212a..ec9b504e8d899 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reduce-reactive-deps/infer-function-uncond-access-hoisted.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reduce-reactive-deps/infer-function-uncond-access-hoisted.expect.md @@ -2,7 +2,7 @@ ## Input ```javascript -import { Stringify } from "shared-runtime"; +import {Stringify} from 'shared-runtime'; function useFoo(a) { return a.b.c} shouldInvokeFns={true} />; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reduce-reactive-deps/infer-function-uncond-access-hoisted.tsx b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reduce-reactive-deps/infer-function-uncond-access-hoisted.tsx index 669412a544789..4895a7002a135 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reduce-reactive-deps/infer-function-uncond-access-hoisted.tsx +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reduce-reactive-deps/infer-function-uncond-access-hoisted.tsx @@ -1,4 +1,4 @@ -import { Stringify } from "shared-runtime"; +import {Stringify} from 'shared-runtime'; function useFoo(a) { return a.b.c} shouldInvokeFns={true} />; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reduce-reactive-deps/infer-function-uncond-access-hoists-other-dep.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reduce-reactive-deps/infer-function-uncond-access-hoists-other-dep.expect.md index d1c74077112a1..dd5f2c145ed2f 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reduce-reactive-deps/infer-function-uncond-access-hoists-other-dep.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reduce-reactive-deps/infer-function-uncond-access-hoists-other-dep.expect.md @@ -2,7 +2,7 @@ ## Input ```javascript -import { identity, makeArray, Stringify, useIdentity } from "shared-runtime"; +import {identity, makeArray, Stringify, useIdentity} from 'shared-runtime'; function Foo({a, cond}) { // Assume fn will be uncond evaluated, so we can safely evaluate {a., @@ -19,7 +19,11 @@ function Foo({a, cond}) { export const FIXTURE_ENTRYPOINT = { fn: Foo, params: [{a: null, cond: true}], - sequentialRenders: [{a: null, cond: true}, {a: {b: {c: 4}}, cond: true}, {a: {b: {c: 4}}, cond: true}], + sequentialRenders: [ + {a: null, cond: true}, + {a: {b: {c: 4}}, cond: true}, + {a: {b: {c: 4}}, cond: true}, + ], }; ``` diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reduce-reactive-deps/infer-function-uncond-access-hoists-other-dep.tsx b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reduce-reactive-deps/infer-function-uncond-access-hoists-other-dep.tsx index 9ab0372594dea..fdc4babc6d00f 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reduce-reactive-deps/infer-function-uncond-access-hoists-other-dep.tsx +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reduce-reactive-deps/infer-function-uncond-access-hoists-other-dep.tsx @@ -1,4 +1,4 @@ -import { identity, makeArray, Stringify, useIdentity } from "shared-runtime"; +import {identity, makeArray, Stringify, useIdentity} from 'shared-runtime'; function Foo({a, cond}) { // Assume fn will be uncond evaluated, so we can safely evaluate {a., @@ -15,5 +15,9 @@ function Foo({a, cond}) { export const FIXTURE_ENTRYPOINT = { fn: Foo, params: [{a: null, cond: true}], - sequentialRenders: [{a: null, cond: true}, {a: {b: {c: 4}}, cond: true}, {a: {b: {c: 4}}, cond: true}], + sequentialRenders: [ + {a: null, cond: true}, + {a: {b: {c: 4}}, cond: true}, + {a: {b: {c: 4}}, cond: true}, + ], }; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reduce-reactive-deps/infer-function-uncond-optional-hoists-other-dep.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reduce-reactive-deps/infer-function-uncond-optional-hoists-other-dep.expect.md index aa810d0fbf093..bc4d0fc3df360 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reduce-reactive-deps/infer-function-uncond-optional-hoists-other-dep.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reduce-reactive-deps/infer-function-uncond-optional-hoists-other-dep.expect.md @@ -2,7 +2,7 @@ ## Input ```javascript -import { identity, makeArray, Stringify, useIdentity } from "shared-runtime"; +import {identity, makeArray, Stringify, useIdentity} from 'shared-runtime'; function Foo({a, cond}) { // Assume fn can be uncond evaluated, so we can safely evaluate a.b?.c. @@ -18,7 +18,11 @@ function Foo({a, cond}) { export const FIXTURE_ENTRYPOINT = { fn: Foo, params: [{a: null, cond: true}], - sequentialRenders: [{a: null, cond: true}, {a: {b: {c: {d: 5}}}, cond: true}, {a: {b: null}, cond: false}], + sequentialRenders: [ + {a: null, cond: true}, + {a: {b: {c: {d: 5}}}, cond: true}, + {a: {b: null}, cond: false}, + ], }; ``` diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reduce-reactive-deps/infer-function-uncond-optional-hoists-other-dep.tsx b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reduce-reactive-deps/infer-function-uncond-optional-hoists-other-dep.tsx index 9179f1eead790..7facd985394bc 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reduce-reactive-deps/infer-function-uncond-optional-hoists-other-dep.tsx +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reduce-reactive-deps/infer-function-uncond-optional-hoists-other-dep.tsx @@ -1,4 +1,4 @@ -import { identity, makeArray, Stringify, useIdentity } from "shared-runtime"; +import {identity, makeArray, Stringify, useIdentity} from 'shared-runtime'; function Foo({a, cond}) { // Assume fn can be uncond evaluated, so we can safely evaluate a.b?.c. @@ -14,5 +14,9 @@ function Foo({a, cond}) { export const FIXTURE_ENTRYPOINT = { fn: Foo, params: [{a: null, cond: true}], - sequentialRenders: [{a: null, cond: true}, {a: {b: {c: {d: 5}}}, cond: true}, {a: {b: null}, cond: false}], + sequentialRenders: [ + {a: null, cond: true}, + {a: {b: {c: {d: 5}}}, cond: true}, + {a: {b: null}, cond: false}, + ], }; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reduce-reactive-deps/todo-infer-function-uncond-optionals-hoisted.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reduce-reactive-deps/todo-infer-function-uncond-optionals-hoisted.expect.md index 2eef697e5faad..499abf992c146 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reduce-reactive-deps/todo-infer-function-uncond-optionals-hoisted.expect.md +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reduce-reactive-deps/todo-infer-function-uncond-optionals-hoisted.expect.md @@ -2,7 +2,7 @@ ## Input ```javascript -import { Stringify } from "shared-runtime"; +import {Stringify} from 'shared-runtime'; function useFoo(a) { return a.b?.c.d?.e} shouldInvokeFns={true} />; @@ -11,7 +11,13 @@ function useFoo(a) { export const FIXTURE_ENTRYPOINT = { fn: useFoo, params: [{a: null}], - sequentialRenders: [{a: null}, {a: {b: null}}, {a: {b: {c: {d: null}}}}, , {a: {b: {c: {d: {e: 4}}}}}], + sequentialRenders: [ + {a: null}, + {a: {b: null}}, + {a: {b: {c: {d: null}}}}, + , + {a: {b: {c: {d: {e: 4}}}}}, + ], }; ``` @@ -42,6 +48,7 @@ export const FIXTURE_ENTRYPOINT = { { a: null }, { a: { b: null } }, { a: { b: { c: { d: null } } } }, + , { a: { b: { c: { d: { e: 4 } } } } }, ], diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reduce-reactive-deps/todo-infer-function-uncond-optionals-hoisted.tsx b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reduce-reactive-deps/todo-infer-function-uncond-optionals-hoisted.tsx index 75dc4b35022e9..076be2089ac86 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reduce-reactive-deps/todo-infer-function-uncond-optionals-hoisted.tsx +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reduce-reactive-deps/todo-infer-function-uncond-optionals-hoisted.tsx @@ -1,4 +1,4 @@ -import { Stringify } from "shared-runtime"; +import {Stringify} from 'shared-runtime'; function useFoo(a) { return a.b?.c.d?.e} shouldInvokeFns={true} />; @@ -7,5 +7,11 @@ function useFoo(a) { export const FIXTURE_ENTRYPOINT = { fn: useFoo, params: [{a: null}], - sequentialRenders: [{a: null}, {a: {b: null}}, {a: {b: {c: {d: null}}}}, , {a: {b: {c: {d: {e: 4}}}}}], + sequentialRenders: [ + {a: null}, + {a: {b: null}}, + {a: {b: {c: {d: null}}}}, + , + {a: {b: {c: {d: {e: 4}}}}}, + ], }; diff --git a/compiler/packages/snap/src/SproutTodoFilter.ts b/compiler/packages/snap/src/SproutTodoFilter.ts index 5cef2d3d67a66..ace8b6f7aa235 100644 --- a/compiler/packages/snap/src/SproutTodoFilter.ts +++ b/compiler/packages/snap/src/SproutTodoFilter.ts @@ -479,7 +479,6 @@ const skipFilter = new Set([ 'fbt/bug-fbt-plural-multiple-function-calls', 'fbt/bug-fbt-plural-multiple-mixed-call-tag', 'bug-invalid-hoisting-functionexpr', - 'bug-try-catch-maybe-null-dependency', 'reduce-reactive-deps/bug-merge-uncond-optional-chain-and-cond', 'original-reactive-scopes-fork/bug-nonmutating-capture-in-unsplittable-memo-block', 'original-reactive-scopes-fork/bug-hoisted-declaration-with-scope',