@@ -153,6 +153,8 @@ namespace ts.refactor {
153
153
if ( sourceFile === oldFile ) continue ;
154
154
for ( const statement of sourceFile . statements ) {
155
155
forEachImportInStatement ( statement , importNode => {
156
+ if ( checker . getSymbolAtLocation ( moduleSpecifierFromImport ( importNode ) ) !== oldFile . symbol ) return ;
157
+
156
158
const shouldMove = ( name : Identifier ) : boolean => {
157
159
const symbol = isBindingElement ( name . parent )
158
160
? getPropertySymbolFromBindingElement ( checker , name . parent as BindingElement & { name : Identifier } )
@@ -163,11 +165,76 @@ namespace ts.refactor {
163
165
const newModuleSpecifier = combinePaths ( getDirectoryPath ( moduleSpecifierFromImport ( importNode ) . text ) , newModuleName ) ;
164
166
const newImportDeclaration = filterImport ( importNode , createLiteral ( newModuleSpecifier ) , shouldMove ) ;
165
167
if ( newImportDeclaration ) changes . insertNodeAfter ( sourceFile , statement , newImportDeclaration ) ;
168
+
169
+ const ns = getNamespaceLikeImport ( importNode ) ;
170
+ if ( ns ) updateNamespaceLikeImport ( changes , sourceFile , checker , movedSymbols , newModuleName , newModuleSpecifier , ns , importNode ) ;
166
171
} ) ;
167
172
}
168
173
}
169
174
}
170
175
176
+ function getNamespaceLikeImport ( node : SupportedImport ) : Identifier | undefined {
177
+ switch ( node . kind ) {
178
+ case SyntaxKind . ImportDeclaration :
179
+ return node . importClause && node . importClause . namedBindings && node . importClause . namedBindings . kind === SyntaxKind . NamespaceImport ?
180
+ node . importClause . namedBindings . name : undefined ;
181
+ case SyntaxKind . ImportEqualsDeclaration :
182
+ return node . name ;
183
+ case SyntaxKind . VariableDeclaration :
184
+ return tryCast ( node . name , isIdentifier ) ;
185
+ default :
186
+ return Debug . assertNever ( node ) ;
187
+ }
188
+ }
189
+
190
+ function updateNamespaceLikeImport (
191
+ changes : textChanges . ChangeTracker ,
192
+ sourceFile : SourceFile ,
193
+ checker : TypeChecker ,
194
+ movedSymbols : ReadonlySymbolSet ,
195
+ newModuleName : string ,
196
+ newModuleSpecifier : string ,
197
+ oldImportId : Identifier ,
198
+ oldImportNode : SupportedImport ,
199
+ ) : void {
200
+ const preferredNewNamespaceName = codefix . moduleSpecifierToValidIdentifier ( newModuleName , ScriptTarget . ESNext ) ;
201
+ let needUniqueName = false ;
202
+ const toChange : Identifier [ ] = [ ] ;
203
+ FindAllReferences . Core . eachSymbolReferenceInFile ( oldImportId , checker , sourceFile , ref => {
204
+ if ( ! isPropertyAccessExpression ( ref . parent ) ) return ;
205
+ needUniqueName = needUniqueName || ! ! checker . resolveName ( preferredNewNamespaceName , ref , SymbolFlags . All , /*excludeGlobals*/ true ) ;
206
+ if ( movedSymbols . has ( checker . getSymbolAtLocation ( ref . parent . name ) ! ) ) {
207
+ toChange . push ( ref ) ;
208
+ }
209
+ } ) ;
210
+
211
+ if ( toChange . length ) {
212
+ const newNamespaceName = needUniqueName ? getUniqueName ( preferredNewNamespaceName , sourceFile ) : preferredNewNamespaceName ;
213
+ for ( const ref of toChange ) {
214
+ changes . replaceNode ( sourceFile , ref , createIdentifier ( newNamespaceName ) ) ;
215
+ }
216
+ changes . insertNodeAfter ( sourceFile , oldImportNode , updateNamespaceLikeImportNode ( oldImportNode , newModuleName , newModuleSpecifier ) ) ;
217
+ }
218
+ }
219
+
220
+ function updateNamespaceLikeImportNode ( node : SupportedImport , newNamespaceName : string , newModuleSpecifier : string ) : Node {
221
+ const newNamespaceId = createIdentifier ( newNamespaceName ) ;
222
+ const newModuleString = createLiteral ( newModuleSpecifier ) ;
223
+ switch ( node . kind ) {
224
+ case SyntaxKind . ImportDeclaration :
225
+ return createImportDeclaration (
226
+ /*decorators*/ undefined , /*modifiers*/ undefined ,
227
+ createImportClause ( /*name*/ undefined , createNamespaceImport ( newNamespaceId ) ) ,
228
+ newModuleString ) ;
229
+ case SyntaxKind . ImportEqualsDeclaration :
230
+ return createImportEqualsDeclaration ( /*decorators*/ undefined , /*modifiers*/ undefined , newNamespaceId , createExternalModuleReference ( newModuleString ) ) ;
231
+ case SyntaxKind . VariableDeclaration :
232
+ return createVariableDeclaration ( newNamespaceId , /*type*/ undefined , createRequireCall ( newModuleString ) ) ;
233
+ default :
234
+ return Debug . assertNever ( node ) ;
235
+ }
236
+ }
237
+
171
238
function moduleSpecifierFromImport ( i : SupportedImport ) : StringLiteralLike {
172
239
return ( i . kind === SyntaxKind . ImportDeclaration ? i . moduleSpecifier
173
240
: i . kind === SyntaxKind . ImportEqualsDeclaration ? i . moduleReference . expression
0 commit comments