Sebastian McKenzie fc0baf1e2c Merge pull request #2841 from hzoo/i-2694
transform-es2015-classes: check if node.id is null
2015-11-05 17:09:50 +00:00

35 lines
1005 B
JavaScript

import LooseTransformer from "./loose";
import VanillaTransformer from "./vanilla";
import nameFunction from "babel-helper-function-name";
export default function ({ types: t }) {
return {
visitor: {
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))
]));
},
ClassExpression(path, state) {
let inferred = nameFunction(path);
if (inferred && inferred !== path.node) return path.replaceWith(inferred);
let Constructor = VanillaTransformer;
if (state.opts.loose) Constructor = LooseTransformer;
path.replaceWith(new Constructor(path, state.file).run());
}
}
};
}