@@ -17,7 +17,7 @@ export function testScrubFile(content: string) {
17
17
'ɵsetClassMetadata' ,
18
18
] ;
19
19
20
- return markers . some ( ( marker ) => content . indexOf ( marker ) !== - 1 ) ;
20
+ return markers . some ( ( marker ) => content . includes ( marker ) ) ;
21
21
}
22
22
23
23
export function getScrubFileTransformer ( program ?: ts . Program ) : ts . TransformerFactory < ts . SourceFile > {
@@ -156,7 +156,7 @@ function isDecoratorAssignmentExpression(exprStmt: ts.ExpressionStatement): bool
156
156
return false ;
157
157
}
158
158
const expr = exprStmt . expression as ts . BinaryExpression ;
159
- if ( expr . right . kind !== ts . SyntaxKind . ArrayLiteralExpression ) {
159
+ if ( ! ts . isArrayLiteralExpression ( expr . right ) ) {
160
160
return false ;
161
161
}
162
162
@@ -169,28 +169,26 @@ function isDecorateAssignmentExpression(
169
169
tslibImports : ts . NamespaceImport [ ] ,
170
170
checker : ts . TypeChecker ,
171
171
) : boolean {
172
-
173
- if ( exprStmt . expression . kind !== ts . SyntaxKind . BinaryExpression ) {
172
+ if ( ! ts . isBinaryExpression ( exprStmt . expression ) ) {
174
173
return false ;
175
174
}
176
- const expr = exprStmt . expression as ts . BinaryExpression ;
177
- if ( expr . left . kind !== ts . SyntaxKind . Identifier ) {
175
+ const expr = exprStmt . expression ;
176
+ if ( ! ts . isIdentifier ( expr . left ) ) {
178
177
return false ;
179
178
}
180
- const classIdent = expr . left as ts . Identifier ;
179
+ const classIdent = expr . left ;
181
180
let callExpr : ts . CallExpression ;
182
181
183
- if ( expr . right . kind === ts . SyntaxKind . CallExpression ) {
184
- callExpr = expr . right as ts . CallExpression ;
185
- } else if ( expr . right . kind === ts . SyntaxKind . BinaryExpression ) {
182
+ if ( ts . isCallExpression ( expr . right ) ) {
183
+ callExpr = expr . right ;
184
+ } else if ( ts . isBinaryExpression ( expr . right ) ) {
186
185
// `Clazz = Clazz_1 = __decorate([...], Clazz)` can be found when there are static property
187
186
// accesses.
188
- const innerExpr = expr . right as ts . BinaryExpression ;
189
- if ( innerExpr . left . kind !== ts . SyntaxKind . Identifier
190
- || innerExpr . right . kind !== ts . SyntaxKind . CallExpression ) {
187
+ const innerExpr = expr . right ;
188
+ if ( ! ts . isIdentifier ( innerExpr . left ) || ! ts . isCallExpression ( innerExpr . right ) ) {
191
189
return false ;
192
190
}
193
- callExpr = innerExpr . right as ts . CallExpression ;
191
+ callExpr = innerExpr . right ;
194
192
} else {
195
193
return false ;
196
194
}
@@ -202,14 +200,16 @@ function isDecorateAssignmentExpression(
202
200
if ( callExpr . arguments . length !== 2 ) {
203
201
return false ;
204
202
}
205
- if ( callExpr . arguments [ 1 ] . kind !== ts . SyntaxKind . Identifier ) {
203
+
204
+ const classArg = callExpr . arguments [ 1 ] ;
205
+ if ( ! ts . isIdentifier ( classArg ) ) {
206
206
return false ;
207
207
}
208
- const classArg = callExpr . arguments [ 1 ] as ts . Identifier ;
208
+
209
209
if ( classIdent . text !== classArg . text ) {
210
210
return false ;
211
211
}
212
- if ( callExpr . arguments [ 0 ] . kind !== ts . SyntaxKind . ArrayLiteralExpression ) {
212
+ if ( ! ts . isArrayLiteralExpression ( callExpr . arguments [ 0 ] ) ) {
213
213
return false ;
214
214
}
215
215
@@ -224,20 +224,20 @@ function isAngularDecoratorExpression(
224
224
checker : ts . TypeChecker ,
225
225
) : boolean {
226
226
227
- if ( exprStmt . expression . kind !== ts . SyntaxKind . CallExpression ) {
227
+ if ( ! ts . isCallExpression ( exprStmt . expression ) ) {
228
228
return false ;
229
229
}
230
- const callExpr = exprStmt . expression as ts . CallExpression ;
230
+ const callExpr = exprStmt . expression ;
231
231
if ( ! isTslibHelper ( callExpr , '__decorate' , tslibImports , checker ) ) {
232
232
return false ;
233
233
}
234
234
if ( callExpr . arguments . length !== 4 ) {
235
235
return false ;
236
236
}
237
- if ( callExpr . arguments [ 0 ] . kind !== ts . SyntaxKind . ArrayLiteralExpression ) {
237
+ const decorateArray = callExpr . arguments [ 0 ] ;
238
+ if ( ! ts . isArrayLiteralExpression ( decorateArray ) ) {
238
239
return false ;
239
240
}
240
- const decorateArray = callExpr . arguments [ 0 ] as ts . ArrayLiteralExpression ;
241
241
// Check first array entry for Angular decorators.
242
242
if ( decorateArray . elements . length === 0 || ! ts . isCallExpression ( decorateArray . elements [ 0 ] ) ) {
243
243
return false ;
@@ -283,18 +283,18 @@ function isCtorParamsAssignmentExpression(exprStmt: ts.ExpressionStatement): boo
283
283
}
284
284
285
285
function isAssignmentExpressionTo ( exprStmt : ts . ExpressionStatement , name : string ) {
286
- if ( exprStmt . expression . kind !== ts . SyntaxKind . BinaryExpression ) {
286
+ if ( ! ts . isBinaryExpression ( exprStmt . expression ) ) {
287
287
return false ;
288
288
}
289
- const expr = exprStmt . expression as ts . BinaryExpression ;
290
- if ( expr . left . kind !== ts . SyntaxKind . PropertyAccessExpression ) {
289
+ const expr = exprStmt . expression ;
290
+ if ( ! ts . isPropertyAccessExpression ( expr . left ) ) {
291
291
return false ;
292
292
}
293
- const propAccess = expr . left as ts . PropertyAccessExpression ;
293
+ const propAccess = expr . left ;
294
294
if ( propAccess . name . text !== name ) {
295
295
return false ;
296
296
}
297
- if ( propAccess . expression . kind !== ts . SyntaxKind . Identifier ) {
297
+ if ( ! ts . isIdentifier ( propAccess . expression ) ) {
298
298
return false ;
299
299
}
300
300
if ( expr . operatorToken . kind !== ts . SyntaxKind . FirstAssignment ) {
@@ -342,7 +342,7 @@ function isIvyPrivateCallExpression(exprStmt: ts.ExpressionStatement) {
342
342
return false ;
343
343
}
344
344
345
- if ( propAccExpr . name . text != 'ɵsetClassMetadata' ) {
345
+ if ( propAccExpr . name . text !== 'ɵsetClassMetadata' ) {
346
346
return false ;
347
347
}
348
348
@@ -396,29 +396,25 @@ function pickDecorateNodesToRemove(
396
396
const arrLiteral = expect < ts . ArrayLiteralExpression > ( callExpr . arguments [ 0 ] ,
397
397
ts . SyntaxKind . ArrayLiteralExpression ) ;
398
398
399
- if ( ! arrLiteral . elements . every ( ( elem ) => elem . kind === ts . SyntaxKind . CallExpression ) ) {
399
+ if ( ! arrLiteral . elements . every ( ( elem ) => ts . isCallExpression ( elem ) ) ) {
400
400
return [ ] ;
401
401
}
402
402
const elements = arrLiteral . elements as ts . NodeArray < ts . CallExpression > ;
403
403
const ngDecoratorCalls = elements . filter ( ( el ) => {
404
- if ( el . expression . kind !== ts . SyntaxKind . Identifier ) {
404
+ if ( ! ts . isIdentifier ( el . expression ) ) {
405
405
return false ;
406
406
}
407
- const id = el . expression as ts . Identifier ;
408
407
409
- return identifierIsMetadata ( id , ngMetadata , checker ) ;
408
+ return identifierIsMetadata ( el . expression , ngMetadata , checker ) ;
410
409
} ) ;
411
410
412
-
413
411
// Remove __metadata calls of type 'design:paramtypes'.
414
412
const metadataCalls = elements . filter ( ( el ) => {
415
413
if ( ! isTslibHelper ( el , '__metadata' , tslibImports , checker ) ) {
416
414
return false ;
417
415
}
418
- if ( el . arguments . length < 2 ) {
419
- return false ;
420
- }
421
- if ( el . arguments [ 0 ] . kind !== ts . SyntaxKind . StringLiteral ) {
416
+
417
+ if ( el . arguments . length < 2 || ! ts . isStringLiteral ( el . arguments [ 0 ] ) ) {
422
418
return false ;
423
419
}
424
420
@@ -429,10 +425,8 @@ function pickDecorateNodesToRemove(
429
425
if ( ! isTslibHelper ( el , '__param' , tslibImports , checker ) ) {
430
426
return false ;
431
427
}
432
- if ( el . arguments . length != 2 ) {
433
- return false ;
434
- }
435
- if ( el . arguments [ 0 ] . kind !== ts . SyntaxKind . NumericLiteral ) {
428
+
429
+ if ( el . arguments . length !== 2 || ! ts . isNumericLiteral ( el . arguments [ 0 ] ) ) {
436
430
return false ;
437
431
}
438
432
@@ -469,7 +463,7 @@ function pickPropDecorationNodesToRemove(
469
463
const decorators =
470
464
expect < ts . ArrayLiteralExpression > ( assign . initializer ,
471
465
ts . SyntaxKind . ArrayLiteralExpression ) . elements ;
472
- if ( ! decorators . every ( ( el ) => el . kind === ts . SyntaxKind . ObjectLiteralExpression ) ) {
466
+ if ( ! decorators . every ( ( el ) => ts . isObjectLiteralExpression ( el ) ) ) {
473
467
return [ ] ;
474
468
}
475
469
const decsToRemove = decorators . filter ( ( expression ) => {
@@ -507,26 +501,25 @@ function isAngularDecorator(
507
501
return false ;
508
502
}
509
503
const assign = expect < ts . PropertyAssignment > ( types [ 0 ] , ts . SyntaxKind . PropertyAssignment ) ;
510
- if ( assign . initializer . kind !== ts . SyntaxKind . Identifier ) {
504
+ if ( ! ts . isIdentifier ( assign . initializer ) ) {
511
505
return false ;
512
506
}
513
- const id = assign . initializer as ts . Identifier ;
507
+ const id = assign . initializer ;
514
508
const res = identifierIsMetadata ( id , ngMetadata , checker ) ;
515
509
516
510
return res ;
517
511
}
518
512
519
513
function isTypeProperty ( prop : ts . ObjectLiteralElement ) : boolean {
520
- if ( prop . kind !== ts . SyntaxKind . PropertyAssignment ) {
514
+ if ( ! ts . isPropertyAssignment ( prop ) ) {
521
515
return false ;
522
516
}
523
- const assignment = prop as ts . PropertyAssignment ;
524
- if ( assignment . name . kind !== ts . SyntaxKind . Identifier ) {
517
+
518
+ if ( ! ts . isIdentifier ( prop . name ) ) {
525
519
return false ;
526
520
}
527
- const name = assignment . name as ts . Identifier ;
528
521
529
- return name . text === 'type' ;
522
+ return prop . name . text === 'type' ;
530
523
}
531
524
532
525
// Check if an identifier is part of the known Angular Metadata.
@@ -542,30 +535,23 @@ function identifierIsMetadata(
542
535
543
536
return symbol
544
537
. declarations
545
- . some ( ( spec ) => metadata . indexOf ( spec ) !== - 1 ) ;
546
- }
547
-
548
- // Check if an import is a tslib helper import (`import * as tslib from "tslib";`)
549
- function isTslibImport ( node : ts . ImportDeclaration ) : boolean {
550
- return ! ! ( node . moduleSpecifier &&
551
- node . moduleSpecifier . kind === ts . SyntaxKind . StringLiteral &&
552
- ( node . moduleSpecifier as ts . StringLiteral ) . text === 'tslib' &&
553
- node . importClause &&
554
- node . importClause . namedBindings &&
555
- node . importClause . namedBindings . kind === ts . SyntaxKind . NamespaceImport ) ;
538
+ . some ( ( spec ) => metadata . includes ( spec ) ) ;
556
539
}
557
540
558
541
// Find all namespace imports for `tslib`.
559
542
function findTslibImports ( node : ts . Node ) : ts . NamespaceImport [ ] {
560
543
const imports : ts . NamespaceImport [ ] = [ ] ;
561
- ts . forEachChild ( node , ( child ) => {
562
- if ( child . kind === ts . SyntaxKind . ImportDeclaration ) {
563
- const importDecl = child as ts . ImportDeclaration ;
564
- if ( isTslibImport ( importDecl ) ) {
565
- const importClause = importDecl . importClause as ts . ImportClause ;
566
- const namespaceImport = importClause . namedBindings as ts . NamespaceImport ;
567
- imports . push ( namespaceImport ) ;
568
- }
544
+
545
+ ts . forEachChild ( node , child => {
546
+ if (
547
+ ts . isImportDeclaration ( child ) &&
548
+ child . moduleSpecifier &&
549
+ ts . isStringLiteral ( child . moduleSpecifier ) &&
550
+ child . moduleSpecifier . text === 'tslib' &&
551
+ child . importClause ?. namedBindings &&
552
+ ts . isNamespaceImport ( child . importClause . namedBindings )
553
+ ) {
554
+ imports . push ( child . importClause . namedBindings ) ;
569
555
}
570
556
} ) ;
571
557
@@ -585,7 +571,7 @@ function identifierIsTslib(
585
571
586
572
return symbol
587
573
. declarations
588
- . some ( ( spec ) => tslibImports . indexOf ( spec as ts . NamespaceImport ) !== - 1 ) ;
574
+ . some ( ( spec ) => tslibImports . includes ( spec as ts . NamespaceImport ) ) ;
589
575
}
590
576
591
577
// Check if a function call is a tslib helper.
@@ -595,7 +581,7 @@ function isTslibHelper(
595
581
tslibImports : ts . NamespaceImport [ ] ,
596
582
checker : ts . TypeChecker ,
597
583
) {
598
- let name ;
584
+ let name : string | undefined ;
599
585
600
586
if ( ts . isIdentifier ( callExpr . expression ) ) {
601
587
name = callExpr . expression . text ;
0 commit comments