Avoid duplicating traversal of class declarations - fixes T2694

This commit is contained in:
Logan Smyth
2015-11-18 23:57:01 -08:00
parent 82ddbc0ecd
commit b3f2ea0aa1
4 changed files with 74 additions and 6 deletions

View File

@@ -8,16 +8,23 @@ export default function ({ types: t }) {
return {
visitor: {
ExportDefaultDeclaration(path){
if (!path.get("declaration").isClassDeclaration()) return;
let { node } = path;
let ref = node.declaration.id || path.scope.generateUidIdentifier("class");
node.declaration.id = ref;
// Split the class declaration and the export into two separate statements.
path.replaceWith(node.declaration);
path.insertAfter(t.exportDefaultDeclaration(ref));
},
ClassDeclaration(path) {
let { node } = path;
let ref = node.id || path.scope.generateUidIdentifier("class");
if (path.parentPath.isExportDefaultDeclaration()) {
path = path.parentPath;
path.insertAfter(t.exportDefaultDeclaration(ref));
}
path.replaceWith(t.variableDeclaration("let", [
t.variableDeclarator(ref, t.toExpression(node))
]));