Skip to content

Commit 383c1a6

Browse files
authored
Only include asset:///lib.node.d.ts when no @types/node package exists (#16)
1 parent 1193afc commit 383c1a6

File tree

4 files changed

+43
-9
lines changed

4 files changed

+43
-9
lines changed

internal/ast/symbol.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package ast
33
import (
44
"iter"
55
"maps"
6-
"strings"
76
"sync/atomic"
87

98
"github.com/microsoft/typescript-go/internal/collections"
9+
"github.com/microsoft/typescript-go/internal/deno"
1010
"github.com/microsoft/typescript-go/internal/tspath"
1111
)
1212

@@ -350,17 +350,13 @@ func (c *DenoForkContext) GetGlobalsForName(name string) SymbolTable {
350350
}
351351
}
352352

353-
func isTypesNodePkgPath(path tspath.Path) bool {
354-
return strings.HasSuffix(string(path), ".d.ts") && strings.Contains(string(path), "/@types/node/")
355-
}
356-
357353
func symbolHasAnyTypesNodePkgDecl(symbol *Symbol, hasNodeSourceFile func(*Node) bool) bool {
358354
if symbol == nil || symbol.Declarations == nil {
359355
return false
360356
}
361357
for _, decl := range symbol.Declarations {
362358
sourceFile := GetSourceFileOfNode(decl)
363-
if sourceFile != nil && hasNodeSourceFile(decl) && isTypesNodePkgPath(sourceFile.Path()) {
359+
if sourceFile != nil && hasNodeSourceFile(decl) && deno.IsTypesNodePkgPath(sourceFile.Path()) {
364360
return true
365361
}
366362
}
@@ -370,7 +366,7 @@ func symbolHasAnyTypesNodePkgDecl(symbol *Symbol, hasNodeSourceFile func(*Node)
370366
func (c *DenoForkContext) MergeGlobalSymbolTable(node *Node, source SymbolTable, unidirectional bool) {
371367
sourceFile := GetSourceFileOfNode(node)
372368
isNodeFile := c.HasNodeSourceFile(node)
373-
isTypesNodeSourceFile := isNodeFile && isTypesNodePkgPath(sourceFile.Path())
369+
isTypesNodeSourceFile := isNodeFile && deno.IsTypesNodePkgPath(sourceFile.Path())
374370

375371
for id, sourceSymbol := range source.Iter() {
376372
var target SymbolTable

internal/compiler/fileloader.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/microsoft/typescript-go/internal/ast"
1111
"github.com/microsoft/typescript-go/internal/collections"
1212
"github.com/microsoft/typescript-go/internal/core"
13+
"github.com/microsoft/typescript-go/internal/deno"
1314
"github.com/microsoft/typescript-go/internal/module"
1415
"github.com/microsoft/typescript-go/internal/tsoptions"
1516
"github.com/microsoft/typescript-go/internal/tspath"
@@ -107,6 +108,11 @@ func processAllProgramFiles(
107108
for index, rootFile := range rootFiles {
108109
loader.addRootTask(rootFile, nil, &FileIncludeReason{kind: fileIncludeKindRootFile, data: index})
109110
}
111+
112+
// deno: cause the sub tasks to be loaded which will tell us if there's a @types/node package
113+
loader.filesParser.parse(&loader, loader.rootTasks)
114+
rootTasksBeforeLibs := len(loader.rootTasks)
115+
110116
if len(rootFiles) > 0 && compilerOptions.NoLib.IsFalseOrUnknown() {
111117
if compilerOptions.Lib == nil {
112118
name := tsoptions.GetDefaultLibFileName(compilerOptions)
@@ -117,6 +123,10 @@ func processAllProgramFiles(
117123
for index, lib := range compilerOptions.Lib {
118124
if name, ok := tsoptions.GetLibFileName(lib); ok {
119125
libFile := loader.pathForLibFile(name)
126+
// deno: we skip loading the lib.node.d.ts file if the @types/node package has been loaded
127+
if libFile.Name == "lib.node.d.ts" && loader.hasTypesNodePackage() {
128+
continue
129+
}
120130
loader.addRootTask(libFile.path, libFile, &FileIncludeReason{kind: fileIncludeKindLibFile, data: index})
121131
}
122132
// !!! error on unknown name
@@ -128,7 +138,10 @@ func processAllProgramFiles(
128138
loader.addAutomaticTypeDirectiveTasks()
129139
}
130140

131-
loader.filesParser.parse(&loader, loader.rootTasks)
141+
// deno: now load the lib and type directive tasks
142+
loader.filesParser.wg = core.NewWorkGroup(singleThreaded)
143+
loader.filesParser.parse(&loader, loader.rootTasks[rootTasksBeforeLibs:])
144+
132145
// Clear out loader and host to ensure its not used post program creation
133146
loader.projectReferenceFileMapper.loader = nil
134147
loader.projectReferenceFileMapper.host = nil
@@ -257,6 +270,19 @@ func (p *fileLoader) toPath(file string) tspath.Path {
257270
return tspath.ToPath(file, p.opts.Host.GetCurrentDirectory(), p.opts.Host.FS().UseCaseSensitiveFileNames())
258271
}
259272

273+
// deno: function for getting if the loader has a @types/node package
274+
func (p *fileLoader) hasTypesNodePackage() bool {
275+
hasTypesNode := false
276+
p.filesParser.tasksByFileName.Range(func(key string, value *queuedParseTask) bool {
277+
if value.task.loaded && deno.IsTypesNodePkgPath(value.task.path) {
278+
hasTypesNode = true
279+
return false // stop iteration
280+
}
281+
return true // continue iteration
282+
})
283+
return hasTypesNode
284+
}
285+
260286
func (p *fileLoader) addRootTask(fileName string, libFile *LibFile, includeReason *FileIncludeReason) {
261287
absPath := tspath.GetNormalizedAbsolutePath(fileName, p.opts.Host.GetCurrentDirectory())
262288
if core.Tristate.IsTrue(p.opts.Config.CompilerOptions().AllowNonTsExtensions) || slices.Contains(p.supportedExtensions, tspath.TryGetExtensionFromPath(absPath)) {
@@ -609,7 +635,7 @@ func (p *fileLoader) createSyntheticImport(text string, file *ast.SourceFile) *a
609635
}
610636

611637
func isDenoLibFile(name string) bool {
612-
return strings.HasPrefix(name, "lib.deno")
638+
return strings.HasPrefix(name, "lib.deno") || name == "lib.node.d.ts"
613639
}
614640

615641
func (p *fileLoader) pathForLibFile(name string) *LibFile {

internal/deno/deno.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package deno
2+
3+
import (
4+
"strings"
5+
6+
"github.com/microsoft/typescript-go/internal/tspath"
7+
)
8+
9+
func IsTypesNodePkgPath(path tspath.Path) bool {
10+
return strings.HasSuffix(string(path), ".d.ts") && strings.Contains(string(path), "/@types/node/")
11+
}

internal/tsoptions/enummaps.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ var LibMap = collections.NewOrderedMapFromList([]collections.MapEntry[string, an
131131
{Key: "deno.shared_globals", Value: "lib.deno.shared_globals.d.ts"},
132132
{Key: "deno.unstable", Value: "lib.deno.unstable.d.ts"},
133133
{Key: "dom.extras", Value: "lib.dom.extras.d.ts"},
134+
{Key: "node", Value: "lib.node.d.ts"},
134135
})
135136

136137
var (

0 commit comments

Comments
 (0)