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

Spread operator mismatches native behavior when getter is used #8749

Closed
Buslowicz opened this issue Sep 21, 2018 · 4 comments
Closed

Spread operator mismatches native behavior when getter is used #8749

Buslowicz opened this issue Sep 21, 2018 · 4 comments
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@Buslowicz
Copy link

Bug Report

Current Behavior
When using object spread on an object with a getter, the function is triggered only once, and the result is copied to the output object.

Input Code

let counter = 0;

const x = { a: 1 };
const y = { ...x, get b() { counter++; return 2; } };
y.b;
y.b;

console.assert(counter === 2);

Expected behavior/code
Getter should be created in the output object so it can be triggered on each property access, as it happens with native browser implementation.

Babel Configuration (.babelrc, package.json, cli command)
Anything that compiles to ES5.

Environment
Any

Possible Solution
Use Object.getOwnPropertyDescriptors(input) to get a configuration of each input object and in case of getters/setters define them in the output object.

Additional context/Screenshots
This bug happens in both Babel and TypeScript, looks to be pretty common.

@babel-bot
Copy link
Collaborator

Hey @Draccoz! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community that typically always has someone willing to help. You can sign-up here
for an invite.

@mathieutu
Copy link

The same here:

{
  ...attributes,
  get fullName() {
    return `${this.firstName} ${this.lastName}`.trim()
  },
}

Will transpile to (pseudo code):

Object.assign({}, attributes, {
   get fullName() {
    return `${this.firstName} ${this.lastName}`.trim()
  }
})

so all this.* parameters will be undefined.

Works well in browser with direct spread operators.

@mathieutu
Copy link

mathieutu commented Jun 1, 2019

Actually I found:
#7524
#7034
#9322
and https://babeljs.io/docs/en/next/babel-plugin-proposal-object-rest-spread.html

About this topic.
This should be handled by now, so I don't understand why it isn't working on our own.

I'm using a brand new create-react-app... 🤔

EDIT:

It was reverted in: #9379, and then a new PR was merged 7 days ago (so unreleased for now): #9384.

It should be fixed with the next released.

@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 May 3, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 3, 2021
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
Projects
None yet
Development

No branches or pull requests

4 participants