fix(export default class): properly support to use ClassExpression
Also rename default anonymous classes as `default`.
MODIFY
src/babelPlugin.js
src/babelPlugin.js
@@ -265,7 +265,45 @@ function makeModulePlugins(options) {
hiddenIdentifier(h.HIDDEN_ONCE),
t.identifier('default'),
);
- path.replaceWith(t.callExpression(callee, [path.node.declaration]));
+ let decl = path.node.declaration;
+ if (decl.type === 'ClassDeclaration') {
+ decl = t.classExpression(decl.id, decl.superClass, decl.body);
+ if (!decl.id) {
+ // We need to override the class's empty name with `default`.
+ const id = t.identifier(`${h.HIDDEN_CONST_VAR_PREFIX}default`);
+ allowedHiddens.add(id);
+ path.replaceWithMultiple([
+ t.variableDeclaration('const', [
+ t.variableDeclarator(id, decl),
+ ]),
+ t.expressionStatement(
+ t.callExpression(
+ t.memberExpression(
+ t.identifier('Object'),
+ t.identifier('defineProperty'),
+ ),
+ [
+ id,
+ t.stringLiteral('name'),
+ t.objectExpression([
+ t.objectProperty(
+ t.stringLiteral('value'),
+ t.stringLiteral('default'),
+ ),
+ t.objectProperty(
+ t.stringLiteral('writable'),
+ t.booleanLiteral(false),
+ ),
+ ]),
+ ],
+ ),
+ ),
+ t.expressionStatement(t.callExpression(callee, [id])),
+ ]);
+ return;
+ }
+ }
+ path.replaceWith(t.callExpression(callee, [decl]));
}
},
ClassDeclaration(path) {

MODIFY
test/test-export-named.js
test/test-export-named.js
@@ -97,7 +97,7 @@ export const { def, nest: [, ghi, ...nestrest], ...rest } = { def: 456, nest: [
test(`export hoisting`, async t => {
try {
- const importer = makeImporter(['abc', 'fn', 'C', 'count']);
+ const importer = makeImporter(['abc', 'fn']);
const transforms = [makeModuleTransformer(babelCore, importer)];
const { evaluateModule } = makeEvaluators({
transforms,
@@ -140,6 +140,20 @@ export const fn3 = fn;
t.equal(fn2, fn, `function hoisting`);
t.equal(fn, fn3, `function exports with hoisting`);
t.equal(fn(), 'foo', `fn evaluates`);
+ } catch (e) {
+ t.notEqual(e, e, 'unexpected exception');
+ } finally {
+ t.end();
+ }
+});
+
+test(`export class`, async t => {
+ try {
+ const importer = makeImporter(['C', 'count']);
+ const transforms = [makeModuleTransformer(babelCore, importer)];
+ const { evaluateModule } = makeEvaluators({
+ transforms,
+ });
const { C, count } = await evaluateModule(`\
export let count = 0;
@@ -147,6 +161,18 @@ export class C {} if (C) { count += 1; }
`);
t.assert(new C(), `class exports`);
t.equal(count, 1, `class C is global`);
+
+ const { default: C2 } = await evaluateModule(`\
+export default class C {}
+`);
+ t.assert(new C2(), `default class constructs`);
+ t.equal(C2.name, 'C', `C class name`);
+
+ const { default: C3 } = await evaluateModule(`\
+export default class {}
+`);
+ t.assert(new C3(), `default class constructs`);
+ t.equal(C3.name, 'default', `default class name`);
} catch (e) {
console.log('unexpected exception', e);
t.assert(false, e);

fix(export default class): properly support to use ClassExpression

Also rename default anonymous classes as `default`.