Skip to content

Commit 51d4844

Browse files
committed
[compiler] Enable optional dependencies by default
Per title. This gives us much more granular memoization when the source used optional member expressions. Note that we only infer optional deps when the source used optionals: we don't (yet) infer optional dependencies from conditionals. [ghstack-poisoned]
1 parent b21df90 commit 51d4844

File tree

7 files changed

+24
-17
lines changed

7 files changed

+24
-17
lines changed

compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ const EnvironmentConfigSchema = z.object({
230230
* just `props`. With this flag enabled, we'll infer that full path as
231231
* the dependency.
232232
*/
233-
enableOptionalDependencies: z.boolean().default(false),
233+
enableOptionalDependencies: z.boolean().default(true),
234234

235235
/*
236236
* Enable validation of hooks to partially check that the component honors the rules of hooks.

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/optional-call-with-optional-property-load.expect.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ import { c as _c } from "react/compiler-runtime";
1515
function Component(props) {
1616
const $ = _c(2);
1717
let t0;
18-
if ($[0] !== props) {
18+
if ($[0] !== props?.items) {
1919
t0 = props?.items?.map?.(render)?.filter(Boolean) ?? [];
20-
$[0] = props;
20+
$[0] = props?.items;
2121
$[1] = t0;
2222
} else {
2323
t0 = $[1];

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/preserve-memo-validation/error.useMemo-infer-less-specific-conditional-access.expect.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ function Component({propA, propB}) {
4444
| ^^^^^^^^^^^^^^^^^
4545
> 14 | }, [propA?.a, propB.x.y]);
4646
| ^^^^ CannotPreserveMemoization: React Compiler has skipped optimizing this component because the existing manual memoization could not be preserved. The inferred dependencies did not match the manually specified dependencies, which could cause the value to change more or less frequently than expected (6:14)
47-
48-
CannotPreserveMemoization: React Compiler has skipped optimizing this component because the existing manual memoization could not be preserved. The inferred dependencies did not match the manually specified dependencies, which could cause the value to change more or less frequently than expected (6:14)
4947
15 | }
5048
16 |
5149
```

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reduce-reactive-deps/conditional-member-expr.expect.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ import { c as _c } from "react/compiler-runtime"; // To preserve the nullthrows
2929
function Component(props) {
3030
const $ = _c(2);
3131
let x;
32-
if ($[0] !== props.a) {
32+
if ($[0] !== props.a?.b) {
3333
x = [];
3434
x.push(props.a?.b);
35-
$[0] = props.a;
35+
$[0] = props.a?.b;
3636
$[1] = x;
3737
} else {
3838
x = $[1];

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reduce-reactive-deps/memberexpr-join-optional-chain.expect.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ import { c as _c } from "react/compiler-runtime"; // To preserve the nullthrows
4444
function Component(props) {
4545
const $ = _c(2);
4646
let x;
47-
if ($[0] !== props.a) {
47+
if ($[0] !== props.a.b) {
4848
x = [];
4949
x.push(props.a?.b);
5050
x.push(props.a.b.c);
51-
$[0] = props.a;
51+
$[0] = props.a.b;
5252
$[1] = x;
5353
} else {
5454
x = $[1];

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/reduce-reactive-deps/memberexpr-join-optional-chain2.expect.md

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,25 @@ export const FIXTURE_ENTRYPOINT = {
2121
```javascript
2222
import { c as _c } from "react/compiler-runtime";
2323
function Component(props) {
24-
const $ = _c(2);
24+
const $ = _c(5);
2525
let x;
26-
if ($[0] !== props.items) {
26+
if ($[0] !== props.items?.length || $[1] !== props.items?.edges) {
2727
x = [];
2828
x.push(props.items?.length);
29-
x.push(props.items?.edges?.map?.(render)?.filter?.(Boolean) ?? []);
30-
$[0] = props.items;
31-
$[1] = x;
29+
let t0;
30+
if ($[3] !== props.items?.edges) {
31+
t0 = props.items?.edges?.map?.(render)?.filter?.(Boolean) ?? [];
32+
$[3] = props.items?.edges;
33+
$[4] = t0;
34+
} else {
35+
t0 = $[4];
36+
}
37+
x.push(t0);
38+
$[0] = props.items?.length;
39+
$[1] = props.items?.edges;
40+
$[2] = x;
3241
} else {
33-
x = $[1];
42+
x = $[2];
3443
}
3544
return x;
3645
}

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/repro-scope-missing-mutable-range.expect.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ function HomeDiscoStoreItemTileRating(props) {
2323
const $ = _c(4);
2424
const item = useFragment();
2525
let count;
26-
if ($[0] !== item) {
26+
if ($[0] !== item?.aggregates) {
2727
count = 0;
2828
const aggregates = item?.aggregates || [];
2929
aggregates.forEach((aggregate) => {
3030
count = count + (aggregate.count || 0);
3131
count;
3232
});
33-
$[0] = item;
33+
$[0] = item?.aggregates;
3434
$[1] = count;
3535
} else {
3636
count = $[1];

0 commit comments

Comments
 (0)