Skip to content

Commit 6efc2f1

Browse files
committed
Support resolving @typescript/x for libs
1 parent 7849342 commit 6efc2f1

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

src/compiler/program.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,8 @@ namespace ts {
887887
// Key is a file name. Value is the (non-empty, or undefined) list of files that redirect to it.
888888
let redirectTargetsMap = createMultiMap<Path, string>();
889889
let usesUriStyleNodeCoreModules = false;
890+
// Used when resolving something like 'dom' for libs
891+
// const libResolveCache = createModuleResolutionCache(currentDirectory, host.getCanonicalFileName, { moduleResolution: ModuleResolutionKind.NodeJs });
890892

891893
/**
892894
* map with
@@ -995,7 +997,7 @@ namespace ts {
995997
}
996998
else {
997999
forEach(options.lib, (libFileName, index) => {
998-
processRootFile(combinePaths(defaultLibraryPath, libFileName), /*isDefaultLib*/ true, /*ignoreNoDefaultLib*/ false, { kind: FileIncludeKind.LibFile, index });
1000+
processRootFile(pathForLibFile(libFileName), /*isDefaultLib*/ true, /*ignoreNoDefaultLib*/ false, { kind: FileIncludeKind.LibFile, index });
9991001
});
10001002
}
10011003
}
@@ -1737,7 +1739,7 @@ namespace ts {
17371739
return equalityComparer(file.fileName, getDefaultLibraryFileName());
17381740
}
17391741
else {
1740-
return some(options.lib, libFileName => equalityComparer(file.fileName, combinePaths(defaultLibraryPath, libFileName)));
1742+
return some(options.lib, libFileName => equalityComparer(file.fileName, pathForLibFile(libFileName)));
17411743
}
17421744
}
17431745

@@ -2406,7 +2408,7 @@ namespace ts {
24062408
const libName = toFileNameLowerCase(ref.fileName);
24072409
const libFileName = libMap.get(libName);
24082410
if (libFileName) {
2409-
return getSourceFile(combinePaths(defaultLibraryPath, libFileName));
2411+
return getSourceFile(pathForLibFile(libFileName));
24102412
}
24112413
}
24122414

@@ -2883,13 +2885,32 @@ namespace ts {
28832885
}
28842886
}
28852887

2888+
function pathForLibFile(libFileName: string): string {
2889+
// Support resolving to lib.dom.d.ts -> @typescript/dom, and
2890+
// lib.dom.iterable.d.ts -> @typescript/dom/iterable
2891+
// lib.es2015.symbol.wellknown.d.ts -> @typescript/es2015/symbol-wellknown
2892+
const components = libFileName.split(".");
2893+
let path = components[1];
2894+
let i = 2;
2895+
while (components[i] && components[i] !== "d") {
2896+
path += (i === 2 ? "/" : "-") + components[i];
2897+
i++;
2898+
}
2899+
// const localOverride = resolveModuleNameFromCache("@typescript/" + path, currentDirectory, libResolveCache);
2900+
const localOverride = resolveModuleName("@typescript/" + path, currentDirectory, { moduleResolution: ModuleResolutionKind.NodeJs }, host);
2901+
if (localOverride?.resolvedModule) {
2902+
return localOverride.resolvedModule.resolvedFileName;
2903+
}
2904+
return combinePaths(defaultLibraryPath, libFileName);
2905+
}
2906+
28862907
function processLibReferenceDirectives(file: SourceFile) {
28872908
forEach(file.libReferenceDirectives, (libReference, index) => {
28882909
const libName = toFileNameLowerCase(libReference.fileName);
28892910
const libFileName = libMap.get(libName);
28902911
if (libFileName) {
28912912
// we ignore any 'no-default-lib' reference set on this file.
2892-
processRootFile(combinePaths(defaultLibraryPath, libFileName), /*isDefaultLib*/ true, /*ignoreNoDefaultLib*/ true, { kind: FileIncludeKind.LibReferenceDirective, file: file.path, index, });
2913+
processRootFile(pathForLibFile(libFileName), /*isDefaultLib*/ true, /*ignoreNoDefaultLib*/ true, { kind: FileIncludeKind.LibReferenceDirective, file: file.path, index, });
28932914
}
28942915
else {
28952916
const unqualifiedLibName = removeSuffix(removePrefix(libName, "lib."), ".d.ts");
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// @Filename: /node_modules/@typescript/dom/index.d.ts
2+
interface ABC { abc: string }
3+
// @Filename: index.ts
4+
/// <reference lib="dom" />
5+
const a: ABC = { abc: "Hello" }
6+
7+
// This should fail because libdom has been replaced
8+
// by the module above ^
9+
window.localStorage
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// @Filename: /node_modules/@typescript/dom/index.d.ts
2+
// NOOP
3+
// @Filename: /node_modules/@typescript/dom/iterable.d.ts
4+
interface DOMIterable { abc: string }
5+
// @Filename: index.ts
6+
/// <reference lib="dom.iterable" />
7+
const a: DOMIterable = { abc: "Hello" }
8+
9+
// This should fail because libdom has been replaced
10+
// by the module above ^
11+
window.localStorage

0 commit comments

Comments
 (0)