Skip to content

Commit b2f9432

Browse files
author
Orta Therox
authoredSep 15, 2021
Support resolving @typescript/[lib] in node modules (#45771)
* Support resolving @typescript/x for libs * Baselines * Tightens up the PR * Fix the build * Add cache * Better naming * Fixes the lookup path
1 parent 7cba519 commit b2f9432

11 files changed

+183
-4
lines changed
 

Diff for: ‎src/compiler/program.ts

+23-4
Original file line numberDiff line numberDiff line change
@@ -995,7 +995,7 @@ namespace ts {
995995
}
996996
else {
997997
forEach(options.lib, (libFileName, index) => {
998-
processRootFile(combinePaths(defaultLibraryPath, libFileName), /*isDefaultLib*/ true, /*ignoreNoDefaultLib*/ false, { kind: FileIncludeKind.LibFile, index });
998+
processRootFile(pathForLibFile(libFileName), /*isDefaultLib*/ true, /*ignoreNoDefaultLib*/ false, { kind: FileIncludeKind.LibFile, index });
999999
});
10001000
}
10011001
}
@@ -1737,7 +1737,7 @@ namespace ts {
17371737
return equalityComparer(file.fileName, getDefaultLibraryFileName());
17381738
}
17391739
else {
1740-
return some(options.lib, libFileName => equalityComparer(file.fileName, combinePaths(defaultLibraryPath, libFileName)));
1740+
return some(options.lib, libFileName => equalityComparer(file.fileName, pathForLibFile(libFileName)));
17411741
}
17421742
}
17431743

@@ -2406,7 +2406,7 @@ namespace ts {
24062406
const libName = toFileNameLowerCase(ref.fileName);
24072407
const libFileName = libMap.get(libName);
24082408
if (libFileName) {
2409-
return getSourceFile(combinePaths(defaultLibraryPath, libFileName));
2409+
return getSourceFile(pathForLibFile(libFileName));
24102410
}
24112411
}
24122412

@@ -2883,13 +2883,32 @@ namespace ts {
28832883
}
28842884
}
28852885

