Skip to content

Commit baeee73

Browse files
committed
fixes
1 parent d40d5e1 commit baeee73

File tree

5 files changed

+76
-69
lines changed

5 files changed

+76
-69
lines changed

internal/api/api.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"context"
55
"errors"
66
"fmt"
7-
"os"
8-
"runtime"
97
"sync"
108

119
"github.com/go-json-experiment/json"
@@ -64,10 +62,6 @@ func (api *API) HandleRequest(ctx context.Context, method string, payload []byte
6462
if err != nil {
6563
return nil, err
6664
}
67-
fmt.Fprintf(os.Stderr, "method: %s, params: %v\n", method, params)
68-
defer func() {
69-
fmt.Fprintf(os.Stderr, "done handling method: %s, params: %v\n", method, params)
70-
}()
7165

7266
switch Method(method) {
7367
case MethodRelease:
@@ -285,7 +279,6 @@ func (api *API) GetDiagnostics(ctx context.Context, projectId Handle[project.Pro
285279

286280
languageService := ls.NewLanguageService(project, snapshot.Converters())
287281
diagnostics := languageService.GetDiagnostics(ctx)
288-
fmt.Fprintf(os.Stderr, "diagnostics: %v\n", diagnostics)
289282

290283
api.symbolsMu.Lock()
291284
defer api.symbolsMu.Unlock()
@@ -342,20 +335,12 @@ func (api *API) toPath(fileName string) tspath.Path {
342335
return tspath.ToPath(fileName, api.session.GetCurrentDirectory(), api.session.FS().UseCaseSensitiveFileNames())
343336
}
344337

345-
func stackTrace() string {
346-
buf := make([]byte, 1024)
347-
n := runtime.Stack(buf, false)
348-
return string(buf[:n])
349-
}
350-
351338
func encodeJSON(v any, err error) ([]byte, error) {
352339
if err != nil {
353-
fmt.Fprintf(os.Stderr, "encodeJSON: %v\n", err)
354340
return nil, err
355341
}
356342
b, err := json.Marshal(v)
357343
if err != nil {
358-
fmt.Fprintf(os.Stderr, "encodeJSON err: %v\n, v: %v\n; stackTrace: %v\n", err, v, stackTrace())
359344
return nil, err
360345
}
361346
return b, nil

internal/api/server.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"encoding/binary"
77
"fmt"
88
"io"
9-
"os"
109
"runtime/debug"
1110
"strconv"
1211
"strings"
@@ -175,9 +174,7 @@ func (h *hostWrapper) SessionOptions() *project.SessionOptions {
175174

176175
// IsNodeSourceFile implements project.ProjectHost.
177176
func (h *hostWrapper) IsNodeSourceFile(path tspath.Path) bool {
178-
fmt.Fprintf(os.Stderr, "host IsNodeSourceFile %s\n", path)
179177
if h.server.CallbackEnabled(CallbackIsNodeSourceFile) {
180-
fmt.Fprintf(os.Stderr, "IsNodeSourceFile callback %s\n", path)
181178
result, err := h.server.call("isNodeSourceFile", path)
182179
if err != nil {
183180
panic(err)
@@ -187,11 +184,6 @@ func (h *hostWrapper) IsNodeSourceFile(path tspath.Path) bool {
187184
if err := json.Unmarshal(result, &res); err != nil {
188185
panic(err)
189186
}
190-
if res {
191-
fmt.Fprintf(os.Stderr, "IsNodeSourceFile callback returning true %s\n", path)
192-
} else {
193-
fmt.Fprintf(os.Stderr, "IsNodeSourceFile callback returning false %s\n", path)
194-
}
195187
return res
196188
}
197189
}
@@ -219,9 +211,9 @@ func newResolverWrapper(inner module.ResolverInterface, server *Server) *resolve
219211
}
220212

221213
type PackageJsonIfApplicable struct {
222-
PackageDirectory string
223-
DirectoryExists bool
224-
Contents string
214+
PackageDirectory string `json:"packageDirectory"`
215+
DirectoryExists bool `json:"directoryExists"`
216+
Contents string `json:"contents"`
225217
}
226218

227219
// GetPackageJsonScopeIfApplicable implements module.ResolverInterface.
@@ -265,11 +257,24 @@ func (r *resolverWrapper) GetPackageScopeForPath(directory string) *packagejson.
265257
panic(err)
266258
}
267259
if len(result) > 0 {
268-
var res packagejson.InfoCacheEntry
260+
var res *PackageJsonIfApplicable
269261
if err := json.Unmarshal(result, &res); err != nil {
270262
panic(err)
271263
}
272-
return &res
264+
if res == nil {
265+
return nil
266+
}
267+
contents, err := packagejson.Parse([]byte(res.Contents))
268+
if err != nil {
269+
panic(err)
270+
}
271+
return &packagejson.InfoCacheEntry{
272+
PackageDirectory: res.PackageDirectory,
273+
DirectoryExists: res.DirectoryExists,
274+
Contents: &packagejson.PackageJson{
275+
Fields: contents,
276+
},
277+
}
273278
}
274279
}
275280
return r.inner.GetPackageScopeForPath(directory)

internal/ast/symbol.go

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package ast
22

33
import (
4-
"fmt"
54
"iter"
65
"maps"
7-
"os"
86
"strings"
97
"sync/atomic"
108

@@ -199,16 +197,18 @@ func (c *CombinedSymbolTable) Each(fn func(name string, symbol *Symbol)) {
199197

200198
// Find implements SymbolTable.
201199
func (c *CombinedSymbolTable) Find(predicate func(*Symbol) bool) *Symbol {
202-
if c.firstTable.Find(predicate) != nil {
203-
return c.firstTable.Find(predicate)
200+
ret := c.firstTable.Find(predicate)
201+
if ret != nil {
202+
return ret
204203
}
205204
return c.secondTable.Find(predicate)
206205
}
207206

208207
// Get implements SymbolTable.
209208
func (c *CombinedSymbolTable) Get(name string) *Symbol {
210-
if c.firstTable.Get(name) != nil {
211-
return c.firstTable.Get(name)
209+
ret := c.firstTable.Get(name)
210+
if ret != nil {
211+
return ret
212212
}
213213
return c.secondTable.Get(name)
214214
}
@@ -225,29 +225,46 @@ func (c *CombinedSymbolTable) Get2(name string) (*Symbol, bool) {
225225
func (c *CombinedSymbolTable) Iter() iter.Seq2[string, *Symbol] {
226226
seen := make(map[string]struct{})
227227
return func(yield func(string, *Symbol) bool) {
228-
c.firstTable.Iter()(func(name string, symbol *Symbol) bool {
228+
for name, symbol := range c.firstTable.Iter() {
229229
if _, ok := seen[name]; !ok {
230230
seen[name] = struct{}{}
231-
return yield(name, symbol)
231+
if !yield(name, symbol) {
232+
break
233+
}
232234
}
233-
return true
234-
})
235-
c.secondTable.Iter()(func(name string, symbol *Symbol) bool {
235+
}
236+
for name, symbol := range c.secondTable.Iter() {
236237
if _, ok := seen[name]; !ok {
237238
seen[name] = struct{}{}
238-
return yield(name, symbol)
239+
if !yield(name, symbol) {
240+
return
241+
}
239242
}
240-
return true
241-
})
243+
}
242244
}
243245
}
244246

245247
// Keys implements SymbolTable.
246248
func (c *CombinedSymbolTable) Keys() iter.Seq[string] {
247249
return func(yield func(string) bool) {
248-
c.Iter()(func(name string, symbol *Symbol) bool {
249-
return yield(name)
250-
})
250+
seen := make(map[string]struct{})
251+
for name := range c.firstTable.Keys() {
252+
if _, ok := seen[name]; !ok {
253+
seen[name] = struct{}{}
254+
if !yield(name) {
255+
break
256+
}
257+
}
258+
}
259+
260+
for name := range c.secondTable.Keys() {
261+
if _, ok := seen[name]; !ok {
262+
seen[name] = struct{}{}
263+
if !yield(name) {
264+
return
265+
}
266+
}
267+
}
251268
}
252269
}
253270

@@ -336,6 +353,7 @@ var TypesNodeIgnorableNames = collections.NewSetFromItems(
336353
"PerformanceEntry",
337354
"PerformanceMark",
338355
"PerformanceMeasure",
356+
"QueuingStrategy",
339357
"ReadableByteStreamController",
340358
"ReadableStream",
341359
"ReadableStreamBYOBReader",
@@ -366,13 +384,15 @@ type DenoForkContext struct {
366384
nodeGlobals SymbolTable
367385
combinedGlobals SymbolTable
368386
mergeSymbol func(target *Symbol, source *Symbol, unidirectional bool) *Symbol
387+
getMergedSymbol func(source *Symbol) *Symbol
369388
isNodeSourceFile func(path tspath.Path) bool
370389
}
371390

372391
func NewDenoForkContext(
373392
globals SymbolTable,
374393
nodeGlobals SymbolTable,
375394
mergeSymbol func(target *Symbol, source *Symbol, unidirectional bool) *Symbol,
395+
getMergedSymbol func(source *Symbol) *Symbol,
376396
isNodeSourceFile func(path tspath.Path) bool,
377397
) *DenoForkContext {
378398
return &DenoForkContext{
@@ -383,6 +403,7 @@ func NewDenoForkContext(
383403
secondTable: globals,
384404
},
385405
mergeSymbol: mergeSymbol,
406+
getMergedSymbol: getMergedSymbol,
386407
isNodeSourceFile: isNodeSourceFile,
387408
}
388409
}
@@ -396,7 +417,6 @@ func (c *DenoForkContext) GetGlobalsForName(name string) SymbolTable {
396417
}
397418

398419
func isTypesNodePkgPath(path tspath.Path) bool {
399-
fmt.Fprintf(os.Stderr, "isTypesNodePkgPath %s\n", path)
400420
return strings.HasSuffix(string(path), ".d.ts") && strings.Contains(string(path), "/@types/node/")
401421
}
402422

@@ -414,14 +434,6 @@ func symbolHasAnyTypesNodePkgDecl(symbol *Symbol, hasNodeSourceFile func(*Node)
414434
}
415435

416436
func (c *DenoForkContext) MergeGlobalSymbolTable(node *Node, source SymbolTable, unidirectional bool) {
417-
name := ""
418-
if node != nil {
419-
decl := node.Name()
420-
if decl != nil {
421-
name = decl.Text()
422-
}
423-
}
424-
fmt.Fprintf(os.Stderr, "MergeGlobalSymbolTable %s\n", name)
425437
sourceFile := GetSourceFileOfNode(node)
426438
isNodeFile := c.HasNodeSourceFile(node)
427439
isTypesNodeSourceFile := isNodeFile && isTypesNodePkgPath(sourceFile.Path())
@@ -435,17 +447,18 @@ func (c *DenoForkContext) MergeGlobalSymbolTable(node *Node, source SymbolTable,
435447
}
436448
targetSymbol := target.Get(id)
437449
if isTypesNodeSourceFile {
438-
fmt.Fprintf(os.Stderr, "isTypesNodeSourceFile %s\n", id)
439450
}
440451
if isTypesNodeSourceFile && targetSymbol != nil && TypesNodeIgnorableNames.Has(id) && !symbolHasAnyTypesNodePkgDecl(targetSymbol, c.HasNodeSourceFile) {
441-
fmt.Fprintf(os.Stderr, "ignoring %s\n", id)
442452
continue
443453
}
444-
sym := sourceSymbol
454+
var merged *Symbol
445455
if targetSymbol != nil {
446-
sym = c.mergeSymbol(targetSymbol, sourceSymbol, unidirectional)
456+
merged = c.mergeSymbol(targetSymbol, sourceSymbol, unidirectional)
457+
} else {
458+
merged = c.getMergedSymbol(sourceSymbol)
447459
}
448-
target.Set(id, sym)
460+
461+
target.Set(id, merged)
449462
}
450463
}
451464

internal/binder/nameresolver.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,8 @@ loop:
322322
if !excludeGlobals {
323323
if r.DenoContext.HasNodeSourceFile(lastLocation) {
324324
result = r.lookup(r.NodeGlobals, name, meaning|ast.SymbolFlagsGlobalLookup)
325-
} else {
325+
}
326+
if result == nil {
326327
result = r.lookup(r.DenoGlobals, name, meaning|ast.SymbolFlagsGlobalLookup)
327328
}
328329
}

internal/checker/checker.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -928,11 +928,10 @@ func NewChecker(program Program) *Checker {
928928
c.denoGlobalThisSymbol = c.newSymbolEx(ast.SymbolFlagsModule, "globalThis", ast.CheckFlagsReadonly)
929929
c.denoGlobalThisSymbol.Exports = c.denoGlobals
930930
c.denoGlobals.Set(c.denoGlobalThisSymbol.Name, c.denoGlobalThisSymbol)
931-
c.denoForkContext = ast.NewDenoForkContext(c.denoGlobals, c.nodeGlobals, c.mergeSymbol, c.program.IsNodeSourceFile)
931+
c.denoForkContext = ast.NewDenoForkContext(c.denoGlobals, c.nodeGlobals, c.mergeSymbol, c.getMergedSymbol, c.program.IsNodeSourceFile)
932932
c.nodeGlobalThisSymbol = c.newSymbolEx(ast.SymbolFlagsModule, "globalThis", ast.CheckFlagsReadonly)
933933
c.nodeGlobalThisSymbol.Exports = c.denoForkContext.CombinedGlobals()
934934
c.nodeGlobals.Set(c.nodeGlobalThisSymbol.Name, c.nodeGlobalThisSymbol)
935-
c.nodeGlobals.Set(c.nodeGlobalThisSymbol.Name, c.nodeGlobalThisSymbol)
936935
c.resolveName = c.createNameResolver().Resolve
937936
c.resolveNameForSymbolSuggestion = c.createNameResolverForSuggestion().Resolve
938937
c.tupleTypes = make(map[string]*Type)
@@ -1281,6 +1280,7 @@ func (c *Checker) initializeChecker() {
12811280
for _, file := range c.files {
12821281
if !ast.IsExternalOrCommonJSModule(file) {
12831282
c.denoForkContext.MergeGlobalSymbolTable(&file.Node, file.Locals, false /*unidirectional*/)
1283+
// c.mergeSymbolTable(c.denoGlobals, file.Locals, false,)
12841284
}
12851285
c.patternAmbientModules = append(c.patternAmbientModules, file.PatternAmbientModules...)
12861286
augmentations = append(augmentations, file.ModuleAugmentations)
@@ -10871,7 +10871,9 @@ func (c *Checker) checkPropertyAccessExpressionOrQualifiedName(node *ast.Node, l
1087110871
}
1087210872
return c.anyType
1087310873
}
10874+
// deno: ensure condition matches above
1087410875
if leftType.symbol == c.nodeGlobalThisSymbol {
10876+
// deno: don't bother with errors like above for simplicity
1087510877
return c.anyType
1087610878
}
1087710879
if right.Text() != "" && !c.checkAndReportErrorForExtendingInterface(node) {
@@ -14250,11 +14252,12 @@ func (c *Checker) canHaveSyntheticDefault(file *ast.Node, moduleSymbol *ast.Symb
1425014252
// In Node.js, CommonJS modules always have a synthetic default when imported into ESM
1425114253
return true
1425214254
}
14253-
if usageMode == core.ModuleKindESNext && targetMode == core.ModuleKindESNext {
14254-
// No matter what the `module` setting is, if we're confident that both files
14255-
// are ESM, there cannot be a synthetic default.
14256-
return false
14257-
}
14255+
// deno: commented out for https://github.com/microsoft/TypeScript/issues/51321
14256+
// if usageMode == core.ModuleKindESNext && targetMode == core.ModuleKindESNext {
14257+
// // No matter what the `module` setting is, if we're confident that both files
14258+
// // are ESM, there cannot be a synthetic default.
14259+
// return false
14260+
// }
1425814261
}
1425914262
if !c.allowSyntheticDefaultImports {
1426014263
return false
@@ -14922,7 +14925,7 @@ func (c *Checker) tryFindAmbientModule(moduleName string, withAugmentations bool
1492214925
return nil
1492314926
}
1492414927
symbol := c.getSymbol(c.denoForkContext.CombinedGlobals(), "\""+moduleName+"\"", ast.SymbolFlagsValueModule)
14925-
// merged symbol is module declaration symbol combined with all augmentations
14928+
// module declaration symbol combined with all augmentations
1492614929
if withAugmentations {
1492714930
return c.getMergedSymbol(symbol)
1492814931
}
@@ -14933,7 +14936,7 @@ func (c *Checker) GetAmbientModules(sourceFile *ast.SourceFile) []*ast.Symbol {
1493314936
isNode := c.denoForkContext.HasNodeSourceFile(&sourceFile.Node)
1493414937
if isNode {
1493514938
c.nodeAmbientModulesOnce.Do(func() {
14936-
for sym, global := range c.nodeGlobals.Iter() {
14939+
for sym, global := range c.denoForkContext.CombinedGlobals().Iter() {
1493714940
if strings.HasPrefix(sym, "\"") && strings.HasSuffix(sym, "\"") {
1493814941
c.ambientModules = append(c.ambientModules, global)
1493914942
}

0 commit comments

Comments
 (0)