Skip to content

Commit 680bfbb

Browse files
author
Andy
authored
Combine moduleHasNonRelativeName with isExternalModuleNameRelative (#16564)
1 parent d2ec45f commit 680bfbb

File tree

4 files changed

+18
-11
lines changed

4 files changed

+18
-11
lines changed

src/compiler/core.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1580,10 +1580,21 @@ namespace ts {
15801580
return path && !isRootedDiskPath(path) && path.indexOf("://") !== -1;
15811581
}
15821582

1583+
/* @internal */
1584+
export function pathIsRelative(path: string): boolean {
1585+
return /^\.\.?($|[\\/])/.test(path);
1586+
}
1587+
15831588
export function isExternalModuleNameRelative(moduleName: string): boolean {
15841589
// TypeScript 1.0 spec (April 2014): 11.2.1
15851590
// An external module name is "relative" if the first term is "." or "..".
1586-
return /^\.\.?($|[\\/])/.test(moduleName);
1591+
// Update: We also consider a path like `C:\foo.ts` "relative" because we do not search for it in `node_modules` or treat it as an ambient module.
1592+
return pathIsRelative(moduleName) || isRootedDiskPath(moduleName);
1593+
}
1594+
1595+
/** @deprecated Use `!isExternalModuleNameRelative(moduleName)` instead. */
1596+
export function moduleHasNonRelativeName(moduleName: string): boolean {
1597+
return !isExternalModuleNameRelative(moduleName);
15871598
}
15881599

15891600
export function getEmitScriptTarget(compilerOptions: CompilerOptions) {

src/compiler/moduleNameResolver.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,6 @@ namespace ts {
5454
};
5555
}
5656

57-
export function moduleHasNonRelativeName(moduleName: string): boolean {
58-
return !(isRootedDiskPath(moduleName) || isExternalModuleNameRelative(moduleName));
59-
}
60-
6157
interface ModuleResolutionState {
6258
host: ModuleResolutionHost;
6359
compilerOptions: CompilerOptions;
@@ -318,7 +314,7 @@ namespace ts {
318314
}
319315

320316
function getOrCreateCacheForModuleName(nonRelativeModuleName: string) {
321-
if (!moduleHasNonRelativeName(nonRelativeModuleName)) {
317+
if (isExternalModuleNameRelative(nonRelativeModuleName)) {
322318
return undefined;
323319
}
324320
let perModuleNameCache = moduleNameToDirectoryMap.get(nonRelativeModuleName);
@@ -535,7 +531,7 @@ namespace ts {
535531
function tryLoadModuleUsingOptionalResolutionSettings(extensions: Extensions, moduleName: string, containingDirectory: string, loader: ResolutionKindSpecificLoader,
536532
failedLookupLocations: Push<string>, state: ModuleResolutionState): Resolved | undefined {
537533

538-
if (moduleHasNonRelativeName(moduleName)) {
534+
if (!isExternalModuleNameRelative(moduleName)) {
539535
return tryLoadModuleUsingBaseUrl(extensions, moduleName, loader, failedLookupLocations, state);
540536
}
541537
else {
@@ -711,7 +707,7 @@ namespace ts {
711707
return toSearchResult({ resolved, isExternalLibraryImport: false });
712708
}
713709

714-
if (moduleHasNonRelativeName(moduleName)) {
710+
if (!isExternalModuleNameRelative(moduleName)) {
715711
if (traceEnabled) {
716712
trace(host, Diagnostics.Loading_module_0_from_node_modules_folder_target_file_type_1, moduleName, Extensions[extensions]);
717713
}
@@ -1024,7 +1020,7 @@ namespace ts {
10241020
}
10251021
const perModuleNameCache = cache && cache.getOrCreateCacheForModuleName(moduleName);
10261022

1027-
if (moduleHasNonRelativeName(moduleName)) {
1023+
if (!isExternalModuleNameRelative(moduleName)) {
10281024
// Climb up parent directories looking for a module.
10291025
const resolved = forEachAncestorDirectory(containingDirectory, directory => {
10301026
const resolutionFromCache = tryFindNonRelativeModuleNameInCache(perModuleNameCache, moduleName, directory, traceEnabled, host);

src/server/lsHost.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace ts.server {
2929
: undefined;
3030
const primaryResult = resolveModuleName(moduleName, containingFile, compilerOptions, host);
3131
// return result immediately only if it is .ts, .tsx or .d.ts
32-
if (moduleHasNonRelativeName(moduleName) && !(primaryResult.resolvedModule && extensionIsTypeScript(primaryResult.resolvedModule.extension)) && globalCache !== undefined) {
32+
if (!isExternalModuleNameRelative(moduleName) && !(primaryResult.resolvedModule && extensionIsTypeScript(primaryResult.resolvedModule.extension)) && globalCache !== undefined) {
3333
// otherwise try to load typings from @types
3434

3535
// create different collection of failed lookup locations for second pass

src/services/codefixes/importFixes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ namespace ts.codefix {
565565

566566
function getRelativePath(path: string, directoryPath: string) {
567567
const relativePath = getRelativePathToDirectoryOrUrl(directoryPath, path, directoryPath, getCanonicalFileName, /*isAbsolutePathAnUrl*/ false);
568-
return moduleHasNonRelativeName(relativePath) ? "./" + relativePath : relativePath;
568+
return !pathIsRelative(relativePath) ? "./" + relativePath : relativePath;
569569
}
570570
}
571571

0 commit comments

Comments
 (0)