Skip to content

Commit 462776e

Browse files
committed
add lib.node.d.ts types injection
1 parent 5cfb0e4 commit 462776e

2 files changed

Lines changed: 31 additions & 9 deletions

File tree

src/compiler/deno.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ let typesNodeIgnorableNames = new Set<ts.__String>();
99
export type EnterSpan = (name: string) => object;
1010
export type ExitSpan = (span: object) => void;
1111

12-
export let enterSpan: EnterSpan = () => ({ });
13-
export let exitSpan: ExitSpan = () => { };
12+
export let enterSpan: EnterSpan = () => ({});
13+
export let exitSpan: ExitSpan = () => {};
1414

1515
export function setEnterSpan(f: EnterSpan): void {
1616
enterSpan = f;
@@ -27,11 +27,12 @@ export function spanned<T>(name: string, f: () => T): T {
2727
if (result instanceof Promise) {
2828
needsExit = false;
2929
return result.finally(() => exitSpan(span)) as T;
30-
} else {
30+
}
31+
else {
3132
return result;
3233
}
33-
34-
} finally {
34+
}
35+
finally {
3536
if (needsExit) {
3637
exitSpan(span);
3738
}
@@ -123,10 +124,6 @@ export function createDenoForkContext({
123124
return false;
124125
}
125126

126-
function isTypesNodePkgPath(path: ts.Path) {
127-
return path.endsWith(".d.ts") && path.includes("/@types/node/");
128-
}
129-
130127
function createNodeGlobalsSymbolTable() {
131128
return new Proxy(globals, {
132129
get(target, prop: string | symbol, receiver) {
@@ -207,6 +204,10 @@ export function createDenoForkContext({
207204
}
208205
}
209206

207+
export function isTypesNodePkgPath(path: ts.Path): boolean {
208+
return path.endsWith(".d.ts") && path.includes("/@types/node/");
209+
}
210+
210211
export interface NpmPackageReference {
211212
name: string;
212213
versionReq: string | undefined;

src/compiler/program.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import {
5252
CustomTransformers,
5353
Debug,
5454
DeclarationWithTypeParameterChildren,
55+
deno,
5556
Diagnostic,
5657
DiagnosticArguments,
5758
DiagnosticCategory,
@@ -1783,6 +1784,26 @@ export function createProgram(_rootNamesOrOptions: readonly string[] | CreatePro
17831784
forEach(rootNames, (name, index) => processRootFile(name, /*isDefaultLib*/ false, /*ignoreNoDefaultLib*/ false, { kind: FileIncludeKind.RootFile, index }));
17841785
tracing?.pop();
17851786

1787+
const nodeTypesLibUrl = "asset:///lib.node.d.ts" as Path;
1788+
const denoTypesLibUrl = "asset:///lib.deno.ns.d.ts" as Path;
1789+
const includeImplicitTypesNode = (() => {
1790+
// do not include if not has deno.ns lib or has node types lib already
1791+
if (!filesByName.has(denoTypesLibUrl) || filesByName.has(nodeTypesLibUrl)) {
1792+
return false;
1793+
}
1794+
// do not include if there is any @types/node package
1795+
for (const path of filesByName.keys()) {
1796+
if (deno.isTypesNodePkgPath(path)) {
1797+
return false;
1798+
}
1799+
}
1800+
return true;
1801+
})();
1802+
1803+
if (includeImplicitTypesNode) {
1804+
processRootFile(nodeTypesLibUrl, /*isDefaultLib*/ true, /*ignoreNoDefaultLib*/ false, { kind: FileIncludeKind.LibFile });
1805+
}
1806+
17861807
// load type declarations specified via 'types' argument or implicitly from types/ and node_modules/@types folders
17871808
automaticTypeDirectiveNames ??= rootNames.length ? getAutomaticTypeDirectiveNames(options, host) : emptyArray;
17881809
automaticTypeDirectiveResolutions = createModeAwareCache();

0 commit comments

Comments
 (0)