Skip to content

Commit ea7712e

Browse files
committed
fix(mdc-migration): migrate components selectively bug
Fixes mdc-migration to migrate only the selected components. Fixes #26426
1 parent 6391fc1 commit ea7712e

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/material/schematics/ng-generate/mdc-migration/rules/ts-migration/runtime-migration.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ export class RuntimeCodeMigration extends Migration<ComponentMigrator[], Schemat
5454
});
5555
}
5656

57+
private _importPathHasComponentToMigrate(importTextPath: string): boolean {
58+
return !!this.upgradeData.find(componentMigrator => {
59+
const lastImportName = importTextPath.split('/').slice(-1)[0];
60+
return lastImportName.includes(componentMigrator.component);
61+
});
62+
}
63+
5764
/** Finds the imported symbols in a file that need to be migrated. */
5865
private _findImportsToMigrate(sourceFile: ts.SourceFile) {
5966
const importSpecifiersToNewNames = new Map<ts.ImportSpecifier, string>();
@@ -66,7 +73,8 @@ export class RuntimeCodeMigration extends Migration<ComponentMigrator[], Schemat
6673
ts.isStringLiteral(statement.moduleSpecifier) &&
6774
statement.importClause?.namedBindings &&
6875
ts.isNamedImports(statement.importClause.namedBindings) &&
69-
LEGACY_MODULES.has(statement.moduleSpecifier.text)
76+
LEGACY_MODULES.has(statement.moduleSpecifier.text) &&
77+
this._importPathHasComponentToMigrate(statement.moduleSpecifier.text)
7078
) {
7179
statement.importClause.namedBindings.elements.forEach(element => {
7280
const oldName = (element.propertyName || element.name).text;

src/material/schematics/ng-generate/mdc-migration/rules/ts-migration/runtime-migrator.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,30 @@ describe('runtime code migration', () => {
385385
);
386386
});
387387

388+
it('should not replace imports from non selected components', async () => {
389+
declareLibrarySymbols('legacy-button', 'export declare class MatLegacyButtonModule {};');
390+
declareLibrarySymbols('legacy-checkbox', 'export declare class MatLegacyCheckboxModule {};');
391+
392+
await runMigrationTest(
393+
`
394+
import {NgModule} from '@angular/core';
395+
import {MatLegacyButtonModule} from '@angular/material/legacy-button';
396+
import {MatLegacyCheckboxModule} from '@angular/material/legacy-checkbox';
397+
398+
@NgModule({imports: [MatLegacyButtonModule, MatLegacyCheckboxModule]})
399+
export class AppModule {}
400+
`,
401+
`
402+
import {NgModule} from '@angular/core';
403+
import {MatButtonModule} from '@angular/material/button';
404+
import {MatLegacyCheckboxModule} from '@angular/material/legacy-checkbox';
405+
406+
@NgModule({imports: [MatButtonModule, MatLegacyCheckboxModule]})
407+
export class AppModule {}
408+
`,
409+
);
410+
});
411+
388412
it('should migrate styles for a component', async () => {
389413
await runMigrationTest(
390414
`

0 commit comments

Comments
 (0)