@@ -36,34 +36,72 @@ export const transformExportAssignment: FunctionVisitor<ts.ExportAssignment> = (
3636 }
3737} ;
3838
39- function transformExportAllFrom ( context : TransformationContext , node : ts . ExportDeclaration ) : lua . Statement | undefined {
39+ function transformExportAll ( context : TransformationContext , node : ts . ExportDeclaration ) : lua . Statement | undefined {
4040 assert ( node . moduleSpecifier ) ;
4141
4242 if ( ! context . resolver . moduleExportsSomeValue ( node . moduleSpecifier ) ) {
4343 return undefined ;
4444 }
4545
4646 const moduleRequire = createModuleRequire ( context , node . moduleSpecifier ) ;
47- const tempModuleIdentifier = lua . createIdentifier ( "____export" ) ;
4847
48+ // export * as ns from "...";
49+ // exports.ns = require(...)
50+ if ( node . exportClause && ts . isNamespaceExport ( node . exportClause ) ) {
51+ const assignToExports = lua . createAssignmentStatement (
52+ lua . createTableIndexExpression (
53+ createExportsIdentifier ( ) ,
54+ lua . createStringLiteral ( node . exportClause . name . text )
55+ ) ,
56+ moduleRequire
57+ ) ;
58+ return assignToExports ;
59+ }
60+
61+ // export * from "...";
62+ // exports all values EXCEPT "default" from "..."
63+ const result : lua . Statement [ ] = [ ] ;
64+
65+ // local ____export = require(...)
66+ const tempModuleIdentifier = lua . createIdentifier ( "____export" ) ;
4967 const declaration = lua . createVariableDeclarationStatement ( tempModuleIdentifier , moduleRequire ) ;
68+ result . push ( declaration ) ;
5069
70+ // ____exports[____exportKey] = ____exportValue
5171 const forKey = lua . createIdentifier ( "____exportKey" ) ;
5272 const forValue = lua . createIdentifier ( "____exportValue" ) ;
73+ const leftAssignment = lua . createAssignmentStatement (
74+ lua . createTableIndexExpression ( createExportsIdentifier ( ) , forKey ) ,
75+ forValue
76+ ) ;
5377
54- const body = lua . createBlock ( [
55- lua . createAssignmentStatement ( lua . createTableIndexExpression ( createExportsIdentifier ( ) , forKey ) , forValue ) ,
56- ] ) ;
78+ // if key ~= "default" then
79+ // -- export the value, do not export "default" values
80+ // end
81+ const ifBody = lua . createBlock ( [ leftAssignment ] ) ;
82+ const ifStatement = lua . createIfStatement (
83+ lua . createBinaryExpression (
84+ lua . cloneIdentifier ( forKey ) ,
85+ lua . createStringLiteral ( "default" ) ,
86+ lua . SyntaxKind . InequalityOperator
87+ ) ,
88+ ifBody
89+ ) ;
5790
91+ // for ____exportKey, ____exportValue in ____export do
92+ // -- export ____exportValue, unless ____exportKey is "default"
93+ // end
5894 const pairsIdentifier = lua . createIdentifier ( "pairs" ) ;
5995 const forIn = lua . createForInStatement (
60- body ,
96+ lua . createBlock ( [ ifStatement ] ) ,
6197 [ lua . cloneIdentifier ( forKey ) , lua . cloneIdentifier ( forValue ) ] ,
6298 [ lua . createCallExpression ( pairsIdentifier , [ lua . cloneIdentifier ( tempModuleIdentifier ) ] ) ]
6399 ) ;
64100
101+ result . push ( forIn ) ;
102+
65103 // Wrap this in a DoStatement to prevent polluting the scope.
66- return lua . createDoStatement ( [ declaration , forIn ] , node ) ;
104+ return lua . createDoStatement ( result , node ) ;
67105}
68106
69107const isDefaultExportSpecifier = ( node : ts . ExportSpecifier ) =>
@@ -127,7 +165,7 @@ export const getExported = (context: TransformationContext, exportSpecifiers: ts
127165export const transformExportDeclaration : FunctionVisitor < ts . ExportDeclaration > = ( node , context ) => {
128166 if ( ! node . exportClause ) {
129167 // export * from "...";
130- return transformExportAllFrom ( context , node ) ;
168+ return transformExportAll ( context , node ) ;
131169 }
132170
133171 if ( ! context . resolver . isValueAliasDeclaration ( node ) ) {
@@ -136,7 +174,7 @@ export const transformExportDeclaration: FunctionVisitor<ts.ExportDeclaration> =
136174
137175 if ( ts . isNamespaceExport ( node . exportClause ) ) {
138176 // export * as ns from "...";
139- throw new Error ( "NamespaceExport is not supported" ) ;
177+ return transformExportAll ( context , node ) ;
140178 }
141179
142180 const exportSpecifiers = getExported ( context , node . exportClause ) ;
0 commit comments