Allow patterns as argument of RestElement (#8414)

This commit is contained in:
Elian Ibaj 2018-09-06 22:35:37 +02:00 committed by Nicolò Ribaudo
parent d9149aa2f3
commit 9eef660daa
4 changed files with 24 additions and 1 deletions

View File

@ -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: [],

View File

@ -0,0 +1,5 @@
function foo(...{ length }) {
return length;
}
expect(foo(1, 2, 3)).toEqual(3);

View File

@ -0,0 +1 @@
function foo(...[a]) {}

View File

@ -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];
}