Skip to content

Commit

Permalink
Allow patterns as argument of RestElement (#8414)
Browse files Browse the repository at this point in the history
  • Loading branch information
microbouji authored and nicolo-ribaudo committed Sep 6, 2018
1 parent d9149aa commit 9eef660
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
12 changes: 11 additions & 1 deletion packages/babel-plugin-transform-parameters/src/rest.js
Expand Up @@ -228,10 +228,20 @@ export default function convertFunctionRest(path) {
const { node, scope } = path;
if (!hasRest(node)) return false;

const rest = node.params.pop().argument;
let rest = node.params.pop().argument;

const argsId = t.identifier("arguments");

if (t.isPattern(rest)) {
const pattern = rest;
rest = scope.generateUidIdentifier("ref");

const declar = t.variableDeclaration("let", [
t.variableDeclarator(pattern, rest),
]);
node.body.body.unshift(declar);
}

// check and optimise for extremely common cases
const state = {
references: [],
Expand Down
@@ -0,0 +1,5 @@
function foo(...{ length }) {
return length;
}

expect(foo(1, 2, 3)).toEqual(3);
@@ -0,0 +1 @@
function foo(...[a]) {}
@@ -0,0 +1,7 @@
function foo() {
for (var _len = arguments.length, _ref = new Array(_len), _key = 0; _key < _len; _key++) {
_ref[_key] = arguments[_key];
}

var a = _ref[0];
}

0 comments on commit 9eef660

Please sign in to comment.