Skip to content
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

Fix evaluation order with object spread, 2 #11471

Merged
merged 1 commit into from Apr 26, 2020

Conversation

jridgewell
Copy link
Member

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

When reviewing #11412, I noticed in the TypeScript link that there are more cases where the intermediate values can mutate the spread reference. In { ...obj, p }, obj can contain a getter prop that mutates the p binding. If were to continue transforming into _objectSpread({}, obj, { p }), then the p binding will not be mutated correctly.

We may want to just abandon objectSpread and objectSpread2, and introduce spreadWithGet and spreadWithGetOwnProperty helpers to handle the exact arg as needed.

When reviewing babel#11412, I noticed there are more cases where the intermediate values can mutate the spread reference. It's best demonstrated in the [TypeScript](https://github.com/microsoft/TypeScript/blob/eb105efdcd6db8a73f5b983bf329cb7a5eee55e1/src/compiler/transformers/es2018.ts#L272) examples.
@babel-bot
Copy link
Collaborator

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

@existentialism existentialism added the PR: Bug Fix 🐛 A type of pull request used for our changelog categories label Apr 24, 2020
Copy link
Member

@nicolo-ribaudo nicolo-ribaudo left a comment

Choose a reason for hiding this comment

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

FWIW, I don't "like" it 🙃

@nicolo-ribaudo nicolo-ribaudo added PR: Spec Compliance 👓 A type of pull request used for our changelog categories and removed PR: Bug Fix 🐛 A type of pull request used for our changelog categories labels Apr 25, 2020
@nicolo-ribaudo
Copy link
Member

introduce spreadWithGet and spreadWithGetOwnProperty helpers to handle the exact arg as needed.

Would these functions only take two arguments rather than any number of them?

@jridgewell
Copy link
Member Author

Would these functions only take two arguments rather than any number of them?

Yah, essentially. But it would eliminate the weird empty objects we get in var simpleOutput = _objectSpread(_objectSpread({}, pureA), {}, { test: '1' }, pureB);

@nicolo-ribaudo
Copy link
Member

I am neutral to introducing those two helpers; if you think that it would improve the output feel free to prepare a PR!

@nicolo-ribaudo nicolo-ribaudo merged commit c1d65d8 into babel:master Apr 26, 2020
@cpojer
Copy link
Member

cpojer commented May 4, 2020

When upgrading a large codebase from 7.9.4 to 7.9.6 I notice that a lot of code now receives extra spread calls: babelHelpers.objectSpread2(babelHelpers.objectSpread2({}, sharedData), {}, {…}).

Is this duplication expected? I see a lot of empty object literals and duplicated function calls in the output which looks inefficient to me.

@cpojer
Copy link
Member

cpojer commented May 4, 2020

I can confirm that this change increases our app's size in React Native by about 60k which is an unfortunate regression.

@hzoo
Copy link
Member

hzoo commented May 4, 2020

@cpojer this is good to know, we may need to see the cases where it's increasing the size for what usage.. Yeah we need to look into how to add this in without affecting it when it doesn't need to be there if possible.

Makes me think in general we also need a larger discussion about how we handle spec compliance and default behavior. Tradeoffs of this since fixing bugs is always good but has a cost. And introducing new flags to support what should be correct behavior isn't great either? Probably needs judgement on per case basis at least in the past but we need automated check on effect of codesize @existentialism .

@cpojer
Copy link
Member

cpojer commented May 4, 2020

Could we not move more stuff into the helper? I don't know why empty objects need to be created so much and then passed around.

@developit
Copy link
Member

@cpojer any chance you're using the useSpread option? If not producing modern output, that will trigger this helper instead of the JSX-specific assign() polyfill (which doesn't have to account for these edge cases).

@cpojer
Copy link
Member

cpojer commented May 4, 2020

We are using the new experimental JSX transform.

@jridgewell
Copy link
Member Author

I see a lot of empty object literals and duplicated function calls in the output which looks inefficient to me.

We had discussed creating a special spreadWithGet and spreadWithGetOwnProperty helpers, instead of reusing objectSpread helper for both cases. This would get rid of the weird empty objects (which are caused by the weird API we did for objectSpread).

Could we not move more stuff into the helper?

In this case, unfortunately not. We need multiple calls in order to get the semantics correctly.

Makes me think in general we also need a larger discussion about how we handle spec compliance and default behavior. Tradeoffs of this since fixing bugs is always good but has a cost. And introducing new flags to support what should be correct behavior isn't great either?

@hzoo: Could we change the output for loose mode?

@jridgewell jridgewell deleted the object-spread-fixes branch May 4, 2020 16:27
@nicolo-ribaudo
Copy link
Member

Could we change the output for loose mode?

I thought we already did it. If we don't, then we should fix it.

@hzoo
Copy link
Member

hzoo commented May 8, 2020

We merged #11520 which changes the output in loose

@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Aug 8, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 8, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: Spec Compliance 👓 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants