Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support resolving @typescript/[lib] in node modules #45771

Merged
merged 8 commits into from
Sep 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -995,7 +995,7 @@ namespace ts {
}
else {
forEach(options.lib, (libFileName, index) => {
processRootFile(combinePaths(defaultLibraryPath, libFileName), /*isDefaultLib*/ true, /*ignoreNoDefaultLib*/ false, { kind: FileIncludeKind.LibFile, index });
processRootFile(pathForLibFile(libFileName), /*isDefaultLib*/ true, /*ignoreNoDefaultLib*/ false, { kind: FileIncludeKind.LibFile, index });
});
}
}
Expand Down Expand Up @@ -1737,7 +1737,7 @@ namespace ts {
return equalityComparer(file.fileName, getDefaultLibraryFileName());
}
else {
return some(options.lib, libFileName => equalityComparer(file.fileName, combinePaths(defaultLibraryPath, libFileName)));
return some(options.lib, libFileName => equalityComparer(file.fileName, pathForLibFile(libFileName)));
}
}

Expand Down Expand Up @@ -2406,7 +2406,7 @@ namespace ts {
const libName = toFileNameLowerCase(ref.fileName);
const libFileName = libMap.get(libName);
if (libFileName) {
return getSourceFile(combinePaths(defaultLibraryPath, libFileName));
return getSourceFile(pathForLibFile(libFileName));
}
}

Expand Down Expand Up @@ -2883,13 +2883,32 @@ namespace ts {
}
}

function pathForLibFile(libFileName: string): string {
// Support resolving to lib.dom.d.ts -> @typescript/dom, and
// lib.dom.iterable.d.ts -> @typescript/dom/iterable
// lib.es2015.symbol.wellknown.d.ts -> @typescript/es2015/symbol-wellknown
const components = libFileName.split(".");
let path = components[1];
let i = 2;
while (components[i] && components[i] !== "d") {
path += (i === 2 ? "/" : "-") + components[i];
i++;
}
const resolveFrom = combinePaths(currentDirectory, `__lib_node_modules_lookup_${libFileName}__.ts`);
const localOverrideModuleResult = resolveModuleName("@typescript/" + path, resolveFrom, { moduleResolution: ModuleResolutionKind.NodeJs }, host, moduleResolutionCache);
if (localOverrideModuleResult?.resolvedModule) {
return localOverrideModuleResult.resolvedModule.resolvedFileName;
}
return combinePaths(defaultLibraryPath, libFileName);
}

function processLibReferenceDirectives(file: SourceFile) {
forEach(file.libReferenceDirectives, (libReference, index) => {
const libName = toFileNameLowerCase(libReference.fileName);
const libFileName = libMap.get(libName);
if (libFileName) {
// we ignore any 'no-default-lib' reference set on this file.
processRootFile(combinePaths(defaultLibraryPath, libFileName), /*isDefaultLib*/ true, /*ignoreNoDefaultLib*/ true, { kind: FileIncludeKind.LibReferenceDirective, file: file.path, index, });
processRootFile(pathForLibFile(libFileName), /*isDefaultLib*/ true, /*ignoreNoDefaultLib*/ true, { kind: FileIncludeKind.LibReferenceDirective, file: file.path, index, });
}
else {
const unqualifiedLibName = removeSuffix(removePrefix(libName, "lib."), ".d.ts");
Expand Down
14 changes: 14 additions & 0 deletions tests/baselines/reference/libTypeScriptOverrideSimple.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
tests/cases/compiler/index.ts(6,1): error TS2304: Cannot find name 'window'.


==== /node_modules/@typescript/dom/index.d.ts (0 errors) ====
interface ABC { abc: string }
==== tests/cases/compiler/index.ts (1 errors) ====
/// <reference lib="dom" />
const a: ABC = { abc: "Hello" }

// This should fail because libdom has been replaced
// by the module above ^
window.localStorage
~~~~~~
!!! error TS2304: Cannot find name 'window'.
18 changes: 18 additions & 0 deletions tests/baselines/reference/libTypeScriptOverrideSimple.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//// [tests/cases/compiler/libTypeScriptOverrideSimple.ts] ////

//// [index.d.ts]
interface ABC { abc: string }
//// [index.ts]
/// <reference lib="dom" />
const a: ABC = { abc: "Hello" }

// This should fail because libdom has been replaced
// by the module above ^
window.localStorage

//// [index.js]
/// <reference lib="dom" />
var a = { abc: "Hello" };
// This should fail because libdom has been replaced
// by the module above ^
window.localStorage;
15 changes: 15 additions & 0 deletions tests/baselines/reference/libTypeScriptOverrideSimple.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
=== /node_modules/@typescript/dom/index.d.ts ===
interface ABC { abc: string }
>ABC : Symbol(ABC, Decl(index.d.ts, 0, 0))
>abc : Symbol(ABC.abc, Decl(index.d.ts, 0, 15))

=== tests/cases/compiler/index.ts ===
/// <reference lib="dom" />
const a: ABC = { abc: "Hello" }
>a : Symbol(a, Decl(index.ts, 1, 5))
>ABC : Symbol(ABC, Decl(index.d.ts, 0, 0))
>abc : Symbol(abc, Decl(index.ts, 1, 16))

// This should fail because libdom has been replaced
// by the module above ^
window.localStorage
19 changes: 19 additions & 0 deletions tests/baselines/reference/libTypeScriptOverrideSimple.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
=== /node_modules/@typescript/dom/index.d.ts ===
interface ABC { abc: string }
>abc : string

=== tests/cases/compiler/index.ts ===
/// <reference lib="dom" />
const a: ABC = { abc: "Hello" }
>a : ABC
>{ abc: "Hello" } : { abc: string; }
>abc : string
>"Hello" : "Hello"

// This should fail because libdom has been replaced
// by the module above ^
window.localStorage
>window.localStorage : any
>window : any
>localStorage : any

16 changes: 16 additions & 0 deletions tests/baselines/reference/libTypeScriptSubfileResolving.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
tests/cases/compiler/index.ts(6,1): error TS2304: Cannot find name 'window'.


==== /node_modules/@typescript/dom/index.d.ts (0 errors) ====
// NOOP
==== /node_modules/@typescript/dom/iterable.d.ts (0 errors) ====
interface DOMIterable { abc: string }
==== tests/cases/compiler/index.ts (1 errors) ====
/// <reference lib="dom.iterable" />
const a: DOMIterable = { abc: "Hello" }

// This should fail because libdom has been replaced
// by the module above ^
window.localStorage
~~~~~~
!!! error TS2304: Cannot find name 'window'.
20 changes: 20 additions & 0 deletions tests/baselines/reference/libTypeScriptSubfileResolving.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//// [tests/cases/compiler/libTypeScriptSubfileResolving.ts] ////

//// [index.d.ts]
// NOOP
//// [iterable.d.ts]
interface DOMIterable { abc: string }
//// [index.ts]
/// <reference lib="dom.iterable" />
const a: DOMIterable = { abc: "Hello" }

// This should fail because libdom has been replaced
// by the module above ^
window.localStorage

//// [index.js]
/// <reference lib="dom.iterable" />
var a = { abc: "Hello" };
// This should fail because libdom has been replaced
// by the module above ^
window.localStorage;
17 changes: 17 additions & 0 deletions tests/baselines/reference/libTypeScriptSubfileResolving.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
=== /node_modules/@typescript/dom/index.d.ts ===
// NOOP
No type information for this code.=== /node_modules/@typescript/dom/iterable.d.ts ===
interface DOMIterable { abc: string }
>DOMIterable : Symbol(DOMIterable, Decl(iterable.d.ts, 0, 0))
>abc : Symbol(DOMIterable.abc, Decl(iterable.d.ts, 0, 23))

=== tests/cases/compiler/index.ts ===
/// <reference lib="dom.iterable" />
const a: DOMIterable = { abc: "Hello" }
>a : Symbol(a, Decl(index.ts, 1, 5))
>DOMIterable : Symbol(DOMIterable, Decl(iterable.d.ts, 0, 0))
>abc : Symbol(abc, Decl(index.ts, 1, 24))

// This should fail because libdom has been replaced
// by the module above ^
window.localStorage
21 changes: 21 additions & 0 deletions tests/baselines/reference/libTypeScriptSubfileResolving.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
=== /node_modules/@typescript/dom/index.d.ts ===
// NOOP
No type information for this code.=== /node_modules/@typescript/dom/iterable.d.ts ===
interface DOMIterable { abc: string }
>abc : string

=== tests/cases/compiler/index.ts ===
/// <reference lib="dom.iterable" />
const a: DOMIterable = { abc: "Hello" }
>a : DOMIterable
>{ abc: "Hello" } : { abc: string; }
>abc : string
>"Hello" : "Hello"

// This should fail because libdom has been replaced
// by the module above ^
window.localStorage
>window.localStorage : any
>window : any
>localStorage : any

9 changes: 9 additions & 0 deletions tests/cases/compiler/libTypeScriptOverrideSimple.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// @Filename: /node_modules/@typescript/dom/index.d.ts
interface ABC { abc: string }
// @Filename: index.ts
/// <reference lib="dom" />
const a: ABC = { abc: "Hello" }

// This should fail because libdom has been replaced
// by the module above ^
window.localStorage
11 changes: 11 additions & 0 deletions tests/cases/compiler/libTypeScriptSubfileResolving.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// @Filename: /node_modules/@typescript/dom/index.d.ts
// NOOP
// @Filename: /node_modules/@typescript/dom/iterable.d.ts
interface DOMIterable { abc: string }
// @Filename: index.ts
/// <reference lib="dom.iterable" />
const a: DOMIterable = { abc: "Hello" }

// This should fail because libdom has been replaced
// by the module above ^
window.localStorage