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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: plugin-transform-object-super incorrect output where object created in a loop #15945

Closed
1 task
overlookmotel opened this issue Sep 8, 2023 · 1 comment 路 Fixed by #15948
Closed
1 task
Labels
i: bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@overlookmotel
Copy link
Contributor

馃捇

  • Would you like to work on a fix?

How are you using Babel?

Programmatic API (babel.transform, babel.parse)

Input code

const objects = [];
for (const proto of [{x: 0}, {x: 1}]) {
  objects.push({
    __proto__: proto,
    foo() {
      return super.x
    }
  });
}
assert(objects[0].foo() === 0);
assert(objects[1].foo() === 1);

REPL

Configuration file name

No response

Configuration

No response

Current and expected behavior

In the original code, the assertions pass. When transformed with @babel/plugin-transform-object-super, the first assertion fails.

The transformed output is:

const objects = [];
for (const proto of [{x: 0}, {x: 1}]) {
  var _obj;
  objects.push(
    (_obj = {
      __proto__: proto,
      foo() {
        return _get(_getPrototypeOf(_obj), "x", this);
      }
    })
  );
}
assert(objects[0].foo() === 0);
assert(objects[1].foo() === 1);

Environment

REPL

Possible solution

The problem is that _obj for the 1st turn of the loop is over-written in 2nd turn, because _obj is not block scoped. Could be solved by changing var _obj to let _obj.

Please see REPL.

Additional context

No response

@babel-bot
Copy link
Collaborator

Hey @overlookmotel! 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.

@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 Jan 23, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 23, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
i: bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants