@@ -103,13 +103,16 @@ namespace ts {
103
103
preferences : UserPreferences ,
104
104
) : void {
105
105
for ( const sourceFile of program . getSourceFiles ( ) ) {
106
- const newImportFromPath = oldToNew ( sourceFile . fileName ) || sourceFile . fileName ;
106
+ const newFromOld = oldToNew ( sourceFile . fileName ) ;
107
+ const newImportFromPath = newFromOld !== undefined ? newFromOld : sourceFile . fileName ;
107
108
const newImportFromDirectory = getDirectoryPath ( newImportFromPath ) ;
108
109
109
110
const oldFromNew : string | undefined = newToOld ( sourceFile . fileName ) ;
110
111
const oldImportFromPath : string = oldFromNew || sourceFile . fileName ;
111
112
const oldImportFromDirectory = getDirectoryPath ( oldImportFromPath ) ;
112
113
114
+ const importingSourceFileMoved = newFromOld !== undefined || oldFromNew !== undefined ;
115
+
113
116
updateImportsWorker ( sourceFile , changeTracker ,
114
117
referenceText => {
115
118
if ( ! pathIsRelative ( referenceText ) ) return undefined ;
@@ -123,7 +126,10 @@ namespace ts {
123
126
// TODO:GH#18217
124
127
? getSourceFileToImportFromResolved ( resolveModuleName ( importLiteral . text , oldImportFromPath , program . getCompilerOptions ( ) , host as ModuleResolutionHost ) , oldToNew , program )
125
128
: getSourceFileToImport ( importLiteral , sourceFile , program , host , oldToNew ) ;
126
- return toImport === undefined ? undefined : moduleSpecifiers . getModuleSpecifier ( program . getCompilerOptions ( ) , sourceFile , newImportFromPath , toImport , host , preferences ) ;
129
+ // If neither the importing source file nor the imported file moved, do nothing.
130
+ return toImport === undefined || ! toImport . updated && ! importingSourceFileMoved
131
+ ? undefined
132
+ : moduleSpecifiers . getModuleSpecifier ( program . getCompilerOptions ( ) , sourceFile , newImportFromPath , toImport . newFileName , host , preferences ) ;
127
133
} ) ;
128
134
}
129
135
}
@@ -135,12 +141,18 @@ namespace ts {
135
141
return ensurePathIsNonModuleName ( combineNormal ( pathA , pathB ) ) ;
136
142
}
137
143
138
- function getSourceFileToImport ( importLiteral : StringLiteralLike , importingSourceFile : SourceFile , program : Program , host : LanguageServiceHost , oldToNew : PathUpdater ) : string | undefined {
144
+ interface ToImport {
145
+ readonly newFileName : string ;
146
+ /** True if the imported file was renamed. */
147
+ readonly updated : boolean ;
148
+ }
149
+ function getSourceFileToImport ( importLiteral : StringLiteralLike , importingSourceFile : SourceFile , program : Program , host : LanguageServiceHost , oldToNew : PathUpdater ) : ToImport | undefined {
139
150
const symbol = program . getTypeChecker ( ) . getSymbolAtLocation ( importLiteral ) ;
140
151
if ( symbol ) {
141
152
if ( symbol . declarations . some ( d => isAmbientModule ( d ) ) ) return undefined ; // No need to update if it's an ambient module
142
153
const oldFileName = find ( symbol . declarations , isSourceFile ) ! . fileName ;
143
- return oldToNew ( oldFileName ) || oldFileName ;
154
+ const newFileName = oldToNew ( oldFileName ) ;
155
+ return newFileName === undefined ? { newFileName : oldFileName , updated : false } : { newFileName, updated : true } ;
144
156
}
145
157
else {
146
158
const resolved = host . resolveModuleNames
@@ -150,14 +162,15 @@ namespace ts {
150
162
}
151
163
}
152
164
153
- function getSourceFileToImportFromResolved ( resolved : ResolvedModuleWithFailedLookupLocations | undefined , oldToNew : PathUpdater , program : Program ) : string | undefined {
165
+ function getSourceFileToImportFromResolved ( resolved : ResolvedModuleWithFailedLookupLocations | undefined , oldToNew : PathUpdater , program : Program ) : ToImport | undefined {
154
166
return resolved && (
155
167
( resolved . resolvedModule && getIfInProgram ( resolved . resolvedModule . resolvedFileName ) ) || firstDefined ( resolved . failedLookupLocations , getIfInProgram ) ) ;
156
168
157
- function getIfInProgram ( oldLocation : string ) : string | undefined {
169
+ function getIfInProgram ( oldLocation : string ) : ToImport | undefined {
158
170
const newLocation = oldToNew ( oldLocation ) ;
171
+
159
172
return program . getSourceFile ( oldLocation ) || newLocation !== undefined && program . getSourceFile ( newLocation )
160
- ? newLocation || oldLocation
173
+ ? newLocation !== undefined ? { newFileName : newLocation , updated : true } : { newFileName : oldLocation , updated : false }
161
174
: undefined ;
162
175
}
163
176
}
0 commit comments