Merge pull request #3191 from vhf/destructuring-rest-args-initialization-order

Order of arguments initialization - fixes T6809
This commit is contained in:
Amjad Masad
2015-12-22 20:51:20 -08:00
3 changed files with 25 additions and 3 deletions

View File

@@ -3,8 +3,13 @@ import * as t from "babel-types";
export let visitor = {
Function(path) {
let params: Array = path.get("params");
for (let i = 0; i < params.length; i++) {
// If there's a rest param, no need to loop through it. Also, we need to
// hoist one more level to get `declar` at the right spot.
let hoistTweak = t.isRestElement(params[params.length - 1]) ? 1 : 0;
let outputParamsLength = params.length - hoistTweak;
for (let i = 0; i < outputParamsLength; i++) {
let param = params[i];
if (param.isArrayPattern() || param.isObjectPattern()) {
let uid = path.scope.generateUidIdentifier("ref");
@@ -12,7 +17,7 @@ export let visitor = {
let declar = t.variableDeclaration("let", [
t.variableDeclarator(param.node, uid)
]);
declar._blockHoist = params.length - i;
declar._blockHoist = outputParamsLength - i;
path.ensureBlock();
path.get("body").unshiftContainer("body", declar);

View File

@@ -0,0 +1,4 @@
// T6809
function t(x = "default", { a, b }, ...args) {
console.log(x, a, b, args);
}

View File

@@ -0,0 +1,13 @@
// T6809
function t() {
var x = arguments.length <= 0 || arguments[0] === undefined ? "default" : arguments[0];
var _ref = arguments[1];
var a = _ref.a;
var b = _ref.b;
for (var _len = arguments.length, args = Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
args[_key - 2] = arguments[_key];
}
console.log(x, a, b, args);
}