|
| 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 * as ts from 'typescript'; |
| 10 | +import {Migration} from '../../update-tool/migration'; |
| 11 | +import {SymbolRemovalUpgradeData} from '../data'; |
| 12 | +import {getVersionUpgradeData, UpgradeData} from '../upgrade-data'; |
| 13 | + |
| 14 | +/** Migration that flags imports for symbols that have been removed. */ |
| 15 | +export class SymbolRemovalMigration extends Migration<UpgradeData> { |
| 16 | + /** Change data that upgrades to the specified target version. */ |
| 17 | + data: SymbolRemovalUpgradeData[] = getVersionUpgradeData(this, 'symbolRemoval'); |
| 18 | + |
| 19 | + // Only enable the migration rule if there is upgrade data. |
| 20 | + enabled = this.data.length !== 0; |
| 21 | + |
| 22 | + override visitNode(node: ts.Node): void { |
| 23 | + if (!ts.isImportDeclaration(node) || !ts.isStringLiteral(node.moduleSpecifier)) { |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + const namedBindings = node.importClause && node.importClause.namedBindings; |
| 28 | + |
| 29 | + if (!namedBindings || !ts.isNamedImports(namedBindings)) { |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + const moduleNameMatches = this.data.filter(entry => |
| 34 | + (node.moduleSpecifier as ts.StringLiteral).text === entry.module); |
| 35 | + |
| 36 | + if (!moduleNameMatches.length) { |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + namedBindings.elements.forEach(element => { |
| 41 | + const elementName = element.propertyName?.text || element.name.text; |
| 42 | + |
| 43 | + moduleNameMatches.forEach(match => { |
| 44 | + if (match.name === elementName) { |
| 45 | + this.createFailureAtNode(element, match.message); |
| 46 | + } |
| 47 | + }); |
| 48 | + }); |
| 49 | + } |
| 50 | +} |
0 commit comments