Skip to content

Commit a7c6b2b

Browse files
committed
factor out some constants
1 parent 42daaf4 commit a7c6b2b

File tree

12 files changed

+62
-89
lines changed

12 files changed

+62
-89
lines changed

internal/api/proto.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,15 @@ var unmarshalers = map[Method]func([]byte) (any, error){
104104
MethodGetDiagnostics: unmarshallerFor[GetDiagnosticsParams],
105105
}
106106

107+
type ForkContextInfo struct {
108+
TypesNodeIgnorableNames []string `json:"typesNodeIgnorableNames"`
109+
NodeOnlyGlobalNames []string `json:"nodeOnlyGlobalNames"`
110+
}
111+
107112
type ConfigureParams struct {
108-
Callbacks []string `json:"callbacks"`
109-
LogFile string `json:"logFile"`
113+
Callbacks []string `json:"callbacks"`
114+
LogFile string `json:"logFile"`
115+
Fork ForkContextInfo `json:"forkContextInfo"`
110116
}
111117

112118
type ParseConfigFileParams struct {

internal/api/server.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ type Server struct {
101101
logger logging.Logger
102102
api *API
103103

104+
forkContextInfo ast.DenoForkContextInfo
105+
104106
requestId int
105107
}
106108

@@ -174,6 +176,11 @@ func (h *hostWrapper) SessionOptions() *project.SessionOptions {
174176
return h.inner.SessionOptions()
175177
}
176178

179+
// TypesNodeIgnorableNames implements project.ProjectHost.
180+
func (h *hostWrapper) GetDenoForkContextInfo() ast.DenoForkContextInfo {
181+
return h.server.forkContextInfo
182+
}
183+
177184
// IsNodeSourceFile implements project.ProjectHost.
178185
func (h *hostWrapper) IsNodeSourceFile(path tspath.Path) bool {
179186
if h.server.CallbackEnabled(CallbackIsNodeSourceFile) {
@@ -571,6 +578,10 @@ func (s *Server) handleConfigure(payload []byte) error {
571578
} else {
572579
// s.logger.SetFile("")
573580
}
581+
s.forkContextInfo = ast.DenoForkContextInfo{
582+
TypesNodeIgnorableNames: collections.NewSetFromItems(params.Fork.TypesNodeIgnorableNames...),
583+
NodeOnlyGlobalNames: collections.NewSetFromItems(params.Fork.NodeOnlyGlobalNames...),
584+
}
574585
return nil
575586
}
576587

internal/ast/symbol.go

Lines changed: 9 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -294,90 +294,10 @@ func (c *CombinedSymbolTable) Values() iter.Seq[*Symbol] {
294294

295295
var _ SymbolTable = (*CombinedSymbolTable)(nil)
296296

297-
var NodeOnlyGlobalNames = collections.NewSetFromItems(
298-
"__dirname",
299-
"__filename",
300-
"buffer",
301-
"Buffer",
302-
"BufferConstructor",
303-
"BufferEncoding",
304-
"clearImmediate",
305-
"clearInterval",
306-
"clearTimeout",
307-
"console",
308-
"Console",
309-
"crypto",
310-
"ErrorConstructor",
311-
"gc",
312-
"Global",
313-
"localStorage",
314-
"queueMicrotask",
315-
"RequestInit",
316-
"ResponseInit",
317-
"sessionStorage",
318-
"setImmediate",
319-
"setInterval",
320-
"setTimeout",
321-
)
322-
323-
var TypesNodeIgnorableNames = collections.NewSetFromItems(
324-
"AbortController",
325-
"AbortSignal",
326-
"AsyncIteratorObject",
327-
"atob",
328-
"Blob",
329-
"BroadcastChannel",
330-
"btoa",
331-
"ByteLengthQueuingStrategy",
332-
"CloseEvent",
333-
"CompressionStream",
334-
"CountQueuingStrategy",
335-
"CustomEvent",
336-
"DecompressionStream",
337-
"Disposable",
338-
"DOMException",
339-
"Event",
340-
"EventSource",
341-
"EventTarget",
342-
"fetch",
343-
"File",
344-
"Float32Array",
345-
"Float64Array",
346-
"FormData",
347-
"Headers",
348-
"ImportMeta",
349-
"MessageChannel",
350-
"MessageEvent",
351-
"MessagePort",
352-
"performance",
353-
"PerformanceEntry",
354-
"PerformanceMark",
355-
"PerformanceMeasure",
356-
"QueuingStrategy",
357-
"ReadableByteStreamController",
358-
"ReadableStream",
359-
"ReadableStreamBYOBReader",
360-
"ReadableStreamBYOBRequest",
361-
"ReadableStreamDefaultController",
362-
"ReadableStreamDefaultReader",
363-
"ReadonlyArray",
364-
"Request",
365-
"Response",
366-
"Storage",
367-
"TextDecoder",
368-
"TextDecoderStream",
369-
"TextEncoder",
370-
"TextEncoderStream",
371-
"TransformStream",
372-
"TransformStreamDefaultController",
373-
"URL",
374-
"URLPattern",
375-
"URLSearchParams",
376-
"WebSocket",
377-
"WritableStream",
378-
"WritableStreamDefaultController",
379-
"WritableStreamDefaultWriter",
380-
)
297+
type DenoForkContextInfo struct {
298+
TypesNodeIgnorableNames *collections.Set[string]
299+
NodeOnlyGlobalNames *collections.Set[string]
300+
}
381301

382302
type DenoForkContext struct {
383303
globals SymbolTable
@@ -386,6 +306,7 @@ type DenoForkContext struct {
386306
mergeSymbol func(target *Symbol, source *Symbol, unidirectional bool) *Symbol
387307
getMergedSymbol func(source *Symbol) *Symbol
388308
isNodeSourceFile func(path tspath.Path) bool
309+
info DenoForkContextInfo
389310
}
390311

391312
func NewDenoForkContext(
@@ -394,6 +315,7 @@ func NewDenoForkContext(
394315
mergeSymbol func(target *Symbol, source *Symbol, unidirectional bool) *Symbol,
395316
getMergedSymbol func(source *Symbol) *Symbol,
396317
isNodeSourceFile func(path tspath.Path) bool,
318+
info DenoForkContextInfo,
397319
) *DenoForkContext {
398320
return &DenoForkContext{
399321
globals: globals,
@@ -405,11 +327,12 @@ func NewDenoForkContext(
405327
mergeSymbol: mergeSymbol,
406328
getMergedSymbol: getMergedSymbol,
407329
isNodeSourceFile: isNodeSourceFile,
330+
info: info,
408331
}
409332
}
410333

411334
func (c *DenoForkContext) GetGlobalsForName(name string) SymbolTable {
412-
if NodeOnlyGlobalNames.Has(name) {
335+
if c.info.NodeOnlyGlobalNames.Has(name) {
413336
return c.nodeGlobals
414337
} else {
415338
return c.globals
@@ -448,7 +371,7 @@ func (c *DenoForkContext) MergeGlobalSymbolTable(node *Node, source SymbolTable,
448371
targetSymbol := target.Get(id)
449372
if isTypesNodeSourceFile {
450373
}
451-
if isTypesNodeSourceFile && targetSymbol != nil && TypesNodeIgnorableNames.Has(id) && !symbolHasAnyTypesNodePkgDecl(targetSymbol, c.HasNodeSourceFile) {
374+
if isTypesNodeSourceFile && targetSymbol != nil && c.info.TypesNodeIgnorableNames.Has(id) && !symbolHasAnyTypesNodePkgDecl(targetSymbol, c.HasNodeSourceFile) {
452375
continue
453376
}
454377
var merged *Symbol

internal/checker/checker.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,7 @@ type Host interface {
549549
modulespecifiers.ModuleSpecifierGenerationHost
550550

551551
IsNodeSourceFile(path tspath.Path) bool
552+
GetDenoForkContextInfo() ast.DenoForkContextInfo
552553
}
553554

554555
// Checker
@@ -928,7 +929,7 @@ func NewChecker(program Program) *Checker {
928929
c.denoGlobalThisSymbol = c.newSymbolEx(ast.SymbolFlagsModule, "globalThis", ast.CheckFlagsReadonly)
929930
c.denoGlobalThisSymbol.Exports = c.denoGlobals
930931
c.denoGlobals.Set(c.denoGlobalThisSymbol.Name, c.denoGlobalThisSymbol)
931-
c.denoForkContext = ast.NewDenoForkContext(c.denoGlobals, c.nodeGlobals, c.mergeSymbol, c.getMergedSymbol, c.program.IsNodeSourceFile)
932+
c.denoForkContext = ast.NewDenoForkContext(c.denoGlobals, c.nodeGlobals, c.mergeSymbol, c.getMergedSymbol, c.program.IsNodeSourceFile, c.program.GetDenoForkContextInfo())
932933
c.nodeGlobalThisSymbol = c.newSymbolEx(ast.SymbolFlagsModule, "globalThis", ast.CheckFlagsReadonly)
933934
c.nodeGlobalThisSymbol.Exports = c.denoForkContext.CombinedGlobals()
934935
c.nodeGlobals.Set(c.nodeGlobalThisSymbol.Name, c.nodeGlobalThisSymbol)

internal/compiler/emitHost.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ type emitHost struct {
3434
emitResolver printer.EmitResolver
3535
}
3636

37+
// TypesNodeIgnorableNames implements EmitHost.
38+
func (host *emitHost) GetDenoForkContextInfo() ast.DenoForkContextInfo {
39+
return host.program.GetDenoForkContextInfo()
40+
}
41+
3742
// IsNodeSourceFile implements EmitHost.
3843
func (host *emitHost) IsNodeSourceFile(path tspath.Path) bool {
3944
return host.program.IsNodeSourceFile(path)

internal/compiler/host.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type CompilerHost interface {
2020
GetResolvedProjectReference(fileName string, path tspath.Path) *tsoptions.ParsedCommandLine
2121
MakeResolver(host module.ResolutionHost, options *core.CompilerOptions, typingsLocation string, projectName string) module.ResolverInterface
2222
IsNodeSourceFile(path tspath.Path) bool
23+
GetDenoForkContextInfo() ast.DenoForkContextInfo
2324
}
2425

2526
var _ CompilerHost = (*compilerHost)(nil)
@@ -97,3 +98,7 @@ func (h *compilerHost) GetResolvedProjectReference(fileName string, path tspath.
9798
func (h *compilerHost) MakeResolver(host module.ResolutionHost, options *core.CompilerOptions, typingsLocation string, projectName string) module.ResolverInterface {
9899
return module.NewResolver(host, options, typingsLocation, projectName)
99100
}
101+
102+
func (h *compilerHost) GetDenoForkContextInfo() ast.DenoForkContextInfo {
103+
return ast.DenoForkContextInfo{}
104+
}

internal/compiler/program.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ func (p *Program) IsNodeSourceFile(path tspath.Path) bool {
163163
return p.Host().IsNodeSourceFile(path)
164164
}
165165

166+
func (p *Program) GetDenoForkContextInfo() ast.DenoForkContextInfo {
167+
return p.Host().GetDenoForkContextInfo()
168+
}
169+
166170
var _ checker.Program = (*Program)(nil)
167171

168172
/** This should have similar behavior to 'processSourceFile' without diagnostics or mutation. */

internal/execute/build/compilerHost.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ func (h *compilerHost) GetCurrentDirectory() string {
2929
return h.host.GetCurrentDirectory()
3030
}
3131

32+
func (h *compilerHost) GetDenoForkContextInfo() ast.DenoForkContextInfo {
33+
return h.host.GetDenoForkContextInfo()
34+
}
35+
3236
func (h *compilerHost) IsNodeSourceFile(path tspath.Path) bool {
3337
return h.host.IsNodeSourceFile(path)
3438
}

internal/execute/build/host.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ func (h *host) IsNodeSourceFile(path tspath.Path) bool {
4848
return h.host.IsNodeSourceFile(path)
4949
}
5050

51+
func (h *host) GetDenoForkContextInfo() ast.DenoForkContextInfo {
52+
return h.host.GetDenoForkContextInfo()
53+
}
54+
5155
func (h *host) DefaultLibraryPath() string {
5256
return h.host.DefaultLibraryPath()
5357
}

internal/modulespecifiers/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ type ModuleSpecifierGenerationHost interface {
6464
GetResolvedModuleFromModuleSpecifier(file ast.HasFileName, moduleSpecifier *ast.StringLiteralLike) *module.ResolvedModule
6565
GetModeForUsageLocation(file ast.HasFileName, moduleSpecifier *ast.StringLiteralLike) core.ResolutionMode
6666
IsNodeSourceFile(path tspath.Path) bool
67+
GetDenoForkContextInfo() ast.DenoForkContextInfo
6768
}
6869

6970
type ImportModuleSpecifierPreference string

0 commit comments

Comments
 (0)