|
| 1 | +import * as ts from 'typescript'; |
| 2 | + |
| 3 | +import { collectDeepNodes } from './ast_helpers'; |
| 4 | +import { removeImport } from './remove_import'; |
| 5 | +import { StandardTransform, TransformOperation, RemoveNodeOperation } from './interfaces'; |
| 6 | +import { makeTransform } from './make_transform'; |
| 7 | + |
| 8 | + |
| 9 | +export function removeDecorators( |
| 10 | + getTypeChecker: () => ts.TypeChecker, |
| 11 | + shouldTransform: (fileName: string) => boolean |
| 12 | +): ts.TransformerFactory<ts.SourceFile> { |
| 13 | + |
| 14 | + const standardTransform: StandardTransform = function (sourceFile: ts.SourceFile) { |
| 15 | + const ops: TransformOperation[] = []; |
| 16 | + |
| 17 | + if (!shouldTransform(sourceFile.fileName)) { |
| 18 | + return ops; |
| 19 | + } |
| 20 | + |
| 21 | + collectDeepNodes<ts.Decorator>(sourceFile, ts.SyntaxKind.Decorator) |
| 22 | + .filter((decorator) => shouldRemove(decorator, getTypeChecker())) |
| 23 | + .forEach((decorator) => { |
| 24 | + // Remove the decorator node. |
| 25 | + ops.push(new RemoveNodeOperation(sourceFile, decorator)); |
| 26 | + // Also remove imports for identifiers used in the decorator. |
| 27 | + // TS doesn't automatically elide imports for things removed in transformers so we have |
| 28 | + // to do it manually. |
| 29 | + collectDeepNodes<ts.Identifier>(decorator, ts.SyntaxKind.Identifier) |
| 30 | + .forEach((identifier) => { |
| 31 | + // This is finding a lot of things that might not be imports at all, but we can't |
| 32 | + // use the type checker since previous transforms might have modified things |
| 33 | + // Worst case scenario, some imports will be left over because there was another |
| 34 | + // identifier somewhere in the file as a property access or something with the same |
| 35 | + // name as the identifer we want to remove. |
| 36 | + // TODO: figure out if there's a better way to elide imports. |
| 37 | + ops.push(...removeImport(sourceFile, [identifier])); |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + return ops; |
| 42 | + }; |
| 43 | + |
| 44 | + return makeTransform(standardTransform); |
| 45 | +} |
| 46 | + |
| 47 | +function shouldRemove(decorator: ts.Decorator, typeChecker: ts.TypeChecker): boolean { |
| 48 | + const origin = getDecoratorOrigin(decorator, typeChecker); |
| 49 | + |
| 50 | + return origin && origin.module === '@angular/core'; |
| 51 | +} |
| 52 | + |
| 53 | +// Decorator helpers. |
| 54 | +interface DecoratorOrigin { |
| 55 | + name: string; |
| 56 | + module: string; |
| 57 | +} |
| 58 | + |
| 59 | +function getDecoratorOrigin( |
| 60 | + decorator: ts.Decorator, |
| 61 | + typeChecker: ts.TypeChecker |
| 62 | +): DecoratorOrigin | null { |
| 63 | + if (!ts.isCallExpression(decorator.expression)) { |
| 64 | + return null; |
| 65 | + } |
| 66 | + |
| 67 | + let identifier: ts.Node; |
| 68 | + let name: string; |
| 69 | + if (ts.isPropertyAccessExpression(decorator.expression.expression)) { |
| 70 | + identifier = decorator.expression.expression.expression; |
| 71 | + name = decorator.expression.expression.name.text; |
| 72 | + } else if (ts.isIdentifier(decorator.expression.expression)) { |
| 73 | + identifier = decorator.expression.expression; |
| 74 | + } else { |
| 75 | + return null; |
| 76 | + } |
| 77 | + |
| 78 | + // NOTE: resolver.getReferencedImportDeclaration would work as well but is internal |
| 79 | + const symbol = typeChecker.getSymbolAtLocation(identifier); |
| 80 | + if (symbol && symbol.declarations && symbol.declarations.length > 0) { |
| 81 | + const declaration = symbol.declarations[0]; |
| 82 | + let module: string; |
| 83 | + if (ts.isImportSpecifier(declaration)) { |
| 84 | + name = (declaration.propertyName || declaration.name).text; |
| 85 | + module = (declaration.parent.parent.parent.moduleSpecifier as ts.StringLiteral).text; |
| 86 | + } else if (ts.isNamespaceImport(declaration)) { |
| 87 | + // Use the name from the decorator namespace property access |
| 88 | + module = (declaration.parent.parent.moduleSpecifier as ts.StringLiteral).text; |
| 89 | + } else if (ts.isImportClause(declaration)) { |
| 90 | + name = declaration.name.text; |
| 91 | + module = (declaration.parent.moduleSpecifier as ts.StringLiteral).text; |
| 92 | + } else { |
| 93 | + return null; |
| 94 | + } |
| 95 | + |
| 96 | + return { name, module }; |
| 97 | + } |
| 98 | + |
| 99 | + return null; |
| 100 | +} |
0 commit comments