From a7c6b2bbb50ed16a4e648c4d225ddec8581b677c Mon Sep 17 00:00:00 2001 From: Nathan Whitaker Date: Mon, 13 Oct 2025 20:51:04 -0700 Subject: [PATCH] factor out some constants --- internal/api/proto.go | 10 +- internal/api/server.go | 11 +++ internal/ast/symbol.go | 95 ++----------------- internal/checker/checker.go | 3 +- internal/compiler/emitHost.go | 5 + internal/compiler/host.go | 5 + internal/compiler/program.go | 4 + internal/execute/build/compilerHost.go | 4 + internal/execute/build/host.go | 4 + internal/modulespecifiers/types.go | 1 + internal/project/compilerhost.go | 5 + .../tstransforms/importelision_test.go | 4 + 12 files changed, 62 insertions(+), 89 deletions(-) diff --git a/internal/api/proto.go b/internal/api/proto.go index 7401d7e394..20f0a8d8b2 100644 --- a/internal/api/proto.go +++ b/internal/api/proto.go @@ -104,9 +104,15 @@ var unmarshalers = map[Method]func([]byte) (any, error){ MethodGetDiagnostics: unmarshallerFor[GetDiagnosticsParams], } +type ForkContextInfo struct { + TypesNodeIgnorableNames []string `json:"typesNodeIgnorableNames"` + NodeOnlyGlobalNames []string `json:"nodeOnlyGlobalNames"` +} + type ConfigureParams struct { - Callbacks []string `json:"callbacks"` - LogFile string `json:"logFile"` + Callbacks []string `json:"callbacks"` + LogFile string `json:"logFile"` + Fork ForkContextInfo `json:"forkContextInfo"` } type ParseConfigFileParams struct { diff --git a/internal/api/server.go b/internal/api/server.go index 20b1e48fdd..c971cd5428 100644 --- a/internal/api/server.go +++ b/internal/api/server.go @@ -101,6 +101,8 @@ type Server struct { logger logging.Logger api *API + forkContextInfo ast.DenoForkContextInfo + requestId int } @@ -174,6 +176,11 @@ func (h *hostWrapper) SessionOptions() *project.SessionOptions { return h.inner.SessionOptions() } +// TypesNodeIgnorableNames implements project.ProjectHost. +func (h *hostWrapper) GetDenoForkContextInfo() ast.DenoForkContextInfo { + return h.server.forkContextInfo +} + // IsNodeSourceFile implements project.ProjectHost. func (h *hostWrapper) IsNodeSourceFile(path tspath.Path) bool { if h.server.CallbackEnabled(CallbackIsNodeSourceFile) { @@ -571,6 +578,10 @@ func (s *Server) handleConfigure(payload []byte) error { } else { // s.logger.SetFile("") } + s.forkContextInfo = ast.DenoForkContextInfo{ + TypesNodeIgnorableNames: collections.NewSetFromItems(params.Fork.TypesNodeIgnorableNames...), + NodeOnlyGlobalNames: collections.NewSetFromItems(params.Fork.NodeOnlyGlobalNames...), + } return nil } diff --git a/internal/ast/symbol.go b/internal/ast/symbol.go index a26325f1ab..e38cfa0500 100644 --- a/internal/ast/symbol.go +++ b/internal/ast/symbol.go @@ -294,90 +294,10 @@ func (c *CombinedSymbolTable) Values() iter.Seq[*Symbol] { var _ SymbolTable = (*CombinedSymbolTable)(nil) -var NodeOnlyGlobalNames = collections.NewSetFromItems( - "__dirname", - "__filename", - "buffer", - "Buffer", - "BufferConstructor", - "BufferEncoding", - "clearImmediate", - "clearInterval", - "clearTimeout", - "console", - "Console", - "crypto", - "ErrorConstructor", - "gc", - "Global", - "localStorage", - "queueMicrotask", - "RequestInit", - "ResponseInit", - "sessionStorage", - "setImmediate", - "setInterval", - "setTimeout", -) - -var TypesNodeIgnorableNames = collections.NewSetFromItems( - "AbortController", - "AbortSignal", - "AsyncIteratorObject", - "atob", - "Blob", - "BroadcastChannel", - "btoa", - "ByteLengthQueuingStrategy", - "CloseEvent", - "CompressionStream", - "CountQueuingStrategy", - "CustomEvent", - "DecompressionStream", - "Disposable", - "DOMException", - "Event", - "EventSource", - "EventTarget", - "fetch", - "File", - "Float32Array", - "Float64Array", - "FormData", - "Headers", - "ImportMeta", - "MessageChannel", - "MessageEvent", - "MessagePort", - "performance", - "PerformanceEntry", - "PerformanceMark", - "PerformanceMeasure", - "QueuingStrategy", - "ReadableByteStreamController", - "ReadableStream", - "ReadableStreamBYOBReader", - "ReadableStreamBYOBRequest", - "ReadableStreamDefaultController", - "ReadableStreamDefaultReader", - "ReadonlyArray", - "Request", - "Response", - "Storage", - "TextDecoder", - "TextDecoderStream", - "TextEncoder", - "TextEncoderStream", - "TransformStream", - "TransformStreamDefaultController", - "URL", - "URLPattern", - "URLSearchParams", - "WebSocket", - "WritableStream", - "WritableStreamDefaultController", - "WritableStreamDefaultWriter", -) +type DenoForkContextInfo struct { + TypesNodeIgnorableNames *collections.Set[string] + NodeOnlyGlobalNames *collections.Set[string] +} type DenoForkContext struct { globals SymbolTable @@ -386,6 +306,7 @@ type DenoForkContext struct { mergeSymbol func(target *Symbol, source *Symbol, unidirectional bool) *Symbol getMergedSymbol func(source *Symbol) *Symbol isNodeSourceFile func(path tspath.Path) bool + info DenoForkContextInfo } func NewDenoForkContext( @@ -394,6 +315,7 @@ func NewDenoForkContext( mergeSymbol func(target *Symbol, source *Symbol, unidirectional bool) *Symbol, getMergedSymbol func(source *Symbol) *Symbol, isNodeSourceFile func(path tspath.Path) bool, + info DenoForkContextInfo, ) *DenoForkContext { return &DenoForkContext{ globals: globals, @@ -405,11 +327,12 @@ func NewDenoForkContext( mergeSymbol: mergeSymbol, getMergedSymbol: getMergedSymbol, isNodeSourceFile: isNodeSourceFile, + info: info, } } func (c *DenoForkContext) GetGlobalsForName(name string) SymbolTable { - if NodeOnlyGlobalNames.Has(name) { + if c.info.NodeOnlyGlobalNames.Has(name) { return c.nodeGlobals } else { return c.globals @@ -448,7 +371,7 @@ func (c *DenoForkContext) MergeGlobalSymbolTable(node *Node, source SymbolTable, targetSymbol := target.Get(id) if isTypesNodeSourceFile { } - if isTypesNodeSourceFile && targetSymbol != nil && TypesNodeIgnorableNames.Has(id) && !symbolHasAnyTypesNodePkgDecl(targetSymbol, c.HasNodeSourceFile) { + if isTypesNodeSourceFile && targetSymbol != nil && c.info.TypesNodeIgnorableNames.Has(id) && !symbolHasAnyTypesNodePkgDecl(targetSymbol, c.HasNodeSourceFile) { continue } var merged *Symbol diff --git a/internal/checker/checker.go b/internal/checker/checker.go index aa519a1046..72539b20fd 100644 --- a/internal/checker/checker.go +++ b/internal/checker/checker.go @@ -549,6 +549,7 @@ type Host interface { modulespecifiers.ModuleSpecifierGenerationHost IsNodeSourceFile(path tspath.Path) bool + GetDenoForkContextInfo() ast.DenoForkContextInfo } // Checker @@ -928,7 +929,7 @@ func NewChecker(program Program) *Checker { c.denoGlobalThisSymbol = c.newSymbolEx(ast.SymbolFlagsModule, "globalThis", ast.CheckFlagsReadonly) c.denoGlobalThisSymbol.Exports = c.denoGlobals c.denoGlobals.Set(c.denoGlobalThisSymbol.Name, c.denoGlobalThisSymbol) - c.denoForkContext = ast.NewDenoForkContext(c.denoGlobals, c.nodeGlobals, c.mergeSymbol, c.getMergedSymbol, c.program.IsNodeSourceFile) + c.denoForkContext = ast.NewDenoForkContext(c.denoGlobals, c.nodeGlobals, c.mergeSymbol, c.getMergedSymbol, c.program.IsNodeSourceFile, c.program.GetDenoForkContextInfo()) c.nodeGlobalThisSymbol = c.newSymbolEx(ast.SymbolFlagsModule, "globalThis", ast.CheckFlagsReadonly) c.nodeGlobalThisSymbol.Exports = c.denoForkContext.CombinedGlobals() c.nodeGlobals.Set(c.nodeGlobalThisSymbol.Name, c.nodeGlobalThisSymbol) diff --git a/internal/compiler/emitHost.go b/internal/compiler/emitHost.go index 55682d481e..6e3aafc575 100644 --- a/internal/compiler/emitHost.go +++ b/internal/compiler/emitHost.go @@ -34,6 +34,11 @@ type emitHost struct { emitResolver printer.EmitResolver } +// TypesNodeIgnorableNames implements EmitHost. +func (host *emitHost) GetDenoForkContextInfo() ast.DenoForkContextInfo { + return host.program.GetDenoForkContextInfo() +} + // IsNodeSourceFile implements EmitHost. func (host *emitHost) IsNodeSourceFile(path tspath.Path) bool { return host.program.IsNodeSourceFile(path) diff --git a/internal/compiler/host.go b/internal/compiler/host.go index 03119db8f3..0817caeae9 100644 --- a/internal/compiler/host.go +++ b/internal/compiler/host.go @@ -20,6 +20,7 @@ type CompilerHost interface { GetResolvedProjectReference(fileName string, path tspath.Path) *tsoptions.ParsedCommandLine MakeResolver(host module.ResolutionHost, options *core.CompilerOptions, typingsLocation string, projectName string) module.ResolverInterface IsNodeSourceFile(path tspath.Path) bool + GetDenoForkContextInfo() ast.DenoForkContextInfo } var _ CompilerHost = (*compilerHost)(nil) @@ -97,3 +98,7 @@ func (h *compilerHost) GetResolvedProjectReference(fileName string, path tspath. func (h *compilerHost) MakeResolver(host module.ResolutionHost, options *core.CompilerOptions, typingsLocation string, projectName string) module.ResolverInterface { return module.NewResolver(host, options, typingsLocation, projectName) } + +func (h *compilerHost) GetDenoForkContextInfo() ast.DenoForkContextInfo { + return ast.DenoForkContextInfo{} +} diff --git a/internal/compiler/program.go b/internal/compiler/program.go index fd25d5d5dd..5d56312d2c 100644 --- a/internal/compiler/program.go +++ b/internal/compiler/program.go @@ -163,6 +163,10 @@ func (p *Program) IsNodeSourceFile(path tspath.Path) bool { return p.Host().IsNodeSourceFile(path) } +func (p *Program) GetDenoForkContextInfo() ast.DenoForkContextInfo { + return p.Host().GetDenoForkContextInfo() +} + var _ checker.Program = (*Program)(nil) /** This should have similar behavior to 'processSourceFile' without diagnostics or mutation. */ diff --git a/internal/execute/build/compilerHost.go b/internal/execute/build/compilerHost.go index 3e12c7ac0a..5d94dac42f 100644 --- a/internal/execute/build/compilerHost.go +++ b/internal/execute/build/compilerHost.go @@ -29,6 +29,10 @@ func (h *compilerHost) GetCurrentDirectory() string { return h.host.GetCurrentDirectory() } +func (h *compilerHost) GetDenoForkContextInfo() ast.DenoForkContextInfo { + return h.host.GetDenoForkContextInfo() +} + func (h *compilerHost) IsNodeSourceFile(path tspath.Path) bool { return h.host.IsNodeSourceFile(path) } diff --git a/internal/execute/build/host.go b/internal/execute/build/host.go index 28951e6915..accf68d3c4 100644 --- a/internal/execute/build/host.go +++ b/internal/execute/build/host.go @@ -48,6 +48,10 @@ func (h *host) IsNodeSourceFile(path tspath.Path) bool { return h.host.IsNodeSourceFile(path) } +func (h *host) GetDenoForkContextInfo() ast.DenoForkContextInfo { + return h.host.GetDenoForkContextInfo() +} + func (h *host) DefaultLibraryPath() string { return h.host.DefaultLibraryPath() } diff --git a/internal/modulespecifiers/types.go b/internal/modulespecifiers/types.go index d890ae51d1..c22e906af5 100644 --- a/internal/modulespecifiers/types.go +++ b/internal/modulespecifiers/types.go @@ -64,6 +64,7 @@ type ModuleSpecifierGenerationHost interface { GetResolvedModuleFromModuleSpecifier(file ast.HasFileName, moduleSpecifier *ast.StringLiteralLike) *module.ResolvedModule GetModeForUsageLocation(file ast.HasFileName, moduleSpecifier *ast.StringLiteralLike) core.ResolutionMode IsNodeSourceFile(path tspath.Path) bool + GetDenoForkContextInfo() ast.DenoForkContextInfo } type ImportModuleSpecifierPreference string diff --git a/internal/project/compilerhost.go b/internal/project/compilerhost.go index 724a6285fd..f7ff520542 100644 --- a/internal/project/compilerhost.go +++ b/internal/project/compilerhost.go @@ -44,6 +44,11 @@ type compilerHost struct { logger *logging.LogTree } +// TypesNodeIgnorableNames implements compiler.CompilerHost. +func (c *compilerHost) GetDenoForkContextInfo() ast.DenoForkContextInfo { + return ast.DenoForkContextInfo{} +} + // IsNodeSourceFile implements compiler.CompilerHost. func (c *compilerHost) IsNodeSourceFile(path tspath.Path) bool { return false diff --git a/internal/transformers/tstransforms/importelision_test.go b/internal/transformers/tstransforms/importelision_test.go index 7e48c24501..f3504cd32a 100644 --- a/internal/transformers/tstransforms/importelision_test.go +++ b/internal/transformers/tstransforms/importelision_test.go @@ -169,6 +169,10 @@ func (p *fakeProgram) IsNodeSourceFile(path tspath.Path) bool { return false } +func (p *fakeProgram) GetDenoForkContextInfo() ast.DenoForkContextInfo { + return ast.DenoForkContextInfo{} +} + func TestImportElision(t *testing.T) { t.Parallel() data := []struct {