2886+
function pathForLibFile(libFileName: string): string {
2887+
// Support resolving to lib.dom.d.ts -> @typescript/dom, and
2888+
// lib.dom.iterable.d.ts -> @typescript/dom/iterable
2889+
// lib.es2015.symbol.wellknown.d.ts -> @typescript/es2015/symbol-wellknown
2890+
const components = libFileName.split(".");
2891+
let path = components[1];
2892+
let i = 2;
2893+
while (components[i] && components[i] !== "d") {
2894+
path += (i === 2 ? "/" : "-") + components[i];
2895+
i++;
2896+
}
2897+
const resolveFrom = combinePaths(currentDirectory, `__lib_node_modules_lookup_${libFileName}__.ts`);
2898+
const localOverrideModuleResult = resolveModuleName("@typescript/" + path, resolveFrom, { moduleResolution: ModuleResolutionKind.NodeJs }, host, moduleResolutionCache);
2899+
if (localOverrideModuleResult?.resolvedModule) {
2900+
return localOverrideModuleResult.resolvedModule.resolvedFileName;
2901+
}
2902+
return combinePaths(defaultLibraryPath, libFileName);
2903+
}
2904+
28862905
function processLibReferenceDirectives(file: SourceFile) {
28872906
forEach(file.libReferenceDirectives, (libReference, index) => {
28882907
const libName = toFileNameLowerCase(libReference.fileName);
28892908
const libFileName = libMap.get(libName);
28902909
if (libFileName) {
28912910
// 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, });
2911+
processRootFile(pathForLibFile(libFileName), /*isDefaultLib*/ true, /*ignoreNoDefaultLib*/ true, { kind: FileIncludeKind.LibReferenceDirective, file: file.path, index, });
28932912
}
28942913
else {
28952914
const unqualifiedLibName = removeSuffix(removePrefix(libName, "lib."), ".d.ts");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
tests/cases/compiler/index.ts(6,1): error TS2304: Cannot find name 'window'.
2+
3+
4+
==== /node_modules/@typescript/dom/index.d.ts (0 errors) ====
5+
interface ABC { abc: string }
6+
==== tests/cases/compiler/index.ts (1 errors) ====
7+
/// <reference lib="dom" />
8+
const a: ABC = { abc: "Hello" }
9+
10+
// This should fail because libdom has been replaced
11+
// by the module above ^
12+
window.localStorage
13+
~~~~~~
14+
!!! error TS2304: Cannot find name 'window'.
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//// [tests/cases/compiler/libTypeScriptOverrideSimple.ts] ////
2+
3+
//// [index.d.ts]
4+
interface ABC { abc: string }
5+
//// [index.ts]
6+
/// <reference lib="dom" />
7+
const a: ABC = { abc: "Hello" }
8+
9+
// This should fail because libdom has been replaced
10+
// by the module above ^
11+
window.localStorage
12+
13+
//// [index.js]
14+
/// <reference lib="dom" />
15+
var a = { abc: "Hello" };
16+
// This should fail because libdom has been replaced
17+
// by the module above ^
18+
window.localStorage;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
=== /node_modules/@typescript/dom/index.d.ts ===
2+
interface ABC { abc: string }
3+
>ABC : Symbol(ABC, Decl(index.d.ts, 0, 0))
4+
>abc : Symbol(ABC.abc, Decl(index.d.ts, 0, 15))
5+
6+
=== tests/cases/compiler/index.ts ===
7+
/// <reference lib="dom" />
8+
const a: ABC = { abc: "Hello" }
9+
>a : Symbol(a, Decl(index.ts, 1, 5))
10+
>ABC : Symbol(ABC, Decl(index.d.ts, 0, 0))
11+
>abc : Symbol(abc, Decl(index.ts, 1, 16))
12+
13+
// This should fail because libdom has been replaced
14+
// by the module above ^
15+
window.localStorage
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
=== /node_modules/@typescript/dom/index.d.ts ===
2+
interface ABC { abc: string }
3+
>abc : string
4+
5+
=== tests/cases/compiler/index.ts ===
6+
/// <reference lib="dom" />
7+
const a: ABC = { abc: "Hello" }
8+
>a : ABC
9+
>{ abc: "Hello" } : { abc: string; }
10+
>abc : string
11+
>"Hello" : "Hello"
12+
13+
// This should fail because libdom has been replaced
14+
// by the module above ^
15+
window.localStorage
16+
>window.localStorage : any
17+
>window : any
18+
>localStorage : any
19+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
tests/cases/compiler/index.ts(6,1): error TS2304: Cannot find name 'window'.
2+
3+
4+
==== /node_modules/@typescript/dom/index.d.ts (0 errors) ====
5+
// NOOP
6+
==== /node_modules/@typescript/dom/iterable.d.ts (0 errors) ====
7+
interface DOMIterable { abc: string }
8+
==== tests/cases/compiler/index.ts (1 errors) ====
9+
/// <reference lib="dom.iterable" />
10+
const a: DOMIterable = { abc: "Hello" }
11+
12+
// This should fail because libdom has been replaced
13+
// by the module above ^
14+
window.localStorage
15+
~~~~~~
16+
!!! error TS2304: Cannot find name 'window'.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//// [tests/cases/compiler/libTypeScriptSubfileResolving.ts] ////
2+
3+
//// [index.d.ts]
4+
// NOOP
5+
//// [iterable.d.ts]
6+
interface DOMIterable { abc: string }
7+
//// [index.ts]
8+
/// <reference lib="dom.iterable" />
9+
const a: DOMIterable = { abc: "Hello" }
10+
11+
// This should fail because libdom has been replaced
12+
// by the module above ^
13+
window.localStorage
14+
15+
//// [index.js]
16+
/// <reference lib="dom.iterable" />
17+
var a = { abc: "Hello" };
18+
// This should fail because libdom has been replaced
19+
// by the module above ^
20+
window.localStorage;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
=== /node_modules/@typescript/dom/index.d.ts ===
2+
// NOOP
3+
No type information for this code.=== /node_modules/@typescript/dom/iterable.d.ts ===
4+
interface DOMIterable { abc: string }
5+
>DOMIterable : Symbol(DOMIterable, Decl(iterable.d.ts, 0, 0))
6+
>abc : Symbol(DOMIterable.abc, Decl(iterable.d.ts, 0, 23))
7+
8+
=== tests/cases/compiler/index.ts ===
9+
/// <reference lib="dom.iterable" />
10+
const a: DOMIterable = { abc: "Hello" }
11+
>a : Symbol(a, Decl(index.ts, 1, 5))
12+
>DOMIterable : Symbol(DOMIterable, Decl(iterable.d.ts, 0, 0))
13+
>abc : Symbol(abc, Decl(index.ts, 1, 24))
14+
15+
// This should fail because libdom has been replaced
16+
// by the module above ^
17+
window.localStorage
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
=== /node_modules/@typescript/dom/index.d.ts ===
2+
// NOOP
3+
No type information for this code.=== /node_modules/@typescript/dom/iterable.d.ts ===
4+
interface DOMIterable { abc: string }
5+
>abc : string
6+
7+
=== tests/cases/compiler/index.ts ===
8+
/// <reference lib="dom.iterable" />
9+
const a: DOMIterable = { abc: "Hello" }
10+
>a : DOMIterable
11+
>{ abc: "Hello" } : { abc: string; }
12+
>abc : string
13+
>"Hello" : "Hello"
14+
15+
// This should fail because libdom has been replaced
16+
// by the module above ^
17+
window.localStorage
18+
>window.localStorage : any
19+
>window : any
20+
>localStorage : any
21+

Diff for: ‎tests/cases/compiler/libTypeScriptOverrideSimple.ts

+9
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
+11
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)
Please sign in to comment.