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

Allow patterns as argument of RestElement #8414

Merged
merged 1 commit into from Sep 6, 2018

Conversation

microbouji
Copy link
Member

@microbouji microbouji commented Aug 1, 2018

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

Patterns as arguments to RestElements were apparently removed back in 9a97d92 when, as mentioned there, they were not part of es6. They're part of the standard now, so this PR adds them back in.

function foo(...[a]) {}

will now get converted to:

function foo() {
  for (var _len = arguments.length, _ref = new Array(_len), _key = 0; _key < _len; _key++) {
    _ref[_key] = arguments[_key];
  }

  var [a] = _ref;
}

letting the destructuring plugin handle it from there ([a] = _ref => a = _ref[0]).

Note that this does not make any of the optimizations that are otherwise made when the argument is an identifier - e.g. even though a is not referenced inside the function it will still be made available. If any optimizations are possible I think they can be added in other PRs?

I'll try to make a backport for v6 as well. EDIT: Don't think it's possible to change babylon 6.x anymore.

@babel-bot
Copy link
Collaborator

babel-bot commented Aug 1, 2018

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

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.

A possible loose mode optimization (it a dfferent PR) can be this one:

function foo(a, b, ...[c, d]) {}
-->
function foo(a, b, c, d) {}

@existentialism existentialism added the PR: Bug Fix 🐛 A type of pull request used for our changelog categories label Aug 14, 2018
@nicolo-ribaudo nicolo-ribaudo merged commit 9eef660 into babel:master Sep 6, 2018
@nicolo-ribaudo
Copy link
Member

Thank you!

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: Bug Fix 🐛 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

parse error on BindingPattern in BindingRestElement Destructuring rest parameters
4 participants