mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-12 01:48:33 -05:00
Avoid rewriting bare module specifiers on rename when fix is not verifiably correct (#41959)
This commit is contained in:
@@ -151,7 +151,7 @@ namespace ts {
|
||||
const toImport = oldFromNew !== undefined
|
||||
// If we're at the new location (file was already renamed), need to redo module resolution starting from the old location.
|
||||
// TODO:GH#18217
|
||||
? getSourceFileToImportFromResolved(resolveModuleName(importLiteral.text, oldImportFromPath, program.getCompilerOptions(), host as ModuleResolutionHost),
|
||||
? getSourceFileToImportFromResolved(importLiteral, resolveModuleName(importLiteral.text, oldImportFromPath, program.getCompilerOptions(), host as ModuleResolutionHost),
|
||||
oldToNew, allFiles)
|
||||
: getSourceFileToImport(importedModuleSymbol, importLiteral, sourceFile, program, host, oldToNew);
|
||||
|
||||
@@ -193,11 +193,11 @@ namespace ts {
|
||||
const resolved = host.resolveModuleNames
|
||||
? host.getResolvedModuleWithFailedLookupLocationsFromCache && host.getResolvedModuleWithFailedLookupLocationsFromCache(importLiteral.text, importingSourceFile.fileName)
|
||||
: program.getResolvedModuleWithFailedLookupLocationsFromCache(importLiteral.text, importingSourceFile.fileName);
|
||||
return getSourceFileToImportFromResolved(resolved, oldToNew, program.getSourceFiles());
|
||||
return getSourceFileToImportFromResolved(importLiteral, resolved, oldToNew, program.getSourceFiles());
|
||||
}
|
||||
}
|
||||
|
||||
function getSourceFileToImportFromResolved(resolved: ResolvedModuleWithFailedLookupLocations | undefined, oldToNew: PathUpdater, sourceFiles: readonly SourceFile[]): ToImport | undefined {
|
||||
function getSourceFileToImportFromResolved(importLiteral: StringLiteralLike, resolved: ResolvedModuleWithFailedLookupLocations | undefined, oldToNew: PathUpdater, sourceFiles: readonly SourceFile[]): ToImport | undefined {
|
||||
// Search through all locations looking for a moved file, and only then test already existing files.
|
||||
// This is because if `a.ts` is compiled to `a.js` and `a.ts` is moved, we don't want to resolve anything to `a.js`, but to `a.ts`'s new location.
|
||||
if (!resolved) return undefined;
|
||||
@@ -210,8 +210,9 @@ namespace ts {
|
||||
|
||||
// Then failed lookups that are in the list of sources
|
||||
const result = forEach(resolved.failedLookupLocations, tryChangeWithIgnoringPackageJsonExisting)
|
||||
// Then failed lookups except package.json since we dont want to touch them (only included ts/js files)
|
||||
|| forEach(resolved.failedLookupLocations, tryChangeWithIgnoringPackageJson);
|
||||
// Then failed lookups except package.json since we dont want to touch them (only included ts/js files).
|
||||
// At this point, the confidence level of this fix being correct is too low to change bare specifiers or absolute paths.
|
||||
|| pathIsRelative(importLiteral.text) && forEach(resolved.failedLookupLocations, tryChangeWithIgnoringPackageJson);
|
||||
if (result) return result;
|
||||
|
||||
// If nothing changed, then result is resolved module file thats not updated
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
/// <reference path="fourslash.ts" />
|
||||
|
||||
// @Filename: /tsconfig.json
|
||||
//// {
|
||||
//// "compilerOptions": {
|
||||
//// "allowJs": true,
|
||||
//// "paths": {
|
||||
//// "*": ["./next/src/*"],
|
||||
//// "@app": ["./modules/@app/*"],
|
||||
//// "@app/*": ["./modules/@app/*"],
|
||||
//// "@local": ["./modules/@local/*"],
|
||||
//// "@local/*": ["./modules/@local/*"]
|
||||
//// }
|
||||
//// }
|
||||
//// }
|
||||
|
||||
// @Filename: /modules/@app/something/index.js
|
||||
//// import "@local/some-other-import";
|
||||
|
||||
// @Filename: /modules/@local/index.js
|
||||
//// import "@local/some-other-import";
|
||||
|
||||
verify.getEditsForFileRename({
|
||||
oldPath: "/modules/@app/something",
|
||||
newPath: "/modules/@app/something-2",
|
||||
newFileContents: {}
|
||||
});
|
||||
@@ -0,0 +1,16 @@
|
||||
/// <reference path="fourslash.ts" />
|
||||
|
||||
// @allowJs: true
|
||||
// @checkJs: true
|
||||
|
||||
// @Filename: /modules/@app/something/index.js
|
||||
//// import "doesnt-exist";
|
||||
|
||||
// @Filename: /modules/@local/foo.js
|
||||
//// import "doesnt-exist";
|
||||
|
||||
verify.getEditsForFileRename({
|
||||
oldPath: "/modules/@app/something",
|
||||
newPath: "/modules/@app/something-2",
|
||||
newFileContents: {}
|
||||
});
|
||||
Reference in New Issue
Block a user