|
| 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.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {Rule, SchematicsException} from '@angular-devkit/schematics'; |
| 10 | + |
| 11 | +import {getProjectTsConfigPaths} from '../../utils/project_tsconfig_paths'; |
| 12 | +import {DevkitMigrationFilesystem} from '../../utils/tsurge/helpers/angular_devkit/devkit_filesystem'; |
| 13 | +import {groupReplacementsByFile} from '../../utils/tsurge/helpers/group_replacements'; |
| 14 | +import {setFileSystem} from '@angular/compiler-cli/src/ngtsc/file_system'; |
| 15 | +import {ProjectRootRelativePath, TextUpdate} from '../../utils/tsurge'; |
| 16 | +import {synchronouslyCombineUnitData} from '../../utils/tsurge/helpers/combine_units'; |
| 17 | +import {CompilationUnitData, UnusedImportsMigration} from './unused_imports_migration'; |
| 18 | + |
| 19 | +export function migrate(): Rule { |
| 20 | + return async (tree, context) => { |
| 21 | + const {buildPaths, testPaths} = await getProjectTsConfigPaths(tree); |
| 22 | + |
| 23 | + if (!buildPaths.length && !testPaths.length) { |
| 24 | + throw new SchematicsException( |
| 25 | + 'Could not find any tsconfig file. Cannot clean up unused imports.', |
| 26 | + ); |
| 27 | + } |
| 28 | + |
| 29 | + const fs = new DevkitMigrationFilesystem(tree); |
| 30 | + setFileSystem(fs); |
| 31 | + |
| 32 | + const migration = new UnusedImportsMigration(); |
| 33 | + const unitResults: CompilationUnitData[] = []; |
| 34 | + const programInfos = [...buildPaths, ...testPaths].map((tsconfigPath) => { |
| 35 | + context.logger.info(`Preparing analysis for ${tsconfigPath}`); |
| 36 | + |
| 37 | + const baseInfo = migration.createProgram(tsconfigPath, fs); |
| 38 | + const info = migration.prepareProgram(baseInfo); |
| 39 | + |
| 40 | + return {info, tsconfigPath}; |
| 41 | + }); |
| 42 | + |
| 43 | + for (const {info, tsconfigPath} of programInfos) { |
| 44 | + context.logger.info(`Scanning for unused imports using ${tsconfigPath}`); |
| 45 | + unitResults.push(await migration.analyze(info)); |
| 46 | + } |
| 47 | + |
| 48 | + const combined = await synchronouslyCombineUnitData(migration, unitResults); |
| 49 | + if (combined === null) { |
| 50 | + context.logger.error('Schematic failed unexpectedly with no analysis data'); |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + const globalMeta = await migration.globalMeta(combined); |
| 55 | + const replacementsPerFile: Map<ProjectRootRelativePath, TextUpdate[]> = new Map(); |
| 56 | + const {replacements} = await migration.migrate(globalMeta); |
| 57 | + const changesPerFile = groupReplacementsByFile(replacements); |
| 58 | + |
| 59 | + for (const [file, changes] of changesPerFile) { |
| 60 | + if (!replacementsPerFile.has(file)) { |
| 61 | + replacementsPerFile.set(file, changes); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + for (const [file, changes] of replacementsPerFile) { |
| 66 | + const recorder = tree.beginUpdate(file); |
| 67 | + for (const c of changes) { |
| 68 | + recorder |
| 69 | + .remove(c.data.position, c.data.end - c.data.position) |
| 70 | + .insertLeft(c.data.position, c.data.toInsert); |
| 71 | + } |
| 72 | + tree.commitUpdate(recorder); |
| 73 | + } |
| 74 | + |
| 75 | + const { |
| 76 | + counters: {removedImports, changedFiles}, |
| 77 | + } = await migration.stats(globalMeta); |
| 78 | + let statsMessage: string; |
| 79 | + |
| 80 | + if (removedImports === 0) { |
| 81 | + statsMessage = 'Schematic could not find unused imports in the project'; |
| 82 | + } else { |
| 83 | + statsMessage = |
| 84 | + `Removed ${removedImports} import${removedImports !== 1 ? 's' : ''} ` + |
| 85 | + `in ${changedFiles} file${changedFiles !== 1 ? 's' : ''}`; |
| 86 | + } |
| 87 | + |
| 88 | + context.logger.info(''); |
| 89 | + context.logger.info(statsMessage); |
| 90 | + }; |
| 91 | +} |
0 commit comments