Skip to content
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
10 changes: 8 additions & 2 deletions internal/api/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 11 additions & 0 deletions internal/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ type Server struct {
logger logging.Logger
api *API

forkContextInfo ast.DenoForkContextInfo

requestId int
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}

Expand Down
95 changes: 9 additions & 86 deletions internal/ast/symbol.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,7 @@ type Host interface {
modulespecifiers.ModuleSpecifierGenerationHost

IsNodeSourceFile(path tspath.Path) bool
GetDenoForkContextInfo() ast.DenoForkContextInfo
}

// Checker
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions internal/compiler/emitHost.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions internal/compiler/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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{}
}
4 changes: 4 additions & 0 deletions internal/compiler/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
4 changes: 4 additions & 0 deletions internal/execute/build/compilerHost.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 4 additions & 0 deletions internal/execute/build/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
1 change: 1 addition & 0 deletions internal/modulespecifiers/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions internal/project/compilerhost.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions internal/transformers/tstransforms/importelision_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down