@@ -14,6 +14,7 @@ export function testWrapEnums(content: string) {
14
14
// tslint:disable:max-line-length
15
15
/ v a r ( \S + ) = \{ \} ; \r ? \n ( \1\. ( \S + ) = \d + ; \r ? \n ) + \1\[ \1\. ( \S + ) \] = " \4" ; \r ? \n ( \1\[ \1\. ( \S + ) \] = " \S + " ; \r ? \n * ) + / ,
16
16
/ v a r ( \S + ) ; ( \/ \* @ _ _ P U R E _ _ \* \/ ) * \r ? \n \( f u n c t i o n \( \1\) \{ \s + ( \1\[ \1\[ " ( \S + ) " \] = 0 \] = " \4" ; ( \s + \1\[ \1\[ " \S + " \] = \d \] = " \S + " ; ) * \r ? \n ) \} \) \( \1 \| \| \( \1 = \{ \} \) \) ; / ,
17
+ / \/ \* \* @ e n u m \{ \w + \} \* \/ / ,
17
18
// tslint:enable:max-line-length
18
19
] ;
19
20
@@ -122,6 +123,7 @@ function visitBlockStatements(
122
123
name ,
123
124
currentStatement ,
124
125
enumStatements ,
126
+ undefined ,
125
127
) ) ;
126
128
// skip IIFE statement
127
129
oIndex ++ ;
@@ -141,13 +143,34 @@ function visitBlockStatements(
141
143
name ,
142
144
currentStatement ,
143
145
enumStatements ,
146
+ variableDeclaration . initializer ,
147
+ ) ) ;
148
+ // skip enum member declarations
149
+ oIndex += enumStatements . length ;
150
+ continue ;
151
+ }
152
+ } else if ( isObjectLiteralExpression ( variableDeclaration . initializer )
153
+ && variableDeclaration . initializer . properties . length !== 0 ) {
154
+ const literalPropertyCount = variableDeclaration . initializer . properties . length ;
155
+ const nextStatements = statements . slice ( oIndex + 1 ) ;
156
+ const enumStatements = findTsickleEnumStatements ( name , nextStatements ) ;
157
+ if ( enumStatements . length === literalPropertyCount ) {
158
+ // found an enum
159
+ if ( ! updatedStatements ) {
160
+ updatedStatements = statements . slice ( ) ;
161
+ }
162
+ // create wrapper and replace variable statement and enum member statements
163
+ updatedStatements . splice ( uIndex , enumStatements . length + 1 , createWrappedEnum (
164
+ name ,
165
+ currentStatement ,
166
+ enumStatements ,
167
+ variableDeclaration . initializer ,
144
168
) ) ;
145
169
// skip enum member declarations
146
170
oIndex += enumStatements . length ;
147
171
continue ;
148
172
}
149
173
}
150
-
151
174
}
152
175
}
153
176
@@ -285,17 +308,54 @@ function findTs2_2EnumStatements(
285
308
return enumStatements ;
286
309
}
287
310
311
+ // Tsickle enums have a variable statement with indexes, followed by value statements.
312
+ // See https://github.com/angular/devkit/issues/229#issuecomment-338512056 fore more information.
313
+ function findTsickleEnumStatements (
314
+ name : string ,
315
+ statements : ts . Statement [ ] ,
316
+ ) : ts . ExpressionStatement [ ] {
317
+ const enumStatements : ts . ExpressionStatement [ ] = [ ] ;
318
+ // let beforeValueStatements = true;
319
+
320
+ for ( const stmt of statements ) {
321
+ // Ensure all statements are of the expected format and using the right identifer.
322
+ // When we find a statement that isn't part of the enum, return what we collected so far.
323
+ const binExpr = drilldownNodes < ts . BinaryExpression > ( stmt ,
324
+ [
325
+ { prop : null , kind : ts . SyntaxKind . ExpressionStatement } ,
326
+ { prop : 'expression' , kind : ts . SyntaxKind . BinaryExpression } ,
327
+ ] ) ;
328
+
329
+ if ( binExpr === null || binExpr . left . kind !== ts . SyntaxKind . ElementAccessExpression ) {
330
+ return enumStatements ;
331
+ }
332
+
333
+ const exprStmt = stmt as ts . ExpressionStatement ;
334
+ const leftExpr = binExpr . left as ts . ElementAccessExpression ;
335
+
336
+ if ( ! ( leftExpr . expression . kind === ts . SyntaxKind . Identifier
337
+ && ( leftExpr . expression as ts . Identifier ) . text === name ) ) {
338
+ return enumStatements ;
339
+ }
340
+ enumStatements . push ( exprStmt ) ;
341
+ }
342
+
343
+ return enumStatements ;
344
+ }
345
+
288
346
function createWrappedEnum (
289
347
name : string ,
290
348
hostNode : ts . VariableStatement ,
291
349
statements : Array < ts . Statement > ,
350
+ literalInitializer : ts . ObjectLiteralExpression | undefined ,
292
351
) : ts . Statement {
293
352
const pureFunctionComment = '@__PURE__' ;
294
353
354
+ literalInitializer = literalInitializer || ts . createObjectLiteral ( ) ;
295
355
const innerVarStmt = ts . createVariableStatement (
296
356
undefined ,
297
357
ts . createVariableDeclarationList ( [
298
- ts . createVariableDeclaration ( name , undefined , ts . createObjectLiteral ( ) ) ,
358
+ ts . createVariableDeclaration ( name , undefined , literalInitializer ) ,
299
359
] ) ,
300
360
) ;
301
361
0 commit comments