Skip to content

Commit 90b511e

Browse files
authored
fix(react-compiler): implement NumericLiteral as ObjectPropertyKey (#31791)
1 parent 0237295 commit 90b511e

File tree

6 files changed

+98
-0
lines changed

6 files changed

+98
-0
lines changed

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

+5
Original file line numberDiff line numberDiff line change
@@ -1455,6 +1455,11 @@ function lowerObjectPropertyKey(
14551455
kind: 'identifier',
14561456
name: key.node.name,
14571457
};
1458+
} else if (key.isNumericLiteral()) {
1459+
return {
1460+
kind: 'identifier',
1461+
name: String(key.node.value),
1462+
};
14581463
}
14591464

14601465
builder.errors.push({

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

+4
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,10 @@ export type ObjectPropertyKey =
703703
| {
704704
kind: 'computed';
705705
name: Place;
706+
}
707+
| {
708+
kind: 'number';
709+
name: number;
706710
};
707711

708712
export type ObjectProperty = {

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

+3
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,9 @@ function printObjectPropertyKey(key: ObjectPropertyKey): string {
330330
case 'computed': {
331331
return `[${printPlace(key.name)}]`;
332332
}
333+
case 'number': {
334+
return String(key.name);
335+
}
333336
}
334337
}
335338

compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/CodegenReactiveFunction.ts

+3
Original file line numberDiff line numberDiff line change
@@ -2429,6 +2429,9 @@ function codegenObjectPropertyKey(
24292429
});
24302430
return expr;
24312431
}
2432+
case 'number': {
2433+
return t.numericLiteral(key.name);
2434+
}
24322435
}
24332436
}
24342437

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
2+
## Input
3+
4+
```javascript
5+
function Test() {
6+
const obj = {
7+
21: 'dimaMachina',
8+
};
9+
// Destructuring assignment
10+
const {21: myVar} = obj;
11+
return (
12+
<div>
13+
{obj[21]}
14+
{myVar}
15+
</div>
16+
);
17+
}
18+
19+
export const FIXTURE_ENTRYPOINT = {
20+
fn: Test,
21+
params: [{}],
22+
};
23+
24+
```
25+
26+
## Code
27+
28+
```javascript
29+
import { c as _c } from "react/compiler-runtime";
30+
function Test() {
31+
const $ = _c(2);
32+
let t0;
33+
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
34+
t0 = { 21: "dimaMachina" };
35+
$[0] = t0;
36+
} else {
37+
t0 = $[0];
38+
}
39+
const obj = t0;
40+
41+
const { 21: myVar } = obj;
42+
let t1;
43+
if ($[1] === Symbol.for("react.memo_cache_sentinel")) {
44+
t1 = (
45+
<div>
46+
{obj[21]}
47+
{myVar}
48+
</div>
49+
);
50+
$[1] = t1;
51+
} else {
52+
t1 = $[1];
53+
}
54+
return t1;
55+
}
56+
57+
export const FIXTURE_ENTRYPOINT = {
58+
fn: Test,
59+
params: [{}],
60+
};
61+
62+
```
63+
64+
### Eval output
65+
(kind: ok) <div>dimaMachinadimaMachina</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function Test() {
2+
const obj = {
3+
21: 'dimaMachina',
4+
};
5+
// Destructuring assignment
6+
const {21: myVar} = obj;
7+
return (
8+
<div>
9+
{obj[21]}
10+
{myVar}
11+
</div>
12+
);
13+
}
14+
15+
export const FIXTURE_ENTRYPOINT = {
16+
fn: Test,
17+
params: [{}],
18+
};

0 commit comments

Comments
 (0)