-
Notifications
You must be signed in to change notification settings - Fork 12k
/
remove_decorators.ts
98 lines (83 loc) · 3.22 KB
/
remove_decorators.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
/**
* @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 './ast_helpers';
import { RemoveNodeOperation, StandardTransform, TransformOperation } from './interfaces';
import { makeTransform } from './make_transform';
export function removeDecorators(
shouldTransform: (fileName: string) => boolean,
getTypeChecker: () => ts.TypeChecker,
): ts.TransformerFactory<ts.SourceFile> {
const standardTransform: StandardTransform = function (sourceFile: ts.SourceFile) {
const ops: TransformOperation[] = [];
if (!shouldTransform(sourceFile.fileName)) {
return ops;
}
collectDeepNodes<ts.Decorator>(sourceFile, ts.SyntaxKind.Decorator)
.filter((decorator) => shouldRemove(decorator, getTypeChecker()))
.forEach((decorator) => {
// Remove the decorator node.
ops.push(new RemoveNodeOperation(sourceFile, decorator));
});
return ops;
};
return makeTransform(standardTransform, getTypeChecker);
}
function shouldRemove(decorator: ts.Decorator, typeChecker: ts.TypeChecker): boolean {
const origin = getDecoratorOrigin(decorator, typeChecker);
return origin ? origin.module === '@angular/core' : false;
}
// Decorator helpers.
interface DecoratorOrigin {
name: string;
module: string;
}
function getDecoratorOrigin(
decorator: ts.Decorator,
typeChecker: ts.TypeChecker,
): DecoratorOrigin | null {
if (!ts.isCallExpression(decorator.expression)) {
return null;
}
let identifier: ts.Node;
let name: string | undefined = undefined;
if (ts.isPropertyAccessExpression(decorator.expression.expression)) {
identifier = decorator.expression.expression.expression;
name = decorator.expression.expression.name.text;
} else if (ts.isIdentifier(decorator.expression.expression)) {
identifier = decorator.expression.expression;
} else {
return null;
}
// NOTE: resolver.getReferencedImportDeclaration would work as well but is internal
const symbol = typeChecker.getSymbolAtLocation(identifier);
if (symbol && symbol.declarations && symbol.declarations.length > 0) {
const declaration = symbol.declarations[0];
let module: string | undefined;
if (ts.isImportSpecifier(declaration)) {
name = (declaration.propertyName || declaration.name).text;
module = declaration.parent
&& declaration.parent.parent
&& declaration.parent.parent.parent
&& (declaration.parent.parent.parent.moduleSpecifier as ts.StringLiteral).text
|| '';
} else if (ts.isNamespaceImport(declaration)) {
// Use the name from the decorator namespace property access
module = declaration.parent
&& declaration.parent.parent
&& (declaration.parent.parent.moduleSpecifier as ts.StringLiteral).text;
} else if (ts.isImportClause(declaration)) {
name = declaration.name && declaration.name.text;
module = declaration.parent && (declaration.parent.moduleSpecifier as ts.StringLiteral).text;
} else {
return null;
}
return { name: name || '', module: module || '' };
}
return null;
}