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

transpiled rest param assumes Array is built-ins #11744

Closed
yortus opened this issue Jun 25, 2020 · 3 comments
Closed

transpiled rest param assumes Array is built-ins #11744

yortus opened this issue Jun 25, 2020 · 3 comments
Labels
i: bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@yortus
Copy link

yortus commented Jun 25, 2020

Bug Report

The transpiled code for rest params may have different semantics to the original code. The transpiled code injects a reference to Array, assuming it is the builtin array constructor. However if another identifier called Array is in scope, it will be referenced instead.

Input Code

let Array = [];
let f = (...args) => args;
console.log(f(1, 2, 3));

Current behavior

Running the transpiled code for this example throws "TypeError: Array is not a constructor".

Expected behavior
Running the transpiled code outputs [1, 2, 3].

Environment

  • Babel version: v7.10.3
  • How you are using Babel: babeljs.io/repl
@babel-bot
Copy link
Collaborator

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

@JLHwung
Copy link
Contributor

JLHwung commented Jun 25, 2020

Currently Babel output assumes the built-ins are not modified in a spec-incompliant way. In your case it is shadowed and Array is not a constructor anymore.

@JLHwung JLHwung closed this as completed Jun 25, 2020
@JLHwung JLHwung changed the title transpiled rest param changes semantics of code transpiled rest param assumes Array is built-ins Jun 25, 2020
@yortus
Copy link
Author

yortus commented Jun 26, 2020

Currently Babel output assumes the built-ins are not modified in a spec-incompliant way.

The builtin is not modified, and there is nothing spec-incompliant in the sample code.

This is simple shadowing by an ordinary variable in lexical scope. Babel is just making a faulty assumption here.

The section added to caveats.md in the linked PR (babel/website#2283) doesn't cover babel's assumptions about simple shadowing. To cover this case and eg #10807, I suggest extending the wording in caveats.md to add babel's assumption that the program will not have any variables in scope with the same name as builtins.

Alternatively, since babel knows the variable names in the program being transpiled, it could issue an error here since the output would be invalid. Or even better, refer to builtins in a way that can't clash with local names.

@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 Sep 25, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 25, 2020
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

No branches or pull requests

3 participants