Skip to content

Fix (@babel/plugin-transform-computed-properties): rest spread data is lost - #17150

Closed
KHZhanibek wants to merge 4 commits into
babel:mainfrom
KHZhanibek:main
Closed

KHZhanibek wants to merge 4 commits into
babel:mainfrom
KHZhanibek:main

Conversation

@KHZhanibek

@KHZhanibek KHZhanibek commented Feb 24, 2025 •

Copy link
Copy Markdown
Q                       A
Fixed Issues? Fixes #16233
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

@babel-bot

babel-bot commented Feb 24, 2025 •

Copy link
Copy Markdown
Collaborator

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/58785

@KHZhanibek
KHZhanibek force-pushed the main branch 2 times, most recently from 0e7263d to c7c76f5 Compare February 24, 2025 11:56
@nicolo-ribaudo nicolo-ribaudo added the PR: Bug Fix 🐛 A type of pull request used for our changelog categories label Feb 24, 2025

@nicolo-ribaudo nicolo-ribaudo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is reasonable, but I'm curious to know why one would transpile computed properties without transpiling object spread.

...first,
a: 'a',
['c']: 'c'
} No newline at end of file

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you also add ...second after the computed property?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

like just type ...second on 10th line?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

or you mean ...first after computed property?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

...second at the end, so that there are both first and second :)

['c']: 'c',
}

const third = {...second} No newline at end of file

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

like that @nicolo-ribaudo?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sorry, I didn't realize you already had a second variable so I wasn't clear 😅

I meant this:

const second = {
  ...first,
  a: 'a',
  ['c']: 'c',
  ...third
}

@nicolo-ribaudo nicolo-ribaudo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, pending #17150 (comment)

JLHwung
JLHwung previously approved these changes Mar 2, 2025

@JLHwung JLHwung left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks. Left a non-blocker.


for (const prop of node.properties) {
if (t.isSpreadElement(prop)) {
initProps.push(prop);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This for loop seems a bit too verbose, could you help simplify it?

...first,
a: 'a',
...third
}, 'c', 'c'); No newline at end of file

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is wrong: if third contains c: 1, second should contain c: 1 but with this output code it will contain c: 'c'. You should take a look at how we make sure that normal properies that come after the computer property are defined after the defineProperty call, and do the same.

@JLHwung
JLHwung dismissed their stale review March 2, 2025 03:08

incorrect output.

opensource-fork-sync Bot pushed a commit to cla7aye15I4nd/opensource-babel__babel that referenced this pull request Nov 17, 2026
* fix(plugin-transform-computed-properties): keep spread elements

When an object literal had both a computed key and a spread element,
and object rest/spread was not transformed, the spread element was
dropped from the output.

Spread elements before the first computed key now stay in the initial
object literal. Those after it are applied in source order with the
objectSpread2 helper, so that they still override the properties
defined before them.

Fixes babel#16233
Closes babel#17150

Assisted-by: Claude Fable 5.2

* address review comments

Throw an error instead of compiling spread elements that follow
a computed key.

Spread elements before the first computed key still stay in the
initial object literal.

Assisted-by: Claude Opus 5.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR: Bug Fix 🐛 A type of pull request used for our changelog categories

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: rest spread data is lost when @babel/plugin-transform-computed-properties is used alone

4 participants