@@ -10,7 +10,7 @@ import * as ts from 'typescript';
1010import * as postcss from 'postcss' ;
1111import * 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
1515import { 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 ) ;
0 commit comments