MCPcopy Index your code
hub / github.com/microsoft/typescript-go / encodeTree

Function encodeTree

internal/api/encoder/encoder.go:416–620  ·  view source on GitHub ↗
(rootNode *ast.Node, sourceFile *ast.SourceFile)

Source from the content-addressed store, hash-verified

414}
415
416func encodeTree(rootNode *ast.Node, sourceFile *ast.SourceFile) ([]byte, *NodeIndexTable, error) {
417 var parentIndex, nodeCount, prevIndex uint32
418 var extendedData []byte
419 var structuredData []byte
420 var strs *stringTable
421 var positionMap *ast.PositionMap
422 if rootNode.Kind == ast.KindSourceFile {
423 strs = newStringTable(sourceFile.Text(), sourceFile.TextCount)
424 positionMap = sourceFile.GetPositionMap()
425 } else {
426 strs = newStringTable("", 0)
427 if sourceFile != nil {
428 positionMap = sourceFile.GetPositionMap()
429 }
430 }
431 if positionMap == nil {
432 positionMap = ast.ComputePositionMap("")
433 }
434 utf16 := func(pos int) uint32 {
435 return uint32(positionMap.UTF8ToUTF16(pos))
436 }
437 var initialNodeCount int
438 if sourceFile != nil {
439 initialNodeCount = sourceFile.NodeCount
440 }
441 nodes := make([]byte, 0, (initialNodeCount+1)*NodeSize)
442
443 // Build node index table for O(1) handle resolution.
444 // Index 0 is a nil sentinel; real nodes start at index 1.
445 nodeTable := make([]*ast.Node, 1, initialNodeCount+1) // index 0 = nil sentinel
446
447 // Build a small map of nodes we need to track indices for (imports + moduleAugmentations).
448 // Values start at 0 and are filled in during the walk.
449 var nodeIndexMap map[*ast.Node]uint32
450 var sfExtendedDataOffset int // byte offset in extendedData where SourceFile fields start
451 if rootNode.Kind == ast.KindSourceFile {
452 sf := rootNode.AsSourceFile()
453 total := len(sf.Imports()) + len(sf.ModuleAugmentations)
454 if sf.ExternalModuleIndicator != nil && sf.ExternalModuleIndicator != rootNode {
455 total++
456 }
457 if total > 0 {
458 nodeIndexMap = make(map[*ast.Node]uint32, total)
459 for _, imp := range sf.Imports() {
460 nodeIndexMap[imp.AsNode()] = 0
461 }
462 for _, aug := range sf.ModuleAugmentations {
463 nodeIndexMap[aug.AsNode()] = 0
464 }
465 if sf.ExternalModuleIndicator != nil && sf.ExternalModuleIndicator != rootNode {
466 nodeIndexMap[sf.ExternalModuleIndicator] = 0
467 }
468 }
469 }
470
471 visitor := &ast.NodeVisitor{
472 Hooks: ast.NodeVisitorHooks{
473 VisitNodes: func(nodeList *ast.NodeList, visitor *ast.NodeVisitor) *ast.NodeList {

Callers 2

EncodeSourceFileFunction · 0.85
EncodeNodeFunction · 0.85

Calls 15

UTF8ToUTF16Method · 0.95
VisitSliceMethod · 0.95
VisitEachChildMethod · 0.95
stringLengthMethod · 0.95
encodeMethod · 0.95
ComputePositionMapFunction · 0.92
newStringTableFunction · 0.85
lenFunction · 0.85
appendUint32sFunction · 0.85
encodeParseOptionsFunction · 0.85
encodeNodeIndexArrayFunction · 0.85

Tested by

no test coverage detected