@@ -10,18 +10,18 @@ import { addPureComment } from '../helpers/ast-utils';
10
10
11
11
function isBlockLike ( node : ts . Node ) : node is ts . BlockLike {
12
12
return node . kind === ts . SyntaxKind . Block
13
- || node . kind === ts . SyntaxKind . ModuleBlock
14
- || node . kind === ts . SyntaxKind . CaseClause
15
- || node . kind === ts . SyntaxKind . DefaultClause
16
- || node . kind === ts . SyntaxKind . SourceFile ;
13
+ || node . kind === ts . SyntaxKind . ModuleBlock
14
+ || node . kind === ts . SyntaxKind . CaseClause
15
+ || node . kind === ts . SyntaxKind . DefaultClause
16
+ || node . kind === ts . SyntaxKind . SourceFile ;
17
17
}
18
18
19
19
export function getWrapEnumsTransformer ( ) : ts . TransformerFactory < ts . SourceFile > {
20
20
return ( context : ts . TransformationContext ) : ts . Transformer < ts . SourceFile > => {
21
21
const transformer : ts . Transformer < ts . SourceFile > = sf => {
22
22
const result = visitBlockStatements ( sf . statements , context ) ;
23
23
24
- return ts . updateSourceFileNode ( sf , ts . setTextRange ( result , sf . statements ) ) ;
24
+ return context . factory . updateSourceFile ( sf , ts . setTextRange ( result , sf . statements ) ) ;
25
25
} ;
26
26
27
27
return transformer ;
@@ -32,9 +32,9 @@ function visitBlockStatements(
32
32
statements : ts . NodeArray < ts . Statement > ,
33
33
context : ts . TransformationContext ,
34
34
) : ts . NodeArray < ts . Statement > {
35
-
36
35
// copy of statements to modify; lazy initialized
37
36
let updatedStatements : Array < ts . Statement > | undefined ;
37
+ const nodeFactory = context . factory ;
38
38
39
39
const visitor : ts . Visitor = ( node ) => {
40
40
if ( isBlockLike ( node ) ) {
@@ -45,13 +45,13 @@ function visitBlockStatements(
45
45
result = ts . setTextRange ( result , node . statements ) ;
46
46
switch ( node . kind ) {
47
47
case ts . SyntaxKind . Block :
48
- return ts . updateBlock ( node , result ) ;
48
+ return nodeFactory . updateBlock ( node , result ) ;
49
49
case ts . SyntaxKind . ModuleBlock :
50
- return ts . updateModuleBlock ( node , result ) ;
50
+ return nodeFactory . updateModuleBlock ( node , result ) ;
51
51
case ts . SyntaxKind . CaseClause :
52
- return ts . updateCaseClause ( node , node . expression , result ) ;
52
+ return nodeFactory . updateCaseClause ( node , node . expression , result ) ;
53
53
case ts . SyntaxKind . DefaultClause :
54
- return ts . updateDefaultClause ( node , result ) ;
54
+ return nodeFactory . updateDefaultClause ( node , result ) ;
55
55
default :
56
56
return node ;
57
57
}
@@ -97,6 +97,7 @@ function visitBlockStatements(
97
97
// update IIFE and replace variable statement and old IIFE
98
98
oldStatementsLength = 2 ;
99
99
newStatement = updateEnumIife (
100
+ nodeFactory ,
100
101
currentStatement ,
101
102
iife [ 0 ] ,
102
103
iife [ 1 ] ,
@@ -118,6 +119,7 @@ function visitBlockStatements(
118
119
119
120
oldStatementsLength = classStatements . length ;
120
121
newStatement = createWrappedClass (
122
+ nodeFactory ,
121
123
variableDeclaration ,
122
124
classStatements ,
123
125
) ;
@@ -134,11 +136,12 @@ function visitBlockStatements(
134
136
135
137
oldStatementsLength = classStatements . length ;
136
138
newStatement = createWrappedClass (
139
+ nodeFactory ,
137
140
currentStatement ,
138
141
classStatements ,
139
142
) ;
140
143
141
- oIndex += classStatements . length - 1 ;
144
+ oIndex += oldStatementsLength - 1 ;
142
145
}
143
146
144
147
if ( newStatement && newStatement . length > 0 ) {
@@ -163,7 +166,7 @@ function visitBlockStatements(
163
166
164
167
// if changes, return updated statements
165
168
// otherwise, return original array instance
166
- return updatedStatements ? ts . createNodeArray ( updatedStatements ) : statements ;
169
+ return updatedStatements ? nodeFactory . createNodeArray ( updatedStatements ) : statements ;
167
170
}
168
171
169
172
// TS 2.3 enums have statements that are inside a IIFE.
@@ -251,20 +254,22 @@ function findEnumIife(
251
254
}
252
255
253
256
function updateHostNode (
257
+ nodeFactory : ts . NodeFactory ,
254
258
hostNode : ts . VariableStatement ,
255
259
expression : ts . Expression ,
256
260
) : ts . Statement {
257
261
// Update existing host node with the pure comment before the variable declaration initializer.
258
262
const variableDeclaration = hostNode . declarationList . declarations [ 0 ] ;
259
- const outerVarStmt = ts . updateVariableStatement (
263
+ const outerVarStmt = nodeFactory . updateVariableStatement (
260
264
hostNode ,
261
265
hostNode . modifiers ,
262
- ts . updateVariableDeclarationList (
266
+ nodeFactory . updateVariableDeclarationList (
263
267
hostNode . declarationList ,
264
268
[
265
- ts . updateVariableDeclaration (
269
+ nodeFactory . updateVariableDeclaration (
266
270
variableDeclaration ,
267
271
variableDeclaration . name ,
272
+ variableDeclaration . exclamationToken ,
268
273
variableDeclaration . type ,
269
274
expression ,
270
275
) ,
@@ -353,46 +358,47 @@ function findStatements(
353
358
}
354
359
355
360
function updateEnumIife (
361
+ nodeFactory : ts . NodeFactory ,
356
362
hostNode : ts . VariableStatement ,
357
363
iife : ts . CallExpression ,
358
364
exportAssignment ?: ts . Expression ,
359
365
) : ts . Statement [ ] {
360
366
if ( ! ts . isParenthesizedExpression ( iife . expression )
361
- || ! ts . isFunctionExpression ( iife . expression . expression ) ) {
367
+ || ! ts . isFunctionExpression ( iife . expression . expression ) ) {
362
368
throw new Error ( 'Invalid IIFE Structure' ) ;
363
369
}
364
370
365
371
// Ignore export assignment if variable is directly exported
366
372
if ( hostNode . modifiers
367
- && hostNode . modifiers . findIndex ( m => m . kind == ts . SyntaxKind . ExportKeyword ) != - 1 ) {
373
+ && hostNode . modifiers . findIndex ( m => m . kind == ts . SyntaxKind . ExportKeyword ) != - 1 ) {
368
374
exportAssignment = undefined ;
369
375
}
370
376
371
377
const expression = iife . expression . expression ;
372
- const updatedFunction = ts . updateFunctionExpression (
378
+ const updatedFunction = nodeFactory . updateFunctionExpression (
373
379
expression ,
374
380
expression . modifiers ,
375
381
expression . asteriskToken ,
376
382
expression . name ,
377
383
expression . typeParameters ,
378
384
expression . parameters ,
379
385
expression . type ,
380
- ts . updateBlock (
386
+ nodeFactory . updateBlock (
381
387
expression . body ,
382
388
[
383
389
...expression . body . statements ,
384
- ts . createReturn ( expression . parameters [ 0 ] . name as ts . Identifier ) ,
390
+ nodeFactory . createReturnStatement ( expression . parameters [ 0 ] . name as ts . Identifier ) ,
385
391
] ,
386
392
) ,
387
393
) ;
388
394
389
- let arg : ts . Expression = ts . createObjectLiteral ( ) ;
395
+ let arg : ts . Expression = nodeFactory . createObjectLiteralExpression ( ) ;
390
396
if ( exportAssignment ) {
391
- arg = ts . createBinary ( exportAssignment , ts . SyntaxKind . BarBarToken , arg ) ;
397
+ arg = nodeFactory . createBinaryExpression ( exportAssignment , ts . SyntaxKind . BarBarToken , arg ) ;
392
398
}
393
- const updatedIife = ts . updateCall (
399
+ const updatedIife = nodeFactory . updateCallExpression (
394
400
iife ,
395
- ts . updateParen (
401
+ nodeFactory . updateParenthesizedExpression (
396
402
iife . expression ,
397
403
updatedFunction ,
398
404
) ,
@@ -402,49 +408,17 @@ function updateEnumIife(
402
408
403
409
let value : ts . Expression = addPureComment ( updatedIife ) ;
404
410
if ( exportAssignment ) {
405
- value = ts . createBinary (
411
+ value = nodeFactory . createBinaryExpression (
406
412
exportAssignment ,
407
413
ts . SyntaxKind . FirstAssignment ,
408
414
updatedIife ) ;
409
415
}
410
416
411
- return [ updateHostNode ( hostNode , value ) ] ;
412
- }
413
-
414
- function createWrappedEnum (
415
- name : string ,
416
- hostNode : ts . VariableStatement ,
417
- statements : Array < ts . Statement > ,
418
- literalInitializer : ts . ObjectLiteralExpression = ts . createObjectLiteral ( ) ,
419
- addExportModifier = false ,
420
- ) : ts . Statement [ ] {
421
- const node = addExportModifier
422
- ? ts . updateVariableStatement (
423
- hostNode ,
424
- [ ts . createToken ( ts . SyntaxKind . ExportKeyword ) ] ,
425
- hostNode . declarationList ,
426
- )
427
- : hostNode ;
428
-
429
- const innerVarStmt = ts . createVariableStatement (
430
- undefined ,
431
- ts . createVariableDeclarationList ( [
432
- ts . createVariableDeclaration ( name , undefined , literalInitializer ) ,
433
- ] ) ,
434
- ) ;
435
-
436
- const innerReturn = ts . createReturn ( ts . createIdentifier ( name ) ) ;
437
-
438
- const iife = ts . createImmediatelyInvokedFunctionExpression ( [
439
- innerVarStmt ,
440
- ...statements ,
441
- innerReturn ,
442
- ] ) ;
443
-
444
- return [ updateHostNode ( node , addPureComment ( ts . createParen ( iife ) ) ) ] ;
417
+ return [ updateHostNode ( nodeFactory , hostNode , value ) ] ;
445
418
}
446
419
447
420
function createWrappedClass (
421
+ nodeFactory : ts . NodeFactory ,
448
422
hostNode : ts . ClassDeclaration | ts . VariableDeclaration ,
449
423
statements : ts . Statement [ ] ,
450
424
) : ts . Statement [ ] {
@@ -453,7 +427,7 @@ function createWrappedClass(
453
427
const updatedStatements = [ ...statements ] ;
454
428
455
429
if ( ts . isClassDeclaration ( hostNode ) ) {
456
- updatedStatements [ 0 ] = ts . createClassDeclaration (
430
+ updatedStatements [ 0 ] = nodeFactory . createClassDeclaration (
457
431
hostNode . decorators ,
458
432
undefined ,
459
433
hostNode . name ,
@@ -464,9 +438,9 @@ function createWrappedClass(
464
438
}
465
439
466
440
const pureIife = addPureComment (
467
- ts . createImmediatelyInvokedArrowFunction ( [
441
+ nodeFactory . createImmediatelyInvokedArrowFunction ( [
468
442
...updatedStatements ,
469
- ts . createReturn ( ts . createIdentifier ( name ) ) ,
443
+ nodeFactory . createReturnStatement ( nodeFactory . createIdentifier ( name ) ) ,
470
444
] ) ,
471
445
) ;
472
446
@@ -476,22 +450,22 @@ function createWrappedClass(
476
450
477
451
const newStatement : ts . Statement [ ] = [ ] ;
478
452
newStatement . push (
479
- ts . createVariableStatement (
453
+ nodeFactory . createVariableStatement (
480
454
isDefault ? undefined : modifiers ,
481
- ts . createVariableDeclarationList ( [
482
- ts . createVariableDeclaration ( name , undefined , pureIife ) ,
455
+ nodeFactory . createVariableDeclarationList ( [
456
+ nodeFactory . createVariableDeclaration ( name , undefined , undefined , pureIife ) ,
483
457
] ,
484
- ts . NodeFlags . Let ,
458
+ ts . NodeFlags . Let ,
485
459
) ,
486
460
) ) ;
487
461
488
462
if ( isDefault ) {
489
463
newStatement . push (
490
- ts . createExportAssignment (
464
+ nodeFactory . createExportAssignment (
491
465
undefined ,
492
466
undefined ,
493
467
false ,
494
- ts . createIdentifier ( name ) ,
468
+ nodeFactory . createIdentifier ( name ) ,
495
469
) ) ;
496
470
}
497
471
0 commit comments