Skip to content

Commit 5ab25d0

Browse files
authored
fix(material/schematics): make ts import replacements more specific (#25504)
* fix(material/schematics): make ts import replacements more specific * fixup! fix(material/schematics): make ts import replacements more specific * fixup! fix(material/schematics): make ts import replacements more specific
1 parent fd5afe7 commit 5ab25d0

File tree

3 files changed

+50
-23
lines changed

3 files changed

+50
-23
lines changed

src/material/schematics/ng-update/migrations/legacy-components-v15/constants.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
export const COMPONENTS = [
1010
'autocomplete',
1111
'button',
12+
'core',
1213
'card',
1314
'checkbox',
1415
'chips',
1516
'dialog',
1617
'form-field',
1718
'input',
19+
'list',
1820
'menu',
19-
'option',
20-
'optgroup',
2121
'paginator',
2222
'progress-bar',
2323
'progress-spinner',
@@ -31,19 +31,19 @@ export const COMPONENTS = [
3131
'tooltip',
3232
];
3333

34-
export const MIXINS = COMPONENTS.flatMap(component => [
34+
export const MAT_IMPORT_CHANGES = COMPONENTS.map(component => ({
35+
old: `@angular/material/${component}`,
36+
new: `@angular/material/legacy-${component}`,
37+
}));
38+
39+
export const MDC_IMPORT_CHANGES = COMPONENTS.map(component => ({
40+
old: `@angular/material-experimental/mdc-${component}`,
41+
new: `@angular/material/${component}`,
42+
}));
43+
44+
export const MIXINS = COMPONENTS.concat(['option', 'optgroup']).flatMap(component => [
3545
`${component}-theme`,
3646
`${component}-color`,
3747
`${component}-density`,
3848
`${component}-typography`,
3949
]);
40-
41-
export const MAT_IMPORT_CHANGE = {
42-
old: '@angular/material/',
43-
new: '@angular/material/legacy-',
44-
};
45-
46-
export const MAT_MDC_IMPORT_CHANGE = {
47-
old: '@angular/material-experimental/mdc-',
48-
new: '@angular/material/',
49-
};

src/material/schematics/ng-update/migrations/legacy-components-v15/index.ts

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import * as ts from 'typescript';
1010
import * as postcss from 'postcss';
1111
import * as scss from 'postcss-scss';
1212

13-
import {MAT_IMPORT_CHANGE, MAT_MDC_IMPORT_CHANGE, MIXINS} from './constants';
13+
import {MAT_IMPORT_CHANGES, MDC_IMPORT_CHANGES, MIXINS} from './constants';
1414

1515
import {Migration, ResolvedResource, TargetVersion, WorkspacePath} from '@angular/cdk/schematics';
1616

@@ -109,16 +109,19 @@ export class LegacyComponentsMigration extends Migration<null> {
109109
*/
110110
private _handleImportDeclaration(node: ts.ImportDeclaration): void {
111111
const moduleSpecifier = node.moduleSpecifier as ts.StringLiteral;
112-
if (moduleSpecifier.text.startsWith(MAT_IMPORT_CHANGE.old)) {
113-
this._tsReplaceAt(node, MAT_IMPORT_CHANGE);
112+
113+
const matImportChange = this._findMatImportChange(moduleSpecifier);
114+
if (matImportChange) {
115+
this._tsReplaceAt(node, matImportChange);
114116

115117
if (node.importClause?.namedBindings && ts.isNamedImports(node.importClause.namedBindings)) {
116118
this._handleNamedImportBindings(node.importClause.namedBindings);
117119
}
118120
}
119121

120-
if (moduleSpecifier.text.startsWith(MAT_MDC_IMPORT_CHANGE.old)) {
121-
this._tsReplaceAt(node, MAT_MDC_IMPORT_CHANGE);
122+
const mdcImportChange = this._findMdcImportChange(moduleSpecifier);
123+
if (mdcImportChange) {
124+
this._tsReplaceAt(node, mdcImportChange);
122125
}
123126
}
124127

@@ -128,12 +131,16 @@ export class LegacyComponentsMigration extends Migration<null> {
128131
*/
129132
private _handleImportExpression(node: ts.CallExpression): void {
130133
const moduleSpecifier = node.arguments[0] as ts.StringLiteral;
131-
if (moduleSpecifier.text.startsWith(MAT_IMPORT_CHANGE.old)) {
132-
this._tsReplaceAt(node, MAT_IMPORT_CHANGE);
134+
135+
const matImportChange = this._findMatImportChange(moduleSpecifier);
136+
if (matImportChange) {
137+
this._tsReplaceAt(node, matImportChange);
138+
return;
133139
}
134140

135-
if (moduleSpecifier.text.startsWith(MAT_MDC_IMPORT_CHANGE.old)) {
136-
this._tsReplaceAt(node, MAT_MDC_IMPORT_CHANGE);
141+
const mdcImportChange = this._findMdcImportChange(moduleSpecifier);
142+
if (mdcImportChange) {
143+
this._tsReplaceAt(node, mdcImportChange);
137144
}
138145
}
139146

@@ -167,7 +174,8 @@ export class LegacyComponentsMigration extends Migration<null> {
167174
!!node.initializer &&
168175
ts.isAwaitExpression(node.initializer) &&
169176
this._isImportCallExpression(node.initializer.expression) &&
170-
node.initializer.expression.arguments[0].text.startsWith(MAT_IMPORT_CHANGE.old) &&
177+
ts.isStringLiteral(node.initializer.expression.arguments[0]) &&
178+
!!this._findMatImportChange(node.initializer.expression.arguments[0]) &&
171179
ts.isObjectBindingPattern(node.name)
172180
);
173181
}
@@ -184,6 +192,18 @@ export class LegacyComponentsMigration extends Migration<null> {
184192
);
185193
}
186194

195+
private _findMatImportChange(
196+
moduleSpecifier: ts.StringLiteral,
197+
): {old: string; new: string} | undefined {
198+
return MAT_IMPORT_CHANGES.find(change => change.old === moduleSpecifier.text);
199+
}
200+
201+
private _findMdcImportChange(
202+
moduleSpecifier: ts.StringLiteral,
203+
): {old: string; new: string} | undefined {
204+
return MDC_IMPORT_CHANGES.find(change => change.old === moduleSpecifier.text);
205+
}
206+
187207
/** Updates the source file of the given ts node with the given replacements. */
188208
private _tsReplaceAt(node: ts.Node, str: {old: string; new: string}): void {
189209
const filePath = this.fileSystem.resolve(node.getSourceFile().fileName);

src/material/schematics/ng-update/test-cases/v15/legacy-components-v15.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ describe('v15 legacy components migration', () => {
8181
new: `import('@angular/material/legacy-button').then(() => {});`,
8282
});
8383
});
84+
85+
it('does not update non-legacy imports', async () => {
86+
await runTypeScriptMigrationTest('named binding', {
87+
old: `import {MatButtonToggleModule} from '@angular/material/button-toggle';`,
88+
new: `import {MatButtonToggleModule} from '@angular/material/button-toggle';`,
89+
});
90+
});
8491
});
8592

8693
describe('material-experimental --> material', () => {

0 commit comments

Comments
 (0)