Skip to content

fix(material/schematics): make ts import replacements more specific #25504

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
export const COMPONENTS = [
'autocomplete',
'button',
'core',
'card',
'checkbox',
'chips',
'dialog',
'form-field',
'input',
'list',
'menu',
'option',
'optgroup',
'paginator',
'progress-bar',
'progress-spinner',
Expand All @@ -31,19 +31,19 @@ export const COMPONENTS = [
'tooltip',
];

export const MIXINS = COMPONENTS.flatMap(component => [
export const MAT_IMPORT_CHANGES = COMPONENTS.map(component => ({
old: `@angular/material/${component}`,
new: `@angular/material/legacy-${component}`,
}));

export const MDC_IMPORT_CHANGES = COMPONENTS.map(component => ({
old: `@angular/material-experimental/mdc-${component}`,
new: `@angular/material/${component}`,
}));

export const MIXINS = COMPONENTS.concat(['option', 'optgroup']).flatMap(component => [
`${component}-theme`,
`${component}-color`,
`${component}-density`,
`${component}-typography`,
]);

export const MAT_IMPORT_CHANGE = {
old: '@angular/material/',
new: '@angular/material/legacy-',
};

export const MAT_MDC_IMPORT_CHANGE = {
old: '@angular/material-experimental/mdc-',
new: '@angular/material/',
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import * as ts from 'typescript';
import * as postcss from 'postcss';
import * as scss from 'postcss-scss';

import {MAT_IMPORT_CHANGE, MAT_MDC_IMPORT_CHANGE, MIXINS} from './constants';
import {MAT_IMPORT_CHANGES, MDC_IMPORT_CHANGES, MIXINS} from './constants';

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

Expand Down Expand Up @@ -109,16 +109,19 @@ export class LegacyComponentsMigration extends Migration<null> {
*/
private _handleImportDeclaration(node: ts.ImportDeclaration): void {
const moduleSpecifier = node.moduleSpecifier as ts.StringLiteral;
if (moduleSpecifier.text.startsWith(MAT_IMPORT_CHANGE.old)) {
this._tsReplaceAt(node, MAT_IMPORT_CHANGE);

const matImportChange = this._findMatImportChange(moduleSpecifier);
if (matImportChange) {
this._tsReplaceAt(node, matImportChange);

if (node.importClause?.namedBindings && ts.isNamedImports(node.importClause.namedBindings)) {
this._handleNamedImportBindings(node.importClause.namedBindings);
}
}

if (moduleSpecifier.text.startsWith(MAT_MDC_IMPORT_CHANGE.old)) {
this._tsReplaceAt(node, MAT_MDC_IMPORT_CHANGE);
const mdcImportChange = this._findMdcImportChange(moduleSpecifier);
if (mdcImportChange) {
this._tsReplaceAt(node, mdcImportChange);
}
}

Expand All @@ -128,12 +131,16 @@ export class LegacyComponentsMigration extends Migration<null> {
*/
private _handleImportExpression(node: ts.CallExpression): void {
const moduleSpecifier = node.arguments[0] as ts.StringLiteral;
if (moduleSpecifier.text.startsWith(MAT_IMPORT_CHANGE.old)) {
this._tsReplaceAt(node, MAT_IMPORT_CHANGE);

const matImportChange = this._findMatImportChange(moduleSpecifier);
if (matImportChange) {
this._tsReplaceAt(node, matImportChange);
return;
}

if (moduleSpecifier.text.startsWith(MAT_MDC_IMPORT_CHANGE.old)) {
this._tsReplaceAt(node, MAT_MDC_IMPORT_CHANGE);
const mdcImportChange = this._findMdcImportChange(moduleSpecifier);
if (mdcImportChange) {
this._tsReplaceAt(node, mdcImportChange);
}
}

Expand Down Expand Up @@ -167,7 +174,8 @@ export class LegacyComponentsMigration extends Migration<null> {
!!node.initializer &&
ts.isAwaitExpression(node.initializer) &&
this._isImportCallExpression(node.initializer.expression) &&
node.initializer.expression.arguments[0].text.startsWith(MAT_IMPORT_CHANGE.old) &&
ts.isStringLiteral(node.initializer.expression.arguments[0]) &&
!!this._findMatImportChange(node.initializer.expression.arguments[0]) &&
ts.isObjectBindingPattern(node.name)
);
}
Expand All @@ -184,6 +192,18 @@ export class LegacyComponentsMigration extends Migration<null> {
);
}

private _findMatImportChange(
moduleSpecifier: ts.StringLiteral,
): {old: string; new: string} | undefined {
return MAT_IMPORT_CHANGES.find(change => change.old === moduleSpecifier.text);
}

private _findMdcImportChange(
moduleSpecifier: ts.StringLiteral,
): {old: string; new: string} | undefined {
return MDC_IMPORT_CHANGES.find(change => change.old === moduleSpecifier.text);
}

/** Updates the source file of the given ts node with the given replacements. */
private _tsReplaceAt(node: ts.Node, str: {old: string; new: string}): void {
const filePath = this.fileSystem.resolve(node.getSourceFile().fileName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ describe('v15 legacy components migration', () => {
new: `import('@angular/material/legacy-button').then(() => {});`,
});
});

it('does not update non-legacy imports', async () => {
await runTypeScriptMigrationTest('named binding', {
old: `import {MatButtonToggleModule} from '@angular/material/button-toggle';`,
new: `import {MatButtonToggleModule} from '@angular/material/button-toggle';`,
});
});
});

describe('material-experimental --> material', () => {
Expand Down