Skip to content

Commit

Permalink
feat(cdk/schematics): add migration for removed symbols (#23530)
Browse files Browse the repository at this point in the history
Sets up a new migration that will log a message for symbols that have been removed from a specific module.
  • Loading branch information
crisbeto committed Sep 7, 2021
1 parent c5e3710 commit 04b5a08
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/cdk/schematics/ng-update/data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ export * from './input-names';
export * from './method-call-checks';
export * from './output-names';
export * from './property-names';
export * from './symbol-removal';
22 changes: 22 additions & 0 deletions src/cdk/schematics/ng-update/data/symbol-removal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import {VersionChanges} from '../../update-tool/version-changes';

export interface SymbolRemovalUpgradeData {
/** Module that the symbol was removed from. */
module: string;

/** Name of the symbol being removed. */
name: string;

/** Message to log explaining why the symbol was removed. */
message: string;
}

export const symbolRemoval: VersionChanges<SymbolRemovalUpgradeData> = {};
2 changes: 2 additions & 0 deletions src/cdk/schematics/ng-update/devkit-migration-rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {MiscTemplateMigration} from './migrations/misc-template';
import {OutputNamesMigration} from './migrations/output-names';
import {PropertyNamesMigration} from './migrations/property-names';
import {UpgradeData} from './upgrade-data';
import {SymbolRemovalMigration} from './migrations/symbol-removal';


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

export type NullableDevkitMigration = MigrationCtor<UpgradeData|null, DevkitContext>;
Expand Down
50 changes: 50 additions & 0 deletions src/cdk/schematics/ng-update/migrations/symbol-removal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import * as ts from 'typescript';
import {Migration} from '../../update-tool/migration';
import {SymbolRemovalUpgradeData} from '../data';
import {getVersionUpgradeData, UpgradeData} from '../upgrade-data';

/** Migration that flags imports for symbols that have been removed. */
export class SymbolRemovalMigration extends Migration<UpgradeData> {
/** Change data that upgrades to the specified target version. */
data: SymbolRemovalUpgradeData[] = getVersionUpgradeData(this, 'symbolRemoval');

// Only enable the migration rule if there is upgrade data.
enabled = this.data.length !== 0;

override visitNode(node: ts.Node): void {
if (!ts.isImportDeclaration(node) || !ts.isStringLiteral(node.moduleSpecifier)) {
return;
}

const namedBindings = node.importClause && node.importClause.namedBindings;

if (!namedBindings || !ts.isNamedImports(namedBindings)) {
return;
}

const moduleNameMatches = this.data.filter(entry =>
(node.moduleSpecifier as ts.StringLiteral).text === entry.module);

if (!moduleNameMatches.length) {
return;
}

namedBindings.elements.forEach(element => {
const elementName = element.propertyName?.text || element.name.text;

moduleNameMatches.forEach(match => {
if (match.name === elementName) {
this.createFailureAtNode(element, match.message);
}
});
});
}
}
4 changes: 4 additions & 0 deletions src/cdk/schematics/ng-update/upgrade-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import {
OutputNameUpgradeData,
propertyNames,
PropertyNameUpgradeData,
SymbolRemovalUpgradeData,
symbolRemoval,
} from './data';


Expand All @@ -41,6 +43,7 @@ export const cdkUpgradeData: UpgradeData = {
methodCallChecks,
outputNames,
propertyNames,
symbolRemoval,
};

/**
Expand All @@ -57,6 +60,7 @@ export interface UpgradeData {
methodCallChecks: VersionChanges<MethodCallUpgradeData>;
outputNames: VersionChanges<OutputNameUpgradeData>;
propertyNames: VersionChanges<PropertyNameUpgradeData>;
symbolRemoval: VersionChanges<SymbolRemovalUpgradeData>;
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/material/schematics/ng-update/data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ export * from './input-names';
export * from './method-call-checks';
export * from './output-names';
export * from './property-names';
export * from './symbol-removal';
29 changes: 29 additions & 0 deletions src/material/schematics/ng-update/data/symbol-removal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import {SymbolRemovalUpgradeData, TargetVersion, VersionChanges} from '@angular/cdk/schematics';

export const symbolRemoval: VersionChanges<SymbolRemovalUpgradeData> = {
[TargetVersion.V13]: [
{
pr: 'https://github.com/angular/components/pull/23529',
changes: [
'CanColorCtor',
'CanDisableRippleCtor',
'CanDisableCtor',
'CanUpdateErrorStateCtor',
'HasInitializedCtor',
'HasTabIndexCtor'
].map(name => ({
name,
module: '@angular/material/core',
message: `\`${name}\` is no longer necessary and has been removed.`
}))
}
]
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {createTestCaseSetup, resolveBazelPath} from '@angular/cdk/schematics/testing';
import {MIGRATION_PATH} from '../../../paths';

describe('symbol removal check', () => {
it('should report symbols that have been removed', async () => {
const {runFixers} = await createTestCaseSetup(
'migration-v13', MIGRATION_PATH,
[resolveBazelPath(__dirname, './symbol-removal_input.ts')]);

const {logOutput} = await runFixers();

expect(logOutput)
.not
.withContext('Expected check not to report symbols that have not been removed.')
.toContain('MatRipple');

expect(logOutput)
.not
.withContext('Expected check not to report symbols with the same name as a ' +
'removed symbol, but from a different module.').toContain('HasInitializedCtor');

expect(logOutput)
.withContext('Expected check to report a removed symbol')
.toContain('@2:3 - `CanColorCtor` is no longer necessary and has been removed.');

expect(logOutput)
.withContext('Expected check to report a removed symbol that has been aliased')
.toContain('@3:3 - `CanDisableRippleCtor` is no longer necessary and has been removed.');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {
CanColorCtor,
CanDisableRippleCtor as DisableRippleCtorAlias,
MatRipple,
} from '@angular/material/core';
import {HasInitializedCtor} from '@not-angular/material/core';

export declare const colorCtor: CanColorCtor;
export declare const disableRIppleCtor: DisableRippleCtorAlias;
export declare const ripple: MatRipple;
export declare const initCtor: HasInitializedCtor;
2 changes: 2 additions & 0 deletions src/material/schematics/ng-update/upgrade-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
methodCallChecks,
outputNames,
propertyNames,
symbolRemoval,
} from './data';

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

0 comments on commit 04b5a08

Please sign in to comment.