Skip to content

fix(react-compiler): implement NumericLiteral as ObjectPropertyKey #31791

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1455,6 +1455,11 @@ function lowerObjectPropertyKey(
kind: 'identifier',
name: key.node.name,
};
} else if (key.isNumericLiteral()) {
return {
kind: 'identifier',
name: String(key.node.value),
};
Comment on lines +1458 to +1462
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also avoid converting numeric literals to babel identifiers. Could you

  1. Add a 'number' variant to HIR:ObjectPropertyKey
  2. Check for the number variant in codegenReactiveFunction:codegenObjectPropertyKey and return a t.numericLiteral

Let's also add a test case for destructuring assignment, which also uses ObjectExpressions

const {21: myVar} = obj;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mofeiZ thanks for your review, done in 5499ae1

Should I move destructuring assignment test to a separate file?

Copy link
Contributor

@mofeiZ mofeiZ Mar 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks for the quick fix! Just started a test run and will merge once everything passes

Should I move destructuring assignment test to a separate file?

This should be fine.

Copy link
Contributor Author

@dimaMachina dimaMachina Mar 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, updated snapshot in 1fb22ae

}

builder.errors.push({
Expand Down
4 changes: 4 additions & 0 deletions compiler/packages/babel-plugin-react-compiler/src/HIR/HIR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,10 @@ export type ObjectPropertyKey =
| {
kind: 'computed';
name: Place;
}
| {
kind: 'number';
name: number;
};

export type ObjectProperty = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,9 @@ function printObjectPropertyKey(key: ObjectPropertyKey): string {
case 'computed': {
return `[${printPlace(key.name)}]`;
}
case 'number': {
return String(key.name);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2429,6 +2429,9 @@ function codegenObjectPropertyKey(
});
return expr;
}
case 'number': {
return t.numericLiteral(key.name);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@

## Input

```javascript
function Test() {
const obj = {
21: 'dimaMachina',
};
// Destructuring assignment
const {21: myVar} = obj;
return (
<div>
{obj[21]}
{myVar}
</div>
);
}

export const FIXTURE_ENTRYPOINT = {
fn: Test,
params: [{}],
};

```

## Code

```javascript
import { c as _c } from "react/compiler-runtime";
function Test() {
const $ = _c(2);
let t0;
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
t0 = { 21: "dimaMachina" };
$[0] = t0;
} else {
t0 = $[0];
}
const obj = t0;

const { 21: myVar } = obj;
let t1;
if ($[1] === Symbol.for("react.memo_cache_sentinel")) {
t1 = (
<div>
{obj[21]}
{myVar}
</div>
);
$[1] = t1;
} else {
t1 = $[1];
}
return t1;
}

export const FIXTURE_ENTRYPOINT = {
fn: Test,
params: [{}],
};

```

### Eval output
(kind: ok) <div>dimaMachinadimaMachina</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function Test() {
const obj = {
21: 'dimaMachina',
};
// Destructuring assignment
const {21: myVar} = obj;
return (
<div>
{obj[21]}
{myVar}
</div>
);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a fixture here so that we can test evaluation with/without compilation:

export const FIXTURE_ENTRYPOINT = {
  fn: Test,
  params: [{}],
};

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also be sure to update fixtures after this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Updated! Could you also review my other contribution to the react compiler? #31792

I just pushed there similar change 🙂


export const FIXTURE_ENTRYPOINT = {
fn: Test,
params: [{}],
};
Loading