Skip to content

Commit

Permalink
Don't split ExportDefaultDeclaration in Class visitor
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Jun 1, 2018
1 parent 354b577 commit ba46a74
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 31 deletions.
50 changes: 19 additions & 31 deletions packages/babel-plugin-proposal-decorators/src/transformer.js
Expand Up @@ -162,6 +162,8 @@ export default ({ automaticParentheses }) => {
}

function transformClass(path, file, automaticParentheses) {
const isDeclaration = path.node.id && path.isDeclaration();

path.node.type = "ClassDeclaration";
if (!path.node.id) path.node.id = path.scope.generateUidIdentifier("class");

Expand All @@ -172,44 +174,30 @@ export default ({ automaticParentheses }) => {

insertInitializeInstanceElements(path, initializeId);

return template.expression.ast`
${file.addHelper("decorate")}(
${classDecorators || t.nullLiteral()},
function (${initializeId}) {
${path.node}
return { F: ${t.cloneNode(path.node.id)}, d: ${definitions} };
}
)
`;
const expr = template.expression.ast`
${file.addHelper("decorate")}(
${classDecorators || t.nullLiteral()},
function (${initializeId}) {
${path.node}
return { F: ${t.cloneNode(path.node.id)}, d: ${definitions} };
}
)
`;

return isDeclaration ? template.ast`let ${path.node.id} = ${expr}` : expr;
}

return {
ClassDeclaration(path) {
if (!hasDecorators(path)) return;

if (path.parentPath.isExportDefaultDeclaration()) {
if (!path.node.id) {
t.toExpression(path.node);
path.replaceWith(
transformClass(path, this.file, automaticParentheses),
);
return;
}
ExportDefaultDeclaration(path) {
let decl = path.get("declaration");
if (!decl.isClassDeclaration() || !hasDecorators(decl)) return;

path = splitExportDeclaration(path.parentPath);
}
if (decl.node.id) decl = splitExportDeclaration(path);

path.replaceWith(
t.variableDeclaration("let", [
t.variableDeclarator(
t.cloneNode(path.node.id),
transformClass(path, this.file),
),
]),
);
decl.replaceWith(transformClass(decl, this.file, automaticParentheses));
},

ClassExpression(path) {
Class(path) {
if (hasDecorators(path)) {
path.replaceWith(transformClass(path, this.file, automaticParentheses));
}
Expand Down
@@ -0,0 +1,2 @@
@dec()
export default class Foo {}
@@ -0,0 +1,14 @@
let Foo = babelHelpers.decorate([dec()], function (_initialize) {
class Foo {
constructor() {
_initialize(this);
}

}

return {
F: Foo,
d: []
};
});
export { Foo as default };

0 comments on commit ba46a74

Please sign in to comment.