From af913a2425159411d2e094e2d0624c15d294108d Mon Sep 17 00:00:00 2001 From: lluisemper <58423269+lluisemper@users.noreply.github.com> Date: Sat, 29 Nov 2025 21:18:12 +0100 Subject: [PATCH] [compiler] Fix computed property keys in object method shorthand Fixes #35203 The compiler was incorrectly discarding computed property key notation when used with method shorthand syntax, transforming `[key]() {}` into `key() {}`. Changed the hardcoded `false` parameter to `property.key.kind === 'computed'` in CodegenReactiveFunction.ts to preserve computed keys, matching the pattern already used for regular object properties. --- .../ReactiveScopes/CodegenReactiveFunction.ts | 2 +- ...ct-method-shorthand-computed-key.expect.md | 55 +++++++++++++++++++ .../object-method-shorthand-computed-key.js | 15 +++++ .../object-method-shorthand-computed-key.tsx | 24 ++++++++ 4 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-method-shorthand-computed-key.expect.md create mode 100644 compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-method-shorthand-computed-key.js create mode 100644 compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-method-shorthand-computed-key.tsx diff --git a/compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/CodegenReactiveFunction.ts b/compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/CodegenReactiveFunction.ts index 0ab7934a1a6..e4b8cd47f7c 100644 --- a/compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/CodegenReactiveFunction.ts +++ b/compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/CodegenReactiveFunction.ts @@ -2104,7 +2104,7 @@ function codegenInstructionValue( key, fn.params, fn.body, - false, + property.key.kind === 'computed', ); babelNode.async = fn.async; babelNode.generator = fn.generator; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-method-shorthand-computed-key.expect.md b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-method-shorthand-computed-key.expect.md new file mode 100644 index 00000000000..701d4954adc --- /dev/null +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-method-shorthand-computed-key.expect.md @@ -0,0 +1,55 @@ + +## Input + +```javascript +const computedPropKey = 'foobar'; + +function Component(props) { + const obj = { + [computedPropKey]() { + return props.value; + }, + }; + return obj[computedPropKey](); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: 42}], +}; + +``` + +## Code + +```javascript +import { c as _c } from "react/compiler-runtime"; +const computedPropKey = "foobar"; + +function Component(props) { + const $ = _c(2); + let t0; + if ($[0] !== props) { + const obj = { + [computedPropKey]() { + return props.value; + }, + }; + t0 = obj[computedPropKey](); + $[0] = props; + $[1] = t0; + } else { + t0 = $[1]; + } + return t0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{ value: 42 }], +}; + +``` + +### Eval output +(kind: ok) 42 \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-method-shorthand-computed-key.js b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-method-shorthand-computed-key.js new file mode 100644 index 00000000000..75a0fa36a87 --- /dev/null +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-method-shorthand-computed-key.js @@ -0,0 +1,15 @@ +const computedPropKey = 'foobar'; + +function Component(props) { + const obj = { + [computedPropKey]() { + return props.value; + }, + }; + return obj[computedPropKey](); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{value: 42}], +}; diff --git a/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-method-shorthand-computed-key.tsx b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-method-shorthand-computed-key.tsx new file mode 100644 index 00000000000..a73f5e60823 --- /dev/null +++ b/compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/object-method-shorthand-computed-key.tsx @@ -0,0 +1,24 @@ +const computedPropKey = 'foobar'; + +function Bar({obj}: {obj: any}) { + return
{obj[computedPropKey]()}
; +} + +function Component() { + return ( +
+ +
+ ); +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +};