Skip to content

Improve rest spread output - #17023

Open
liuxingbaoyu wants to merge 5 commits into
babel:mainfrom
liuxingbaoyu:rest-spread
Open

liuxingbaoyu wants to merge 5 commits into
babel:mainfrom
liuxingbaoyu:rest-spread

Conversation

@liuxingbaoyu

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

@liuxingbaoyu liuxingbaoyu added the PR: Output optimization 🔬 A type of pull request used for our changelog categories label Dec 14, 2024
@babel-bot

babel-bot commented Dec 14, 2024 •

Copy link
Copy Markdown
Collaborator

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

Comment on lines +5 to +7
({
_c = c2, {
a2
} = _c);
b2 = babelHelpers.objectWithoutProperties(_c, ["a2"]);
_c;
} = _c, b2 = babelHelpers.objectWithoutProperties(_c, ["a2"]);

@nicolo-ribaudo nicolo-ribaudo Jan 10, 2025 •

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.

In all these cases, could we generate _c = { a2 } = c2, b2 = babelHelpers.objectWithoutProperties(_c, ["a2"]) instead? It can also be done as a separate PR, this one looks good already.

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.

In cases where the value c2 is not read, then we could make it even smaller:

b2 = babelHelpers.objectWithoutProperties({ a2 } = c2, ["a2"])

with no temp variable.

]),
);
let refName;
if (t.isIdentifier(right) && path.scope.hasBinding(right.name)) {

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.

Should we also check if the binding is constant? A getter in the destructured object could reassign it.

let a = { get x() { a = { z: 2 } }, y: 2 };

let x, rest;
({ x, ...rest } = a);

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.

(add this as an exec test)

(pureGetters ||
path.scope.getBinding(right.name)?.constantViolations.every(
// declared in loop
p => p.isVariableDeclarator(),

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.

Why do we need to check this?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

for(var x of y)
x will be registered as constantViolations.

@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.

One minor comment, but the implementation looks good

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR: Output optimization 🔬 A type of pull request used for our changelog categories Spec: Object Rest/Spread

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: suboptimal output for object rest spread ({ a2, ...b2 } = c2);

3 participants