@@ -49,23 +49,56 @@ export class ImportExportTranspiler implements TranspilerInterface {
4949 } ) ;
5050 }
5151 } ) ;
52- const addExportCall = t . callExpression ( t . identifier ( 'addExport' ) , [ t . objectExpression ( < t . ObjectProperty [ ] > declarations ) ] ) ;
52+
5353 if ( 'declaration' in path . node && path . node . declaration ) {
54- path . replaceWithMultiple ( [
55- path . node . declaration ,
56- t . callExpression ( t . identifier ( 'addExport' ) , [
57- t . objectExpression (
58- // @ts -ignore
59- path . get ( 'declaration' ) . get ( 'declarations' ) . map ( d => t . objectProperty (
60- d . get ( 'id' ) . node ,
61- d . get ( 'id' ) . node
62- ) )
63- )
64- ] )
65- ] ) ;
66- } else {
67- path . replaceWith ( addExportCall ) ;
54+ const decl = path . get ( 'declaration' ) ;
55+
56+ // If its FunctionDeclaration or ClassDeclaration
57+ if (
58+ t . isFunctionDeclaration ( decl . node ) ||
59+ t . isClassDeclaration ( decl . node )
60+ ) {
61+ const name = decl . node . id ;
62+ if ( ! name ) {
63+ reporter . syntaxError ( {
64+ message : 'Exported function/class must have a name' ,
65+ loc : decl . node . loc ,
66+ } ) ;
67+ return ;
68+ }
69+
70+ path . replaceWithMultiple ( [
71+ decl . node ,
72+ t . callExpression ( t . identifier ( 'addExport' ) , [
73+ t . objectExpression ( [ t . objectProperty ( name , name ) ] )
74+ ] )
75+ ] ) ;
76+ return ;
77+ }
78+
79+ // VariableDeclaration (export const foo = ...)
80+ if ( t . isVariableDeclaration ( decl . node ) ) {
81+ path . replaceWithMultiple ( [
82+ decl . node ,
83+ t . callExpression ( t . identifier ( 'addExport' ) , [
84+ t . objectExpression (
85+ // @ts -ignore
86+ decl . get ( 'declarations' ) . map ( d => t . objectProperty ( d . get ( 'id' ) . node , d . get ( 'id' ) . node ) )
87+ )
88+ ] )
89+ ] ) ;
90+ return ;
91+ }
92+
93+ reporter . syntaxError ( {
94+ message : `Unsupported export declaration of type '${ decl . node ?. type } '` ,
95+ loc : decl . node ?. loc ,
96+ } ) ;
97+ return ;
6898 }
99+
100+ const addExportCall = t . callExpression ( t . identifier ( 'addExport' ) , [ t . objectExpression ( < t . ObjectProperty [ ] > declarations ) ] ) ;
101+ path . replaceWith ( addExportCall ) ;
69102 } ,
70103 ExportDefaultDeclaration ( path ) {
71104 // @ts -ignore
0 commit comments