forked from microsoft/typescript-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsymbol.go
More file actions
404 lines (354 loc) · 11.3 KB
/
symbol.go
File metadata and controls
404 lines (354 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
package ast
import (
"iter"
"maps"
"strings"
"sync/atomic"
"github.com/microsoft/typescript-go/internal/collections"
"github.com/microsoft/typescript-go/internal/tspath"
)
// Symbol
type Symbol struct {
Flags SymbolFlags
CheckFlags CheckFlags // Non-zero only in transient symbols created by Checker
Name string
Declarations []*Node
ValueDeclaration *Node
Members SymbolTable
Exports SymbolTable
id atomic.Uint64
Parent *Symbol
ExportSymbol *Symbol
AssignmentDeclarationMembers collections.Set[*Node] // Set of detected assignment declarations
GlobalExports SymbolTable // Conditional global UMD exports
}
// SymbolTable
// type SymbolTable map[string]*Symbol
type SymbolTable interface {
Get(name string) *Symbol
Get2(name string) (*Symbol, bool)
Set(name string, symbol *Symbol)
Delete(name string)
Keys() iter.Seq[string]
Values() iter.Seq[*Symbol]
Each(func(name string, symbol *Symbol))
Iter() iter.Seq2[string, *Symbol]
Len() int
Clone() SymbolTable
Find(predicate func(*Symbol) bool) *Symbol
}
type SymbolMap struct {
m map[string]*Symbol
}
func (m *SymbolMap) Find(predicate func(*Symbol) bool) *Symbol {
for _, symbol := range m.m {
if predicate(symbol) {
return symbol
}
}
return nil
}
func (m *SymbolMap) Clone() SymbolTable {
return &SymbolMap{m: maps.Clone(m.m)}
}
func (m *SymbolMap) Len() int {
return len(m.m)
}
func (m *SymbolMap) Iter() iter.Seq2[string, *Symbol] {
return func(yield func(string, *Symbol) bool) {
for name, symbol := range m.m {
if !yield(name, symbol) {
return
}
}
}
}
func (m *SymbolMap) Get(name string) *Symbol {
return m.m[name]
}
func (m *SymbolMap) Get2(name string) (*Symbol, bool) {
symbol, ok := m.m[name]
return symbol, ok
}
func (m *SymbolMap) Set(name string, symbol *Symbol) {
m.m[name] = symbol
}
func (m *SymbolMap) Delete(name string) {
delete(m.m, name)
}
func (m *SymbolMap) Keys() iter.Seq[string] {
return func(yield func(string) bool) {
for name := range m.m {
if !yield(name) {
return
}
}
}
}
func (m *SymbolMap) Values() iter.Seq[*Symbol] {
return func(yield func(*Symbol) bool) {
for _, symbol := range m.m {
if !yield(symbol) {
return
}
}
}
}
func (m *SymbolMap) Each(fn func(name string, symbol *Symbol)) {
for name, symbol := range m.m {
fn(name, symbol)
}
}
func NewSymbolTable() SymbolTable {
return &SymbolMap{m: make(map[string]*Symbol)}
}
func NewSymbolTableWithCapacity(capacity int) SymbolTable {
return &SymbolMap{m: make(map[string]*Symbol, capacity)}
}
func NewSymbolTableFromMap(m map[string]*Symbol) SymbolTable {
return &SymbolMap{m: m}
}
const InternalSymbolNamePrefix = "\xFE" // Invalid UTF8 sequence, will never occur as IdentifierName
const (
InternalSymbolNameCall = InternalSymbolNamePrefix + "call" // Call signatures
InternalSymbolNameConstructor = InternalSymbolNamePrefix + "constructor" // Constructor implementations
InternalSymbolNameNew = InternalSymbolNamePrefix + "new" // Constructor signatures
InternalSymbolNameIndex = InternalSymbolNamePrefix + "index" // Index signatures
InternalSymbolNameExportStar = InternalSymbolNamePrefix + "export" // Module export * declarations
InternalSymbolNameGlobal = InternalSymbolNamePrefix + "global" // Global self-reference
InternalSymbolNameMissing = InternalSymbolNamePrefix + "missing" // Indicates missing symbol
InternalSymbolNameType = InternalSymbolNamePrefix + "type" // Anonymous type literal symbol
InternalSymbolNameObject = InternalSymbolNamePrefix + "object" // Anonymous object literal declaration
InternalSymbolNameJSXAttributes = InternalSymbolNamePrefix + "jsxAttributes" // Anonymous JSX attributes object literal declaration
InternalSymbolNameClass = InternalSymbolNamePrefix + "class" // Unnamed class expression
InternalSymbolNameFunction = InternalSymbolNamePrefix + "function" // Unnamed function expression
InternalSymbolNameComputed = InternalSymbolNamePrefix + "computed" // Computed property name declaration with dynamic name
InternalSymbolNameResolving = InternalSymbolNamePrefix + "resolving" // Indicator symbol used to mark partially resolved type aliases
InternalSymbolNameInstantiationExpression = InternalSymbolNamePrefix + "instantiationExpression" // Instantiation expressions
InternalSymbolNameImportAttributes = InternalSymbolNamePrefix + "importAttributes"
InternalSymbolNameExportEquals = "export=" // Export assignment symbol
InternalSymbolNameDefault = "default" // Default export symbol (technically not wholly internal, but included here for usability)
InternalSymbolNameThis = "this"
InternalSymbolNameModuleExports = "module.exports"
)
func SymbolName(symbol *Symbol) string {
if symbol.ValueDeclaration != nil && IsPrivateIdentifierClassElementDeclaration(symbol.ValueDeclaration) {
return symbol.ValueDeclaration.Name().Text()
}
return symbol.Name
}
type CombinedSymbolTable struct {
firstTable SymbolTable
secondTable SymbolTable
}
// Clone implements SymbolTable.
func (c *CombinedSymbolTable) Clone() SymbolTable {
return &CombinedSymbolTable{
firstTable: c.firstTable.Clone(),
secondTable: c.secondTable.Clone(),
}
}
// Delete implements SymbolTable.
func (c *CombinedSymbolTable) Delete(name string) {
if c.firstTable.Get(name) != nil {
c.firstTable.Delete(name)
} else {
c.secondTable.Delete(name)
}
}
// Each implements SymbolTable.
func (c *CombinedSymbolTable) Each(fn func(name string, symbol *Symbol)) {
c.firstTable.Each(func(name string, symbol *Symbol) {
fn(name, symbol)
})
c.secondTable.Each(func(name string, symbol *Symbol) {
fn(name, symbol)
})
}
// Find implements SymbolTable.
func (c *CombinedSymbolTable) Find(predicate func(*Symbol) bool) *Symbol {
ret := c.firstTable.Find(predicate)
if ret != nil {
return ret
}
return c.secondTable.Find(predicate)
}
// Get implements SymbolTable.
func (c *CombinedSymbolTable) Get(name string) *Symbol {
ret := c.firstTable.Get(name)
if ret != nil {
return ret
}
return c.secondTable.Get(name)
}
// Get2 implements SymbolTable.
func (c *CombinedSymbolTable) Get2(name string) (*Symbol, bool) {
if value, ok := c.firstTable.Get2(name); ok {
return value, ok
}
return c.secondTable.Get2(name)
}
// Iter implements SymbolTable.
func (c *CombinedSymbolTable) Iter() iter.Seq2[string, *Symbol] {
seen := make(map[string]struct{})
return func(yield func(string, *Symbol) bool) {
for name, symbol := range c.firstTable.Iter() {
if _, ok := seen[name]; !ok {
seen[name] = struct{}{}
if !yield(name, symbol) {
break
}
}
}
for name, symbol := range c.secondTable.Iter() {
if _, ok := seen[name]; !ok {
seen[name] = struct{}{}
if !yield(name, symbol) {
return
}
}
}
}
}
// Keys implements SymbolTable.
func (c *CombinedSymbolTable) Keys() iter.Seq[string] {
return func(yield func(string) bool) {
seen := make(map[string]struct{})
for name := range c.firstTable.Keys() {
if _, ok := seen[name]; !ok {
seen[name] = struct{}{}
if !yield(name) {
break
}
}
}
for name := range c.secondTable.Keys() {
if _, ok := seen[name]; !ok {
seen[name] = struct{}{}
if !yield(name) {
return
}
}
}
}
}
// Len implements SymbolTable.
func (c *CombinedSymbolTable) Len() int {
len := 0
for k := range c.Iter() {
_ = k
len++
}
return len
}
// Set implements SymbolTable.
func (c *CombinedSymbolTable) Set(name string, symbol *Symbol) {
c.firstTable.Set(name, symbol)
}
// Values implements SymbolTable.
func (c *CombinedSymbolTable) Values() iter.Seq[*Symbol] {
return func(yield func(*Symbol) bool) {
c.Iter()(func(name string, symbol *Symbol) bool {
return yield(symbol)
})
}
}
var _ SymbolTable = (*CombinedSymbolTable)(nil)
type DenoForkContextInfo struct {
TypesNodeIgnorableNames *collections.Set[string]
NodeOnlyGlobalNames *collections.Set[string]
}
type DenoForkContext struct {
globals SymbolTable
nodeGlobals SymbolTable
combinedGlobals SymbolTable
mergeSymbol func(target *Symbol, source *Symbol, unidirectional bool) *Symbol
getMergedSymbol func(source *Symbol) *Symbol
isNodeSourceFile func(path tspath.Path) bool
info DenoForkContextInfo
}
func NewDenoForkContext(
globals SymbolTable,
nodeGlobals SymbolTable,
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,
nodeGlobals: nodeGlobals,
combinedGlobals: &CombinedSymbolTable{
firstTable: nodeGlobals,
secondTable: globals,
},
mergeSymbol: mergeSymbol,
getMergedSymbol: getMergedSymbol,
isNodeSourceFile: isNodeSourceFile,
info: info,
}
}
func (c *DenoForkContext) GetGlobalsForName(name string) SymbolTable {
if c.info.NodeOnlyGlobalNames.Has(name) {
return c.nodeGlobals
} else {
return c.globals
}
}
func isTypesNodePkgPath(path tspath.Path) bool {
return strings.HasSuffix(string(path), ".d.ts") && strings.Contains(string(path), "/@types/node/")
}
func symbolHasAnyTypesNodePkgDecl(symbol *Symbol, hasNodeSourceFile func(*Node) bool) bool {
if symbol == nil || symbol.Declarations == nil {
return false
}
for _, decl := range symbol.Declarations {
sourceFile := GetSourceFileOfNode(decl)
if sourceFile != nil && hasNodeSourceFile(decl) && isTypesNodePkgPath(sourceFile.Path()) {
return true
}
}
return false
}
func (c *DenoForkContext) MergeGlobalSymbolTable(node *Node, source SymbolTable, unidirectional bool) {
sourceFile := GetSourceFileOfNode(node)
isNodeFile := c.HasNodeSourceFile(node)
isTypesNodeSourceFile := isNodeFile && isTypesNodePkgPath(sourceFile.Path())
for id, sourceSymbol := range source.Iter() {
var target SymbolTable
if isNodeFile {
target = c.GetGlobalsForName(id)
} else {
target = c.globals
}
targetSymbol := target.Get(id)
if isTypesNodeSourceFile {
}
if isTypesNodeSourceFile && targetSymbol != nil && c.info.TypesNodeIgnorableNames.Has(id) && !symbolHasAnyTypesNodePkgDecl(targetSymbol, c.HasNodeSourceFile) {
continue
}
var merged *Symbol
if targetSymbol != nil {
merged = c.mergeSymbol(targetSymbol, sourceSymbol, unidirectional)
} else {
merged = c.getMergedSymbol(sourceSymbol)
}
target.Set(id, merged)
}
}
func (c *DenoForkContext) CombinedGlobals() SymbolTable {
return c.combinedGlobals
}
func (c *DenoForkContext) HasNodeSourceFile(node *Node) bool {
if node == nil || c == nil || c.isNodeSourceFile == nil {
return false
}
sourceFile := GetSourceFileOfNode(node)
if sourceFile == nil {
return false
}
if c.isNodeSourceFile(sourceFile.Path()) {
return true
}
return false
}