@@ -10,18 +10,18 @@ import { addPureComment } from '../helpers/ast-utils';
1010
1111function isBlockLike ( node : ts . Node ) : node is ts . BlockLike {
1212 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 ;
1717}
1818
1919export function getWrapEnumsTransformer ( ) : ts . TransformerFactory < ts . SourceFile > {
2020 return ( context : ts . TransformationContext ) : ts . Transformer < ts . SourceFile > => {
2121 const transformer : ts . Transformer < ts . SourceFile > = sf => {
2222 const result = visitBlockStatements ( sf . statements , context ) ;
2323
24- return ts . updateSourceFileNode ( sf , ts . setTextRange ( result , sf . statements ) ) ;
24+ return context . factory . updateSourceFile ( sf , ts . setTextRange ( result , sf . statements ) ) ;
2525 } ;
2626
2727 return transformer ;
@@ -32,9 +32,9 @@ function visitBlockStatements(
3232 statements : ts . NodeArray < ts . Statement > ,
3333 context : ts . TransformationContext ,
3434) : ts . NodeArray < ts . Statement > {
35-
3635 // copy of statements to modify; lazy initialized
3736 let updatedStatements : Array < ts . Statement > | undefined ;
37+ const nodeFactory = context . factory ;
3838
3939 const visitor : ts . Visitor = ( node ) => {
4040 if ( isBlockLike ( node ) ) {
@@ -45,13 +45,13 @@ function visitBlockStatements(
4545 result = ts . setTextRange ( result , node . statements ) ;
4646 switch ( node . kind ) {
4747 case ts . SyntaxKind . Block :
48- return ts . updateBlock ( node , result ) ;
48+ return nodeFactory . updateBlock ( node , result ) ;
4949 case ts . SyntaxKind . ModuleBlock :
50- return ts . updateModuleBlock ( node , result ) ;
50+ return nodeFactory . updateModuleBlock ( node , result ) ;
5151 case ts . SyntaxKind . CaseClause :
52- return ts . updateCaseClause ( node , node . expression , result ) ;
52+ return nodeFactory . updateCaseClause ( node , node . expression , result ) ;
5353 case ts . SyntaxKind . DefaultClause :
54- return ts . updateDefaultClause ( node , result ) ;
54+ return nodeFactory . updateDefaultClause ( node , result ) ;
5555 default :
5656 return node ;
5757 }
@@ -97,6 +97,7 @@ function visitBlockStatements(
9797 // update IIFE and replace variable statement and old IIFE
9898 oldStatementsLength = 2 ;
9999 newStatement = updateEnumIife (
100+ nodeFactory ,
100101 currentStatement ,
101102 iife [ 0 ] ,
102103 iife [ 1 ] ,
@@ -118,6 +119,7 @@ function visitBlockStatements(
118119
119120 oldStatementsLength = classStatements . length ;
120121 newStatement = createWrappedClass (
122+ nodeFactory ,
121123 variableDeclaration ,
122124 classStatements ,
123125 ) ;
@@ -134,11 +136,12 @@ function visitBlockStatements(
134136
135137 oldStatementsLength = classStatements . length ;
136138 newStatement = createWrappedClass (
139+ nodeFactory ,
137140 currentStatement ,
138141 classStatements ,
139142 ) ;
140143
141- oIndex += classStatements . length - 1 ;
144+ oIndex += oldStatementsLength - 1 ;
142145 }
143146
144147 if ( newStatement && newStatement . length > 0 ) {
@@ -163,7 +166,7 @@ function visitBlockStatements(
163166
164167 // if changes, return updated statements
165168 // otherwise, return original array instance
166- return updatedStatements ? ts . createNodeArray ( updatedStatements ) : statements ;
169+ return updatedStatements ? nodeFactory . createNodeArray ( updatedStatements ) : statements ;
167170}
168171
169172// TS 2.3 enums have statements that are inside a IIFE.
@@ -251,20 +254,22 @@ function findEnumIife(
251254}
252255
253256function updateHostNode (
257+ nodeFactory : ts . NodeFactory ,
254258 hostNode : ts . VariableStatement ,
255259 expression : ts . Expression ,
256260) : ts . Statement {
257261 // Update existing host node with the pure comment before the variable declaration initializer.
258262 const variableDeclaration = hostNode . declarationList . declarations [ 0 ] ;
259- const outerVarStmt = ts . updateVariableStatement (
263+ const outerVarStmt = nodeFactory . updateVariableStatement (
260264 hostNode ,
261265 hostNode . modifiers ,
262- ts . updateVariableDeclarationList (
266+ nodeFactory . updateVariableDeclarationList (
263267 hostNode . declarationList ,
264268 [
265- ts . updateVariableDeclaration (
269+ nodeFactory . updateVariableDeclaration (
266270 variableDeclaration ,
267271 variableDeclaration . name ,
272+ variableDeclaration . exclamationToken ,
268273 variableDeclaration . type ,
269274 expression ,
270275 ) ,
@@ -353,46 +358,47 @@ function findStatements(
353358}
354359
355360function updateEnumIife (
361+ nodeFactory : ts . NodeFactory ,
356362 hostNode : ts . VariableStatement ,
357363 iife : ts . CallExpression ,
358364 exportAssignment ?: ts . Expression ,
359365) : ts . Statement [ ] {
360366 if ( ! ts . isParenthesizedExpression ( iife . expression )
361- || ! ts . isFunctionExpression ( iife . expression . expression ) ) {
367+ || ! ts . isFunctionExpression ( iife . expression . expression ) ) {
362368 throw new Error ( 'Invalid IIFE Structure' ) ;
363369 }
364370
365371 // Ignore export assignment if variable is directly exported
366372 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 ) {
368374 exportAssignment = undefined ;
369375 }
370376
371377 const expression = iife . expression . expression ;
372- const updatedFunction = ts . updateFunctionExpression (
378+ const updatedFunction = nodeFactory . updateFunctionExpression (
373379 expression ,
374380 expression . modifiers ,
375381 expression . asteriskToken ,
376382 expression . name ,
377383 expression . typeParameters ,
378384 expression . parameters ,
379385 expression . type ,
380- ts . updateBlock (
386+ nodeFactory . updateBlock (
381387 expression . body ,
382388 [
383389 ...expression . body . statements ,
384- ts . createReturn ( expression . parameters [ 0 ] . name as ts . Identifier ) ,
390+ nodeFactory . createReturnStatement ( expression . parameters [ 0 ] . name as ts . Identifier ) ,
385391 ] ,
386392 ) ,
387393 ) ;
388394
389- let arg : ts . Expression = ts . createObjectLiteral ( ) ;
395+ let arg : ts . Expression = nodeFactory . createObjectLiteralExpression ( ) ;
390396 if ( exportAssignment ) {
391- arg = ts . createBinary ( exportAssignment , ts . SyntaxKind . BarBarToken , arg ) ;
397+ arg = nodeFactory . createBinaryExpression ( exportAssignment , ts . SyntaxKind . BarBarToken , arg ) ;
392398 }
393- const updatedIife = ts . updateCall (
399+ const updatedIife = nodeFactory . updateCallExpression (
394400 iife ,
395- ts . updateParen (
401+ nodeFactory . updateParenthesizedExpression (
396402 iife . expression ,
397403 updatedFunction ,
398404 ) ,
@@ -402,49 +408,17 @@ function updateEnumIife(
402408
403409 let value : ts . Expression = addPureComment ( updatedIife ) ;
404410 if ( exportAssignment ) {
405- value = ts . createBinary (
411+ value = nodeFactory . createBinaryExpression (
406412 exportAssignment ,
407413 ts . SyntaxKind . FirstAssignment ,
408414 updatedIife ) ;
409415 }
410416
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 ) ] ;
445418}
446419
447420function createWrappedClass (
421+ nodeFactory : ts . NodeFactory ,
448422 hostNode : ts . ClassDeclaration | ts . VariableDeclaration ,
449423 statements : ts . Statement [ ] ,
450424) : ts . Statement [ ] {
@@ -453,7 +427,7 @@ function createWrappedClass(
453427 const updatedStatements = [ ...statements ] ;
454428
455429 if ( ts . isClassDeclaration ( hostNode ) ) {
456- updatedStatements [ 0 ] = ts . createClassDeclaration (
430+ updatedStatements [ 0 ] = nodeFactory . createClassDeclaration (
457431 hostNode . decorators ,
458432 undefined ,
459433 hostNode . name ,
@@ -464,9 +438,9 @@ function createWrappedClass(
464438 }
465439
466440 const pureIife = addPureComment (
467- ts . createImmediatelyInvokedArrowFunction ( [
441+ nodeFactory . createImmediatelyInvokedArrowFunction ( [
468442 ...updatedStatements ,
469- ts . createReturn ( ts . createIdentifier ( name ) ) ,
443+ nodeFactory . createReturnStatement ( nodeFactory . createIdentifier ( name ) ) ,
470444 ] ) ,
471445 ) ;
472446
@@ -476,22 +450,22 @@ function createWrappedClass(
476450
477451 const newStatement : ts . Statement [ ] = [ ] ;
478452 newStatement . push (
479- ts . createVariableStatement (
453+ nodeFactory . createVariableStatement (
480454 isDefault ? undefined : modifiers ,
481- ts . createVariableDeclarationList ( [
482- ts . createVariableDeclaration ( name , undefined , pureIife ) ,
455+ nodeFactory . createVariableDeclarationList ( [
456+ nodeFactory . createVariableDeclaration ( name , undefined , undefined , pureIife ) ,
483457 ] ,
484- ts . NodeFlags . Let ,
458+ ts . NodeFlags . Let ,
485459 ) ,
486460 ) ) ;
487461
488462 if ( isDefault ) {
489463 newStatement . push (
490- ts . createExportAssignment (
464+ nodeFactory . createExportAssignment (
491465 undefined ,
492466 undefined ,
493467 false ,
494- ts . createIdentifier ( name ) ,
468+ nodeFactory . createIdentifier ( name ) ,
495469 ) ) ;
496470 }
497471
0 commit comments