Allow patterns as argument of RestElement (#8414)
This commit is contained in:
parent
d9149aa2f3
commit
9eef660daa
@ -228,10 +228,20 @@ export default function convertFunctionRest(path) {
|
|||||||
const { node, scope } = path;
|
const { node, scope } = path;
|
||||||
if (!hasRest(node)) return false;
|
if (!hasRest(node)) return false;
|
||||||
|
|
||||||
const rest = node.params.pop().argument;
|
let rest = node.params.pop().argument;
|
||||||
|
|
||||||
const argsId = t.identifier("arguments");
|
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
|
// check and optimise for extremely common cases
|
||||||
const state = {
|
const state = {
|
||||||
references: [],
|
references: [],
|
||||||
|
|||||||
5
packages/babel-plugin-transform-parameters/test/fixtures/parameters/rest-patterns/exec.js
vendored
Normal file
5
packages/babel-plugin-transform-parameters/test/fixtures/parameters/rest-patterns/exec.js
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
function foo(...{ length }) {
|
||||||
|
return length;
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(foo(1, 2, 3)).toEqual(3);
|
||||||
1
packages/babel-plugin-transform-parameters/test/fixtures/parameters/rest-patterns/input.js
vendored
Normal file
1
packages/babel-plugin-transform-parameters/test/fixtures/parameters/rest-patterns/input.js
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
function foo(...[a]) {}
|
||||||
7
packages/babel-plugin-transform-parameters/test/fixtures/parameters/rest-patterns/output.js
vendored
Normal file
7
packages/babel-plugin-transform-parameters/test/fixtures/parameters/rest-patterns/output.js
vendored
Normal 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];
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user