Support exporting deep destructuring (#5953)

This commit is contained in:
Justin Ridgewell 2017-07-18 15:07:09 -04:00 committed by Brian Ng
parent 8a5488e59f
commit 827c70e015
6 changed files with 35 additions and 53 deletions

View File

@ -11,9 +11,9 @@ define(["exports"], function (exports) {
var foo3 = exports.foo3 = function () {};
var foo4 = exports.foo4 = undefined;
var foo4 = exports.foo4 = void 0;
let foo5 = exports.foo5 = 2;
let foo6 = exports.foo6 = undefined;
let foo6 = exports.foo6 = void 0;
const foo7 = exports.foo7 = 3;
function foo8() {}
@ -21,4 +21,4 @@ define(["exports"], function (exports) {
class foo9 {}
exports.foo9 = foo9;
});
});

View File

@ -372,55 +372,27 @@ export default function() {
]);
nonHoistedExportNames[id.name] = true;
} else if (declaration.isVariableDeclaration()) {
const declarators = declaration.get("declarations");
for (const decl of declarators) {
const id = decl.get("id");
const ids = declaration.getBindingIdentifierPaths();
const exportsToInsert = [];
for (const name in ids) {
const id = ids[name];
const { parentPath, node } = id;
const init = decl.get("init");
const exportsToInsert = [];
addTo(exports, name, node);
nonHoistedExportNames[name] = true;
if (!init.node) init.replaceWith(t.identifier("undefined"));
if (id.isIdentifier()) {
addTo(exports, id.node.name, id.node);
init.replaceWith(
buildExportsAssignment(id.node, init.node).expression,
if (parentPath.isVariableDeclarator()) {
const init = parentPath.get("init");
const assignment = buildExportsAssignment(
node,
init.node || path.scope.buildUndefinedNode(),
);
nonHoistedExportNames[id.node.name] = true;
} else if (id.isObjectPattern()) {
for (let i = 0; i < id.node.properties.length; i++) {
const prop = id.node.properties[i];
let propValue = prop.value;
if (t.isAssignmentPattern(propValue)) {
propValue = propValue.left;
} else if (t.isRestProperty(prop)) {
propValue = prop.argument;
}
addTo(exports, propValue.name, propValue);
exportsToInsert.push(
buildExportsAssignment(propValue, propValue),
);
nonHoistedExportNames[propValue.name] = true;
}
} else if (id.isArrayPattern() && id.node.elements) {
for (let i = 0; i < id.node.elements.length; i++) {
let elem = id.node.elements[i];
if (!elem) continue;
if (t.isAssignmentPattern(elem)) {
elem = elem.left;
} else if (t.isRestElement(elem)) {
elem = elem.argument;
}
const name = elem.name;
addTo(exports, name, elem);
exportsToInsert.push(
buildExportsAssignment(elem, elem),
);
nonHoistedExportNames[name] = true;
}
init.replaceWith(assignment.expression);
} else {
exportsToInsert.push(buildExportsAssignment(node, node));
}
path.insertAfter(exportsToInsert);
}
path.insertAfter(exportsToInsert);
path.replaceWith(declaration.node);
}
continue;

View File

@ -10,13 +10,13 @@ var foo2 = exports.foo2 = 1,
var foo3 = exports.foo3 = function () {};
var foo4 = exports.foo4 = undefined;
var foo4 = exports.foo4 = void 0;
let foo5 = exports.foo5 = 2;
let foo6 = exports.foo6 = undefined;
let foo6 = exports.foo6 = void 0;
const foo7 = exports.foo7 = 3;
function foo8() {}
class foo9 {}
exports.foo9 = foo9;
exports.foo9 = foo9;

View File

@ -0,0 +1 @@
export const { foo: { bar: [baz, qux] } } = {};

View File

@ -0,0 +1,9 @@
"use strict";
const {
foo: {
bar: [baz, qux]
}
} = {};
exports.baz = baz;
exports.qux = qux;

View File

@ -23,9 +23,9 @@
var foo3 = exports.foo3 = function () {};
var foo4 = exports.foo4 = undefined;
var foo4 = exports.foo4 = void 0;
let foo5 = exports.foo5 = 2;
let foo6 = exports.foo6 = undefined;
let foo6 = exports.foo6 = void 0;
const foo7 = exports.foo7 = 3;
function foo8() {}
@ -33,4 +33,4 @@
class foo9 {}
exports.foo9 = foo9;
});
});