-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathscrub-file.ts
601 lines (515 loc) · 18.8 KB
/
scrub-file.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import * as ts from 'typescript';
import { collectDeepNodes } from '../helpers/ast-utils';
/**
* @deprecated From 0.9.0
*/
export function testScrubFile(content: string) {
const markers = [
'decorators',
'__decorate',
'propDecorators',
'ctorParameters',
'ɵsetClassMetadata',
'ɵɵsetNgModuleScope',
];
return markers.some((marker) => content.indexOf(marker) !== -1);
}
export function getScrubFileTransformer(program: ts.Program): ts.TransformerFactory<ts.SourceFile> {
return scrubFileTransformer(program.getTypeChecker(), false);
}
export function getScrubFileTransformerForCore(
program: ts.Program,
): ts.TransformerFactory<ts.SourceFile> {
return scrubFileTransformer(program.getTypeChecker(), true);
}
function scrubFileTransformer(checker: ts.TypeChecker, isAngularCoreFile: boolean) {
return (context: ts.TransformationContext): ts.Transformer<ts.SourceFile> => {
const transformer: ts.Transformer<ts.SourceFile> = (sf: ts.SourceFile) => {
const ngMetadata = findAngularMetadata(sf, isAngularCoreFile);
const tslibImports = findTslibImports(sf);
const nodes: ts.Node[] = [];
ts.forEachChild(sf, checkNodeForDecorators);
function checkNodeForDecorators(node: ts.Node): void {
if (node.kind !== ts.SyntaxKind.ExpressionStatement) {
// TS 2.4 nests decorators inside downleveled class IIFEs, so we
// must recurse into them to find the relevant expression statements.
return ts.forEachChild(node, checkNodeForDecorators);
}
const exprStmt = node as ts.ExpressionStatement;
// Do checks that don't need the typechecker first and bail early.
if (isIvyPrivateCallExpression(exprStmt)
|| isCtorParamsAssignmentExpression(exprStmt)) {
nodes.push(node);
} else if (isDecoratorAssignmentExpression(exprStmt)) {
nodes.push(...pickDecorationNodesToRemove(exprStmt, ngMetadata, checker));
} else if (isDecorateAssignmentExpression(exprStmt, tslibImports, checker)
|| isAngularDecoratorExpression(exprStmt, ngMetadata, tslibImports, checker)) {
nodes.push(...pickDecorateNodesToRemove(exprStmt, tslibImports, ngMetadata, checker));
} else if (isPropDecoratorAssignmentExpression(exprStmt)) {
nodes.push(...pickPropDecorationNodesToRemove(exprStmt, ngMetadata, checker));
}
}
const visitor: ts.Visitor = (node: ts.Node): ts.VisitResult<ts.Node> => {
// Check if node is a statement to be dropped.
if (nodes.find((n) => n === node)) {
return undefined;
}
// Otherwise return node as is.
return ts.visitEachChild(node, visitor, context);
};
return ts.visitNode(sf, visitor);
};
return transformer;
};
}
export function expect<T extends ts.Node>(node: ts.Node, kind: ts.SyntaxKind): T {
if (node.kind !== kind) {
throw new Error('Invalid node type.');
}
return node as T;
}
function findAngularMetadata(node: ts.Node, isAngularCoreFile: boolean): ts.Node[] {
let specs: ts.Node[] = [];
// Find all specifiers from imports of `@angular/core`.
ts.forEachChild(node, (child) => {
if (child.kind === ts.SyntaxKind.ImportDeclaration) {
const importDecl = child as ts.ImportDeclaration;
if (isAngularCoreImport(importDecl, isAngularCoreFile)) {
specs.push(...collectDeepNodes<ts.ImportSpecifier>(importDecl, ts.SyntaxKind.ImportSpecifier));
}
}
});
// If the current module is a Angular core file, we also consider all declarations in it to
// potentially be Angular metadata.
if (isAngularCoreFile) {
const localDecl = findAllDeclarations(node);
specs = specs.concat(localDecl);
}
return specs;
}
function findAllDeclarations(node: ts.Node): ts.VariableDeclaration[] {
const nodes: ts.VariableDeclaration[] = [];
ts.forEachChild(node, (child) => {
if (child.kind === ts.SyntaxKind.VariableStatement) {
const vStmt = child as ts.VariableStatement;
vStmt.declarationList.declarations.forEach((decl) => {
if (decl.name.kind !== ts.SyntaxKind.Identifier) {
return;
}
nodes.push(decl);
});
}
});
return nodes;
}
function isAngularCoreImport(node: ts.ImportDeclaration, isAngularCoreFile: boolean): boolean {
if (!(node.moduleSpecifier && ts.isStringLiteral(node.moduleSpecifier))) {
return false;
}
const importText = node.moduleSpecifier.text;
// Imports to `@angular/core` are always core imports.
if (importText === '@angular/core') {
return true;
}
// Relative imports from a Angular core file are also core imports.
if (isAngularCoreFile && (importText.startsWith('./') || importText.startsWith('../'))) {
return true;
}
return false;
}
// Check if assignment is `Clazz.decorators = [...];`.
function isDecoratorAssignmentExpression(exprStmt: ts.ExpressionStatement): boolean {
if (!isAssignmentExpressionTo(exprStmt, 'decorators')) {
return false;
}
const expr = exprStmt.expression as ts.BinaryExpression;
if (expr.right.kind !== ts.SyntaxKind.ArrayLiteralExpression) {
return false;
}
return true;
}
// Check if assignment is `Clazz = __decorate([...], Clazz)`.
function isDecorateAssignmentExpression(
exprStmt: ts.ExpressionStatement,
tslibImports: ts.NamespaceImport[],
checker: ts.TypeChecker,
): boolean {
if (exprStmt.expression.kind !== ts.SyntaxKind.BinaryExpression) {
return false;
}
const expr = exprStmt.expression as ts.BinaryExpression;
if (expr.left.kind !== ts.SyntaxKind.Identifier) {
return false;
}
const classIdent = expr.left as ts.Identifier;
let callExpr: ts.CallExpression;
if (expr.right.kind === ts.SyntaxKind.CallExpression) {
callExpr = expr.right as ts.CallExpression;
} else if (expr.right.kind === ts.SyntaxKind.BinaryExpression) {
// `Clazz = Clazz_1 = __decorate([...], Clazz)` can be found when there are static property
// accesses.
const innerExpr = expr.right as ts.BinaryExpression;
if (innerExpr.left.kind !== ts.SyntaxKind.Identifier
|| innerExpr.right.kind !== ts.SyntaxKind.CallExpression) {
return false;
}
callExpr = innerExpr.right as ts.CallExpression;
} else {
return false;
}
if (!isTslibHelper(callExpr, '__decorate', tslibImports, checker)) {
return false;
}
if (callExpr.arguments.length !== 2) {
return false;
}
if (callExpr.arguments[1].kind !== ts.SyntaxKind.Identifier) {
return false;
}
const classArg = callExpr.arguments[1] as ts.Identifier;
if (classIdent.text !== classArg.text) {
return false;
}
if (callExpr.arguments[0].kind !== ts.SyntaxKind.ArrayLiteralExpression) {
return false;
}
return true;
}
// Check if expression is `__decorate([smt, __metadata("design:type", Object)], ...)`.
function isAngularDecoratorExpression(
exprStmt: ts.ExpressionStatement,
ngMetadata: ts.Node[],
tslibImports: ts.NamespaceImport[],
checker: ts.TypeChecker,
): boolean {
if (exprStmt.expression.kind !== ts.SyntaxKind.CallExpression) {
return false;
}
const callExpr = exprStmt.expression as ts.CallExpression;
if (!isTslibHelper(callExpr, '__decorate', tslibImports, checker)) {
return false;
}
if (callExpr.arguments.length !== 4) {
return false;
}
if (callExpr.arguments[0].kind !== ts.SyntaxKind.ArrayLiteralExpression) {
return false;
}
const decorateArray = callExpr.arguments[0] as ts.ArrayLiteralExpression;
// Check first array entry for Angular decorators.
if (decorateArray.elements.length === 0 || !ts.isCallExpression(decorateArray.elements[0])) {
return false;
}
return decorateArray.elements.some(decoratorCall => {
if (!ts.isCallExpression(decoratorCall) || !ts.isIdentifier(decoratorCall.expression)) {
return false;
}
const decoratorId = decoratorCall.expression;
return identifierIsMetadata(decoratorId, ngMetadata, checker);
});
}
// Check if assignment is `Clazz.propDecorators = [...];`.
function isPropDecoratorAssignmentExpression(exprStmt: ts.ExpressionStatement): boolean {
if (!isAssignmentExpressionTo(exprStmt, 'propDecorators')) {
return false;
}
const expr = exprStmt.expression as ts.BinaryExpression;
if (expr.right.kind !== ts.SyntaxKind.ObjectLiteralExpression) {
return false;
}
return true;
}
// Check if assignment is `Clazz.ctorParameters = [...];`.
function isCtorParamsAssignmentExpression(exprStmt: ts.ExpressionStatement): boolean {
if (!isAssignmentExpressionTo(exprStmt, 'ctorParameters')) {
return false;
}
const expr = exprStmt.expression as ts.BinaryExpression;
if (expr.right.kind !== ts.SyntaxKind.FunctionExpression
&& expr.right.kind !== ts.SyntaxKind.ArrowFunction
) {
return false;
}
return true;
}
function isAssignmentExpressionTo(exprStmt: ts.ExpressionStatement, name: string) {
if (exprStmt.expression.kind !== ts.SyntaxKind.BinaryExpression) {
return false;
}
const expr = exprStmt.expression as ts.BinaryExpression;
if (expr.left.kind !== ts.SyntaxKind.PropertyAccessExpression) {
return false;
}
const propAccess = expr.left as ts.PropertyAccessExpression;
if (propAccess.name.text !== name) {
return false;
}
if (propAccess.expression.kind !== ts.SyntaxKind.Identifier) {
return false;
}
if (expr.operatorToken.kind !== ts.SyntaxKind.FirstAssignment) {
return false;
}
return true;
}
function isIvyPrivateCallExpression(exprStmt: ts.ExpressionStatement) {
const callExpr = exprStmt.expression;
if (!ts.isCallExpression(callExpr)) {
return false;
}
const propAccExpr = callExpr.expression;
if (!ts.isPropertyAccessExpression(propAccExpr)) {
return false;
}
if (propAccExpr.name.text != 'ɵsetClassMetadata'
&& propAccExpr.name.text != 'ɵɵsetNgModuleScope') {
return false;
}
return true;
}
// Remove Angular decorators from`Clazz.decorators = [...];`, or expression itself if all are
// removed.
function pickDecorationNodesToRemove(
exprStmt: ts.ExpressionStatement,
ngMetadata: ts.Node[],
checker: ts.TypeChecker,
): ts.Node[] {
const expr = expect<ts.BinaryExpression>(exprStmt.expression, ts.SyntaxKind.BinaryExpression);
const literal = expect<ts.ArrayLiteralExpression>(expr.right,
ts.SyntaxKind.ArrayLiteralExpression);
if (!literal.elements.every((elem) => elem.kind === ts.SyntaxKind.ObjectLiteralExpression)) {
return [];
}
const elements = literal.elements as ts.NodeArray<ts.ObjectLiteralExpression>;
const ngDecorators = elements.filter((elem) => isAngularDecorator(elem, ngMetadata, checker));
return (elements.length > ngDecorators.length) ? ngDecorators : [exprStmt];
}
// Remove Angular decorators from `Clazz = __decorate([...], Clazz)`, or expression itself if all
// are removed.
function pickDecorateNodesToRemove(
exprStmt: ts.ExpressionStatement,
tslibImports: ts.NamespaceImport[],
ngMetadata: ts.Node[],
checker: ts.TypeChecker,
): ts.Node[] {
let callExpr: ts.CallExpression | undefined;
if (ts.isCallExpression(exprStmt.expression)) {
callExpr = exprStmt.expression;
} else if (ts.isBinaryExpression(exprStmt.expression)) {
const expr = exprStmt.expression;
if (ts.isCallExpression(expr.right)) {
callExpr = expr.right;
} else if (ts.isBinaryExpression(expr.right) && ts.isCallExpression(expr.right.right)) {
callExpr = expr.right.right;
}
}
if (!callExpr) {
return [];
}
const arrLiteral = expect<ts.ArrayLiteralExpression>(callExpr.arguments[0],
ts.SyntaxKind.ArrayLiteralExpression);
if (!arrLiteral.elements.every((elem) => elem.kind === ts.SyntaxKind.CallExpression)) {
return [];
}
const elements = arrLiteral.elements as ts.NodeArray<ts.CallExpression>;
const ngDecoratorCalls = elements.filter((el) => {
if (el.expression.kind !== ts.SyntaxKind.Identifier) {
return false;
}
const id = el.expression as ts.Identifier;
return identifierIsMetadata(id, ngMetadata, checker);
});
// Remove __metadata calls of type 'design:paramtypes'.
const metadataCalls = elements.filter((el) => {
if (!isTslibHelper(el, '__metadata', tslibImports, checker)) {
return false;
}
if (el.arguments.length < 2) {
return false;
}
if (el.arguments[0].kind !== ts.SyntaxKind.StringLiteral) {
return false;
}
return true;
});
// Remove all __param calls.
const paramCalls = elements.filter((el) => {
if (!isTslibHelper(el, '__param', tslibImports, checker)) {
return false;
}
if (el.arguments.length != 2) {
return false;
}
if (el.arguments[0].kind !== ts.SyntaxKind.NumericLiteral) {
return false;
}
return true;
});
ngDecoratorCalls.push(...metadataCalls, ...paramCalls);
// If all decorators are metadata decorators then return the whole `Class = __decorate([...])'`
// statement so that it is removed in entirety
return (elements.length === ngDecoratorCalls.length) ? [exprStmt] : ngDecoratorCalls;
}
// Remove Angular decorators from`Clazz.propDecorators = [...];`, or expression itself if all
// are removed.
function pickPropDecorationNodesToRemove(
exprStmt: ts.ExpressionStatement,
ngMetadata: ts.Node[],
checker: ts.TypeChecker,
): ts.Node[] {
const expr = expect<ts.BinaryExpression>(exprStmt.expression, ts.SyntaxKind.BinaryExpression);
const literal = expect<ts.ObjectLiteralExpression>(expr.right,
ts.SyntaxKind.ObjectLiteralExpression);
if (!literal.properties.every((elem) => elem.kind === ts.SyntaxKind.PropertyAssignment &&
(elem as ts.PropertyAssignment).initializer.kind === ts.SyntaxKind.ArrayLiteralExpression)) {
return [];
}
const assignments = literal.properties as ts.NodeArray<ts.PropertyAssignment>;
// Consider each assignment individually. Either the whole assignment will be removed or
// a particular decorator within will.
const toRemove = assignments
.map((assign) => {
const decorators =
expect<ts.ArrayLiteralExpression>(assign.initializer,
ts.SyntaxKind.ArrayLiteralExpression).elements;
if (!decorators.every((el) => el.kind === ts.SyntaxKind.ObjectLiteralExpression)) {
return [];
}
const decsToRemove = decorators.filter((expression) => {
const lit = expect<ts.ObjectLiteralExpression>(expression,
ts.SyntaxKind.ObjectLiteralExpression);
return isAngularDecorator(lit, ngMetadata, checker);
});
if (decsToRemove.length === decorators.length) {
return [assign];
}
return decsToRemove;
})
.reduce((accum, toRm) => accum.concat(toRm), [] as ts.Node[]);
// If every node to be removed is a property assignment (full property's decorators) and
// all properties are accounted for, remove the whole assignment. Otherwise, remove the
// nodes which were marked as safe.
if (toRemove.length === assignments.length &&
toRemove.every((node) => node.kind === ts.SyntaxKind.PropertyAssignment)) {
return [exprStmt];
}
return toRemove;
}
function isAngularDecorator(
literal: ts.ObjectLiteralExpression,
ngMetadata: ts.Node[],
checker: ts.TypeChecker,
): boolean {
const types = literal.properties.filter(isTypeProperty);
if (types.length !== 1) {
return false;
}
const assign = expect<ts.PropertyAssignment>(types[0], ts.SyntaxKind.PropertyAssignment);
if (assign.initializer.kind !== ts.SyntaxKind.Identifier) {
return false;
}
const id = assign.initializer as ts.Identifier;
const res = identifierIsMetadata(id, ngMetadata, checker);
return res;
}
function isTypeProperty(prop: ts.ObjectLiteralElement): boolean {
if (prop.kind !== ts.SyntaxKind.PropertyAssignment) {
return false;
}
const assignment = prop as ts.PropertyAssignment;
if (assignment.name.kind !== ts.SyntaxKind.Identifier) {
return false;
}
const name = assignment.name as ts.Identifier;
return name.text === 'type';
}
// Check if an identifier is part of the known Angular Metadata.
function identifierIsMetadata(
id: ts.Identifier,
metadata: ts.Node[],
checker: ts.TypeChecker,
): boolean {
const symbol = checker.getSymbolAtLocation(id);
if (!symbol || !symbol.declarations || !symbol.declarations.length) {
return false;
}
return symbol
.declarations
.some((spec) => metadata.indexOf(spec) !== -1);
}
// Check if an import is a tslib helper import (`import * as tslib from "tslib";`)
function isTslibImport(node: ts.ImportDeclaration): boolean {
return !!(node.moduleSpecifier &&
node.moduleSpecifier.kind === ts.SyntaxKind.StringLiteral &&
(node.moduleSpecifier as ts.StringLiteral).text === 'tslib' &&
node.importClause &&
node.importClause.namedBindings &&
node.importClause.namedBindings.kind === ts.SyntaxKind.NamespaceImport);
}
// Find all namespace imports for `tslib`.
function findTslibImports(node: ts.Node): ts.NamespaceImport[] {
const imports: ts.NamespaceImport[] = [];
ts.forEachChild(node, (child) => {
if (child.kind === ts.SyntaxKind.ImportDeclaration) {
const importDecl = child as ts.ImportDeclaration;
if (isTslibImport(importDecl)) {
const importClause = importDecl.importClause as ts.ImportClause;
const namespaceImport = importClause.namedBindings as ts.NamespaceImport;
imports.push(namespaceImport);
}
}
});
return imports;
}
// Check if an identifier is part of the known tslib identifiers.
function identifierIsTslib(
id: ts.Identifier,
tslibImports: ts.NamespaceImport[],
checker: ts.TypeChecker,
): boolean {
const symbol = checker.getSymbolAtLocation(id);
if (!symbol || !symbol.declarations || !symbol.declarations.length) {
return false;
}
return symbol
.declarations
.some((spec) => tslibImports.indexOf(spec as ts.NamespaceImport) !== -1);
}
// Check if a function call is a tslib helper.
function isTslibHelper(
callExpr: ts.CallExpression,
helper: string,
tslibImports: ts.NamespaceImport[],
checker: ts.TypeChecker,
) {
let callExprIdent = callExpr.expression as ts.Identifier;
if (callExpr.expression.kind !== ts.SyntaxKind.Identifier) {
if (callExpr.expression.kind === ts.SyntaxKind.PropertyAccessExpression) {
const propAccess = callExpr.expression as ts.PropertyAccessExpression;
const left = propAccess.expression;
callExprIdent = propAccess.name;
if (left.kind !== ts.SyntaxKind.Identifier) {
return false;
}
const id = left as ts.Identifier;
if (!identifierIsTslib(id, tslibImports, checker)) {
return false;
}
} else {
return false;
}
}
// node.text on a name that starts with two underscores will return three instead.
// Unless it's an expression like tslib.__decorate, in which case it's only 2.
if (callExprIdent.text !== `_${helper}` && callExprIdent.text !== helper) {
return false;
}
return true;
}