Skip to content

Commit 17dd9bb

Browse files
llorenspujolmmalerba
authored andcommitted
fix(mdc-migration): migrate components selectively bug
Fixes mdc-migration to migrate only the selected components. Fixes #26426
1 parent 1564880 commit 17dd9bb

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
@@ -65,6 +65,13 @@ export class RuntimeCodeMigration extends Migration<ComponentMigrator[], Schemat
6565
});
6666
}
6767

68+
private _importPathHasComponentToMigrate(importTextPath: string): boolean {
69+
return !!this.upgradeData.find(componentMigrator => {
70+
const lastImportName = importTextPath.split('/').slice(-1)[0];
71+
return lastImportName.includes(componentMigrator.component);
72+
});
73+
}
74+
6875
/** Finds the imported symbols in a file that need to be migrated. */
6976
private _findImportsToMigrate(sourceFile: ts.SourceFile) {
7077
const importSpecifiersToNewNames = new Map<ts.ImportSpecifier, string>();
@@ -77,7 +84,8 @@ export class RuntimeCodeMigration extends Migration<ComponentMigrator[], Schemat
7784
ts.isStringLiteral(statement.moduleSpecifier) &&
7885
statement.importClause?.namedBindings &&
7986
ts.isNamedImports(statement.importClause.namedBindings) &&
80-
LEGACY_MODULES.has(statement.moduleSpecifier.text)
87+
LEGACY_MODULES.has(statement.moduleSpecifier.text) &&
88+
this._importPathHasComponentToMigrate(statement.moduleSpecifier.text)
8189
) {
8290
statement.importClause.namedBindings.elements.forEach(element => {
8391
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)