Skip to content

Commit

Permalink
fix(mdc-migration): migrate components selectively bug
Browse files Browse the repository at this point in the history
Fixes mdc-migration to migrate only the selected components.

Fixes angular#26426
  • Loading branch information
llorenspujol committed Feb 2, 2023
1 parent 1564880 commit 33fc842
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ export class RuntimeCodeMigration extends Migration<ComponentMigrator[], Schemat
});
}

private _importPathHasComponentToMigrate(importTextPath: string): boolean {
const lastImportName = importTextPath.split('/').slice(-1)[0];
return (
lastImportName === 'legacy-core' ||
!!this.upgradeData.find(componentMigrator => {
return lastImportName.includes(componentMigrator.component);
})
);
}

/** Finds the imported symbols in a file that need to be migrated. */
private _findImportsToMigrate(sourceFile: ts.SourceFile) {
const importSpecifiersToNewNames = new Map<ts.ImportSpecifier, string>();
Expand All @@ -77,7 +87,8 @@ export class RuntimeCodeMigration extends Migration<ComponentMigrator[], Schemat
ts.isStringLiteral(statement.moduleSpecifier) &&
statement.importClause?.namedBindings &&
ts.isNamedImports(statement.importClause.namedBindings) &&
LEGACY_MODULES.has(statement.moduleSpecifier.text)
LEGACY_MODULES.has(statement.moduleSpecifier.text) &&
this._importPathHasComponentToMigrate(statement.moduleSpecifier.text)
) {
statement.importClause.namedBindings.elements.forEach(element => {
const oldName = (element.propertyName || element.name).text;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,49 @@ describe('runtime code migration', () => {
);
});

it('should not replace imports from non selected components', async () => {
declareLibrarySymbols('legacy-button', 'export declare class MatLegacyButtonModule {};');
declareLibrarySymbols('legacy-checkbox', 'export declare class MatLegacyCheckboxModule {};');

await runMigrationTest(
`
import {NgModule} from '@angular/core';
import {MatLegacyButtonModule} from '@angular/material/legacy-button';
import {MatLegacyCheckboxModule} from '@angular/material/legacy-checkbox';
@NgModule({imports: [MatLegacyButtonModule, MatLegacyCheckboxModule]})
export class AppModule {}
`,
`
import {NgModule} from '@angular/core';
import {MatButtonModule} from '@angular/material/button';
import {MatLegacyCheckboxModule} from '@angular/material/legacy-checkbox';
@NgModule({imports: [MatButtonModule, MatLegacyCheckboxModule]})
export class AppModule {}
`,
);
});

it('should always replace legacy-core imports even if they are not specified', async () => {
await runMigrationTest(
`
import {NgModule} from '@angular/core';
import {MatLegacyOptionModule as MatOptionModule, LEGACY_VERSION as VERSION} from '@angular/material/legacy-core';
@NgModule({imports: [MatOptionModule]})
export class AppModule {}
`,
`
import {NgModule} from '@angular/core';
import {MatOptionModule, VERSION} from '@angular/material/core';
@NgModule({imports: [MatOptionModule]})
export class AppModule {}
`,
);
});

it('should migrate styles for a component', async () => {
await runMigrationTest(
`
Expand Down

0 comments on commit 33fc842

Please sign in to comment.