@samitouri / QOS-React-2 / commits / a0e90065c6

[patch][babel] check babel identifier before lowering to HoistedConst

Same babel identifier issue as #2510 but for HoistedConst Not sure how we should best test this -- one possibility is using constant prop. Currently, we have false positives for HoistedConst that prevent constant propagation. I don't want to over-rotate on babel apis tests in our fixtures (instead of semantically interesting ones) ```js // input function Component() { { x: 4 }; const x = 2; return x; } // output function Component() { const $ = useMemoCache(1); let x; if ($[0] === Symbol.for("react.memo_cache_sentinel")) { x = 2; $[0] = x; } else { x = $[0]; } return x; } ```

Mofei Zhang committed Jan 18, 2024 at 18:29 UTC a0e90065c60223ed14caa0a233e2bcf5a903a821
6 files changed +82 -61
compiler/packages/babel-plugin-react-forget/src/HIR/BuildHIR.ts
+3
@@ -372,6 +372,9 @@ function lowerStatement(
372 */
373 s.traverse({
374 Identifier(id: NodePath<t.Identifier>) {
375 + if (!id.isReferencedIdentifier()) {
376 + return;
377 + }
378 const binding = id.scope.getBinding(id.node.name);
379 if (binding != null && hoistableBindings.has(binding)) {
380 if (
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/bug-destructuring-same-property-identifier-names.expect.md deleted
-51
@@ -1,51 +0,0 @@
1 -
2 -## Input
3 -
4 -```javascript
5 -function Component(props) {
6 - const {
7 - x: { destructured },
8 - sameName: renamed,
9 - } = props;
10 - const sameName = foo(destructured);
11 -
12 - return [sameName, renamed];
13 -}
14 -
15 -```
16 -
17 -## Code
18 -
19 -```javascript
20 -import { unstable_useMemoCache as useMemoCache } from "react";
21 -function Component(props) {
22 - const $ = useMemoCache(6);
23 - let sameName;
24 - let renamed;
25 - if ($[0] !== props) {
26 - const { x: t20, sameName: t32 } = props;
27 - renamed = t32;
28 - sameName = foo(destructured);
29 - $[0] = props;
30 - $[1] = sameName;
31 - $[2] = renamed;
32 - } else {
33 - sameName = $[1];
34 - renamed = $[2];
35 - }
36 -
37 - const t0 = sameName;
38 - let t1;
39 - if ($[3] !== t0 || $[4] !== renamed) {
40 - t1 = [t0, renamed];
41 - $[3] = t0;
42 - $[4] = renamed;
43 - $[5] = t1;
44 - } else {
45 - t1 = $[5];
46 - }
47 - return t1;
48 -}
49 -
50 -```
51 -
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/bug-destructuring-same-property-identifier-names.js deleted
-9
@@ -1,9 +0,0 @@
1 -function Component(props) {
2 - const {
3 - x: { destructured },
4 - sameName: renamed,
5 - } = props;
6 - const sameName = foo(destructured);
7 -
8 - return [sameName, renamed];
9 -}
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/destructuring-same-property-identifier-names.expect.md new
+63
@@ -0,0 +1,63 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +import { identity } from "shared-runtime";
6 +
7 +function Component(props) {
8 + const {
9 + x: { destructured },
10 + sameName: renamed,
11 + } = props;
12 + const sameName = identity(destructured);
13 +
14 + return [sameName, renamed];
15 +}
16 +
17 +export const FIXTURE_ENTRYPOINT = {
18 + fn: Component,
19 + params: [{ x: { destructured: 0 }, sameName: 2 }],
20 +};
21 +
22 +```
23 +
24 +## Code
25 +
26 +```javascript
27 +import { unstable_useMemoCache as useMemoCache } from "react";
28 +import { identity } from "shared-runtime";
29 +
30 +function Component(props) {
31 + const $ = useMemoCache(5);
32 + const { x: t18, sameName: renamed } = props;
33 + const { destructured } = t18;
34 + let t0;
35 + if ($[0] !== destructured) {
36 + t0 = identity(destructured);
37 + $[0] = destructured;
38 + $[1] = t0;
39 + } else {
40 + t0 = $[1];
41 + }
42 + const sameName = t0;
43 + let t1;
44 + if ($[2] !== sameName || $[3] !== renamed) {
45 + t1 = [sameName, renamed];
46 + $[2] = sameName;
47 + $[3] = renamed;
48 + $[4] = t1;
49 + } else {
50 + t1 = $[4];
51 + }
52 + return t1;
53 +}
54 +
55 +export const FIXTURE_ENTRYPOINT = {
56 + fn: Component,
57 + params: [{ x: { destructured: 0 }, sameName: 2 }],
58 +};
59 +
60 +```
61 +
62 +### Eval output
63 +(kind: ok) [0,2]
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/destructuring-same-property-identifier-names.js new
+16
@@ -0,0 +1,16 @@
1 +import { identity } from "shared-runtime";
2 +
3 +function Component(props) {
4 + const {
5 + x: { destructured },
6 + sameName: renamed,
7 + } = props;
8 + const sameName = identity(destructured);
9 +
10 + return [sameName, renamed];
11 +}
12 +
13 +export const FIXTURE_ENTRYPOINT = {
14 + fn: Component,
15 + params: [{ x: { destructured: 0 }, sameName: 2 }],
16 +};
compiler/packages/sprout/src/SproutTodoFilter.ts
-1
@@ -516,7 +516,6 @@ const skipFilter = new Set([
516 "bug-jsx-memberexpr-tag-in-lambda",
517 "bug-invalid-code-when-bailout",
518 "component-syntax-ref-gating.flow",
519 - "bug-destructuring-same-property-identifier-names",
519
520 // 'react-forget-runtime' not yet supported
521 "flag-enable-emit-hook-guards",