Skip to content

Commit f04147b

Browse files
committed
fix(material/schematics): migrate components selectively bug
Fixes mdc-migration to migrate only the selected components. Fixes #26426
1 parent 1564880 commit f04147b

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ export class RuntimeCodeMigration extends Migration<ComponentMigrator[], Schemat
6565
});
6666
}
6767

68+
private _importPathHasComponentToMigrate(importTextPath: string): boolean {
69+
const lastImportName = importTextPath.split('/').slice(-1)[0];
70+
return (
71+
lastImportName === 'legacy-core' ||
72+
!!this.upgradeData.find(componentMigrator => {
73+
return lastImportName.includes(componentMigrator.component);
74+
})
75+
);
76+
}
77+
6878
/** Finds the imported symbols in a file that need to be migrated. */
6979
private _findImportsToMigrate(sourceFile: ts.SourceFile) {
7080
const importSpecifiersToNewNames = new Map<ts.ImportSpecifier, string>();
@@ -77,7 +87,8 @@ export class RuntimeCodeMigration extends Migration<ComponentMigrator[], Schemat
7787
ts.isStringLiteral(statement.moduleSpecifier) &&
7888
statement.importClause?.namedBindings &&
7989
ts.isNamedImports(statement.importClause.namedBindings) &&
80-
LEGACY_MODULES.has(statement.moduleSpecifier.text)
90+
LEGACY_MODULES.has(statement.moduleSpecifier.text) &&
91+
this._importPathHasComponentToMigrate(statement.moduleSpecifier.text)
8192
) {
8293
statement.importClause.namedBindings.elements.forEach(element => {
8394
const oldName = (element.propertyName || element.name).text;

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,49 @@ 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+
412+
it('should always replace legacy-core imports even if they are not specified', async () => {
413+
await runMigrationTest(
414+
`
415+
import {NgModule} from '@angular/core';
416+
import {MatLegacyOptionModule as MatOptionModule, LEGACY_VERSION as VERSION} from '@angular/material/legacy-core';
417+
418+
@NgModule({imports: [MatOptionModule]})
419+
export class AppModule {}
420+
`,
421+
`
422+
import {NgModule} from '@angular/core';
423+
import {MatOptionModule, VERSION} from '@angular/material/core';
424+
425+
@NgModule({imports: [MatOptionModule]})
426+
export class AppModule {}
427+
`,
428+
);
429+
});
430+
388431
it('should migrate styles for a component', async () => {
389432
await runMigrationTest(
390433
`

0 commit comments

Comments
 (0)