|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {Rule, SchematicsException, Tree} from '@angular-devkit/schematics'; |
| 10 | +import {relative} from 'path'; |
| 11 | +import ts from 'typescript'; |
| 12 | + |
| 13 | +import {extractAngularClassMetadata} from '../../utils/extract_metadata'; |
| 14 | +import {getProjectTsConfigPaths} from '../../utils/project_tsconfig_paths'; |
| 15 | +import {canMigrateFile, createMigrationProgram} from '../../utils/typescript/compiler_host'; |
| 16 | +import {getPropertyNameText} from '../../utils/typescript/property_name'; |
| 17 | + |
| 18 | +export default function(): Rule { |
| 19 | + return async (tree: Tree) => { |
| 20 | + const {buildPaths, testPaths} = await getProjectTsConfigPaths(tree); |
| 21 | + const basePath = process.cwd(); |
| 22 | + const allPaths = [...buildPaths, ...testPaths]; |
| 23 | + |
| 24 | + if (!allPaths.length) { |
| 25 | + throw new SchematicsException( |
| 26 | + 'Could not find any tsconfig file. Cannot run the `RemoveModuleId` migration.'); |
| 27 | + } |
| 28 | + |
| 29 | + for (const tsconfigPath of allPaths) { |
| 30 | + runRemoveModuleIdMigration(tree, tsconfigPath, basePath); |
| 31 | + } |
| 32 | + }; |
| 33 | +} |
| 34 | + |
| 35 | +function runRemoveModuleIdMigration(tree: Tree, tsconfigPath: string, basePath: string) { |
| 36 | + const program = createMigrationProgram(tree, tsconfigPath, basePath); |
| 37 | + const typeChecker = program.getTypeChecker(); |
| 38 | + const sourceFiles = |
| 39 | + program.getSourceFiles().filter(sourceFile => canMigrateFile(basePath, sourceFile, program)); |
| 40 | + |
| 41 | + for (const sourceFile of sourceFiles) { |
| 42 | + const nodesToRemove = collectUpdatesForFile(typeChecker, sourceFile); |
| 43 | + if (nodesToRemove.length !== 0) { |
| 44 | + const update = tree.beginUpdate(relative(basePath, sourceFile.fileName)); |
| 45 | + for (const node of nodesToRemove) { |
| 46 | + update.remove(node.getFullStart(), node.getFullWidth()); |
| 47 | + } |
| 48 | + tree.commitUpdate(update); |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +function collectUpdatesForFile(typeChecker: ts.TypeChecker, file: ts.SourceFile): ts.Node[] { |
| 54 | + const removeNodes: ts.Node[] = []; |
| 55 | + const attemptMigrateClass = (node: ts.ClassDeclaration) => { |
| 56 | + const metadata = extractAngularClassMetadata(typeChecker, node); |
| 57 | + if (metadata === null) { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + const syntaxList = metadata.node.getChildren().find( |
| 62 | + ((n): n is ts.SyntaxList => n.kind === ts.SyntaxKind.SyntaxList)); |
| 63 | + const tokens = syntaxList?.getChildren(); |
| 64 | + |
| 65 | + if (!tokens) { |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + let removeNextComma = false; |
| 70 | + for (const token of tokens) { |
| 71 | + // Track the comma token if it's requested to be removed. |
| 72 | + if (token.kind === ts.SyntaxKind.CommaToken) { |
| 73 | + if (removeNextComma) { |
| 74 | + removeNodes.push(token); |
| 75 | + } |
| 76 | + removeNextComma = false; |
| 77 | + } |
| 78 | + |
| 79 | + // Track the `moduleId` property assignment. Note that the AST node does not include a |
| 80 | + // potential followed comma token. |
| 81 | + if (ts.isPropertyAssignment(token) && getPropertyNameText(token.name) === 'moduleId') { |
| 82 | + removeNodes.push(token); |
| 83 | + removeNextComma = true; |
| 84 | + } |
| 85 | + } |
| 86 | + }; |
| 87 | + |
| 88 | + ts.forEachChild(file, function visitNode(node: ts.Node) { |
| 89 | + if (ts.isClassDeclaration(node)) { |
| 90 | + attemptMigrateClass(node); |
| 91 | + } |
| 92 | + ts.forEachChild(node, visitNode); |
| 93 | + }); |
| 94 | + |
| 95 | + return removeNodes; |
| 96 | +} |
0 commit comments