Skip to content

Commit 04b5a08

Browse files
authored
feat(cdk/schematics): add migration for removed symbols (#23530)
Sets up a new migration that will log a message for symbols that have been removed from a specific module.
1 parent c5e3710 commit 04b5a08

File tree

10 files changed

+152
-0
lines changed

10 files changed

+152
-0
lines changed

src/cdk/schematics/ng-update/data/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ export * from './input-names';
1515
export * from './method-call-checks';
1616
export * from './output-names';
1717
export * from './property-names';
18+
export * from './symbol-removal';
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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 {VersionChanges} from '../../update-tool/version-changes';
10+
11+
export interface SymbolRemovalUpgradeData {
12+
/** Module that the symbol was removed from. */
13+
module: string;
14+
15+
/** Name of the symbol being removed. */
16+
name: string;
17+
18+
/** Message to log explaining why the symbol was removed. */
19+
message: string;
20+
}
21+
22+
export const symbolRemoval: VersionChanges<SymbolRemovalUpgradeData> = {};

src/cdk/schematics/ng-update/devkit-migration-rule.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {MiscTemplateMigration} from './migrations/misc-template';
3131
import {OutputNamesMigration} from './migrations/output-names';
3232
import {PropertyNamesMigration} from './migrations/property-names';
3333
import {UpgradeData} from './upgrade-data';
34+
import {SymbolRemovalMigration} from './migrations/symbol-removal';
3435

3536

3637
/** List of migrations which run for the CDK update. */
@@ -46,6 +47,7 @@ export const cdkMigrations: MigrationCtor<UpgradeData>[] = [
4647
MiscTemplateMigration,
4748
OutputNamesMigration,
4849
PropertyNamesMigration,
50+
SymbolRemovalMigration,
4951
];
5052

5153
export type NullableDevkitMigration = MigrationCtor<UpgradeData|null, DevkitContext>;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
}

src/cdk/schematics/ng-update/upgrade-data.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import {
2727
OutputNameUpgradeData,
2828
propertyNames,
2929
PropertyNameUpgradeData,
30+
SymbolRemovalUpgradeData,
31+
symbolRemoval,
3032
} from './data';
3133

3234

@@ -41,6 +43,7 @@ export const cdkUpgradeData: UpgradeData = {
4143
methodCallChecks,
4244
outputNames,
4345
propertyNames,
46+
symbolRemoval,
4447
};
4548

4649
/**
@@ -57,6 +60,7 @@ export interface UpgradeData {
5760
methodCallChecks: VersionChanges<MethodCallUpgradeData>;
5861
outputNames: VersionChanges<OutputNameUpgradeData>;
5962
propertyNames: VersionChanges<PropertyNameUpgradeData>;
63+
symbolRemoval: VersionChanges<SymbolRemovalUpgradeData>;
6064
}
6165

6266
/**

src/material/schematics/ng-update/data/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ export * from './input-names';
1515
export * from './method-call-checks';
1616
export * from './output-names';
1717
export * from './property-names';
18+
export * from './symbol-removal';
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 {SymbolRemovalUpgradeData, TargetVersion, VersionChanges} from '@angular/cdk/schematics';
10+
11+
export const symbolRemoval: VersionChanges<SymbolRemovalUpgradeData> = {
12+
[TargetVersion.V13]: [
13+
{
14+
pr: 'https://github.com/angular/components/pull/23529',
15+
changes: [
16+
'CanColorCtor',
17+
'CanDisableRippleCtor',
18+
'CanDisableCtor',
19+
'CanUpdateErrorStateCtor',
20+
'HasInitializedCtor',
21+
'HasTabIndexCtor'
22+
].map(name => ({
23+
name,
24+
module: '@angular/material/core',
25+
message: `\`${name}\` is no longer necessary and has been removed.`
26+
}))
27+
}
28+
]
29+
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import {createTestCaseSetup, resolveBazelPath} from '@angular/cdk/schematics/testing';
2+
import {MIGRATION_PATH} from '../../../paths';
3+
4+
describe('symbol removal check', () => {
5+
it('should report symbols that have been removed', async () => {
6+
const {runFixers} = await createTestCaseSetup(
7+
'migration-v13', MIGRATION_PATH,
8+
[resolveBazelPath(__dirname, './symbol-removal_input.ts')]);
9+
10+
const {logOutput} = await runFixers();
11+
12+
expect(logOutput)
13+
.not
14+
.withContext('Expected check not to report symbols that have not been removed.')
15+
.toContain('MatRipple');
16+
17+
expect(logOutput)
18+
.not
19+
.withContext('Expected check not to report symbols with the same name as a ' +
20+
'removed symbol, but from a different module.').toContain('HasInitializedCtor');
21+
22+
expect(logOutput)
23+
.withContext('Expected check to report a removed symbol')
24+
.toContain('@2:3 - `CanColorCtor` is no longer necessary and has been removed.');
25+
26+
expect(logOutput)
27+
.withContext('Expected check to report a removed symbol that has been aliased')
28+
.toContain('@3:3 - `CanDisableRippleCtor` is no longer necessary and has been removed.');
29+
});
30+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {
2+
CanColorCtor,
3+
CanDisableRippleCtor as DisableRippleCtorAlias,
4+
MatRipple,
5+
} from '@angular/material/core';
6+
import {HasInitializedCtor} from '@not-angular/material/core';
7+
8+
export declare const colorCtor: CanColorCtor;
9+
export declare const disableRIppleCtor: DisableRippleCtorAlias;
10+
export declare const ripple: MatRipple;
11+
export declare const initCtor: HasInitializedCtor;

src/material/schematics/ng-update/upgrade-data.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
methodCallChecks,
1818
outputNames,
1919
propertyNames,
20+
symbolRemoval,
2021
} from './data';
2122

2223
/** Upgrade data that will be used for the Angular Material ng-update schematic. */
@@ -30,4 +31,5 @@ export const materialUpgradeData: UpgradeData = {
3031
methodCallChecks,
3132
outputNames,
3233
propertyNames,
34+
symbolRemoval,
3335
};

0 commit comments

Comments
 (0)