@@ -94,7 +94,8 @@ function visitBlockStatements(
94
94
// update IIFE and replace variable statement and old IIFE
95
95
updatedStatements . splice ( uIndex , 2 , updateEnumIife (
96
96
currentStatement ,
97
- iife ,
97
+ iife [ 0 ] ,
98
+ iife [ 1 ] ,
98
99
) ) ;
99
100
// skip IIFE statement
100
101
oIndex ++ ;
@@ -158,7 +159,10 @@ function visitBlockStatements(
158
159
}
159
160
160
161
// TS 2.3 enums have statements that are inside a IIFE.
161
- function findTs2_3EnumIife ( name : string , statement : ts . Statement ) : ts . CallExpression | null {
162
+ function findTs2_3EnumIife (
163
+ name : string ,
164
+ statement : ts . Statement ,
165
+ ) : [ ts . CallExpression , ts . Expression | undefined ] | null {
162
166
if ( ! ts . isExpressionStatement ( statement ) ) {
163
167
return null ;
164
168
}
@@ -173,6 +177,7 @@ function findTs2_3EnumIife(name: string, statement: ts.Statement): ts.CallExpres
173
177
}
174
178
175
179
const callExpression = expression ;
180
+ let exportExpression ;
176
181
177
182
let argument = expression . arguments [ 0 ] ;
178
183
if ( ! ts . isBinaryExpression ( argument ) ) {
@@ -183,12 +188,14 @@ function findTs2_3EnumIife(name: string, statement: ts.Statement): ts.CallExpres
183
188
return null ;
184
189
}
185
190
191
+ let potentialExport = false ;
186
192
if ( argument . operatorToken . kind === ts . SyntaxKind . FirstAssignment ) {
187
193
if ( ! ts . isBinaryExpression ( argument . right )
188
194
|| argument . right . operatorToken . kind !== ts . SyntaxKind . BarBarToken ) {
189
195
return null ;
190
196
}
191
197
198
+ potentialExport = true ;
192
199
argument = argument . right ;
193
200
}
194
201
@@ -200,6 +207,10 @@ function findTs2_3EnumIife(name: string, statement: ts.Statement): ts.CallExpres
200
207
return null ;
201
208
}
202
209
210
+ if ( potentialExport && ! ts . isIdentifier ( argument . left ) ) {
211
+ exportExpression = argument . left ;
212
+ }
213
+
203
214
expression = expression . expression ;
204
215
while ( ts . isParenthesizedExpression ( expression ) ) {
205
216
expression = expression . expression ;
@@ -264,7 +275,7 @@ function findTs2_3EnumIife(name: string, statement: ts.Statement): ts.CallExpres
264
275
}
265
276
}
266
277
267
- return callExpression ;
278
+ return [ callExpression , exportExpression ] ;
268
279
}
269
280
270
281
// TS 2.2 enums have statements after the variable declaration, with index statements followed
@@ -362,9 +373,22 @@ function findEnumNameStatements(
362
373
return enumStatements ;
363
374
}
364
375
365
- function updateHostNode ( hostNode : ts . VariableStatement , expression : ts . Expression ) : ts . Statement {
376
+ function addPureComment < T extends ts . Node > ( node : T ) : T {
366
377
const pureFunctionComment = '@__PURE__' ;
367
378
379
+ return ts . addSyntheticLeadingComment (
380
+ node ,
381
+ ts . SyntaxKind . MultiLineCommentTrivia ,
382
+ pureFunctionComment ,
383
+ false ,
384
+ ) ;
385
+ }
386
+
387
+ function updateHostNode (
388
+ hostNode : ts . VariableStatement ,
389
+ expression : ts . Expression ,
390
+ ) : ts . Statement {
391
+
368
392
// Update existing host node with the pure comment before the variable declaration initializer.
369
393
const variableDeclaration = hostNode . declarationList . declarations [ 0 ] ;
370
394
const outerVarStmt = ts . updateVariableStatement (
@@ -377,12 +401,7 @@ function updateHostNode(hostNode: ts.VariableStatement, expression: ts.Expressio
377
401
variableDeclaration ,
378
402
variableDeclaration . name ,
379
403
variableDeclaration . type ,
380
- ts . addSyntheticLeadingComment (
381
- expression ,
382
- ts . SyntaxKind . MultiLineCommentTrivia ,
383
- pureFunctionComment ,
384
- false ,
385
- ) ,
404
+ expression ,
386
405
) ,
387
406
] ,
388
407
) ,
@@ -391,12 +410,22 @@ function updateHostNode(hostNode: ts.VariableStatement, expression: ts.Expressio
391
410
return outerVarStmt ;
392
411
}
393
412
394
- function updateEnumIife ( hostNode : ts . VariableStatement , iife : ts . CallExpression ) : ts . Statement {
413
+ function updateEnumIife (
414
+ hostNode : ts . VariableStatement ,
415
+ iife : ts . CallExpression ,
416
+ exportAssignment ?: ts . Expression ,
417
+ ) : ts . Statement {
395
418
if ( ! ts . isParenthesizedExpression ( iife . expression )
396
419
|| ! ts . isFunctionExpression ( iife . expression . expression ) ) {
397
420
throw new Error ( 'Invalid IIFE Structure' ) ;
398
421
}
399
422
423
+ // Ignore export assignment if variable is directly exported
424
+ if ( hostNode . modifiers
425
+ && hostNode . modifiers . findIndex ( m => m . kind == ts . SyntaxKind . ExportKeyword ) != - 1 ) {
426
+ exportAssignment = undefined ;
427
+ }
428
+
400
429
const expression = iife . expression . expression ;
401
430
const updatedFunction = ts . updateFunctionExpression (
402
431
expression ,
@@ -415,17 +444,29 @@ function updateEnumIife(hostNode: ts.VariableStatement, iife: ts.CallExpression)
415
444
) ,
416
445
) ;
417
446
447
+ let arg : ts . Expression = ts . createObjectLiteral ( ) ;
448
+ if ( exportAssignment ) {
449
+ arg = ts . createBinary ( exportAssignment , ts . SyntaxKind . BarBarToken , arg ) ;
450
+ }
418
451
const updatedIife = ts . updateCall (
419
452
iife ,
420
453
ts . updateParen (
421
454
iife . expression ,
422
455
updatedFunction ,
423
456
) ,
424
457
iife . typeArguments ,
425
- iife . arguments ,
458
+ [ arg ] ,
426
459
) ;
427
460
428
- return updateHostNode ( hostNode , updatedIife ) ;
461
+ let value : ts . Expression = addPureComment ( updatedIife ) ;
462
+ if ( exportAssignment ) {
463
+ value = ts . createBinary (
464
+ exportAssignment ,
465
+ ts . SyntaxKind . FirstAssignment ,
466
+ updatedIife ) ;
467
+ }
468
+
469
+ return updateHostNode ( hostNode , value ) ;
429
470
}
430
471
431
472
function createWrappedEnum (
@@ -450,5 +491,5 @@ function createWrappedEnum(
450
491
innerReturn ,
451
492
] ) ;
452
493
453
- return updateHostNode ( hostNode , ts . createParen ( iife ) ) ;
494
+ return updateHostNode ( hostNode , addPureComment ( ts . createParen ( iife ) ) ) ;
454
495
}
0 commit comments