MCPcopy Create free account
hub / github.com/microsoft/typescript-go / GetNodeAtPosition

Function GetNodeAtPosition

internal/ast/utilities.go:2637–2663  ·  view source on GitHub ↗

Returns a token if position is in [start-of-leading-trivia, end), includes JSDoc only if requested

(file *SourceFile, position int, includeJSDoc bool)

Source from the content-addressed store, hash-verified

2635
2636// Returns a token if position is in [start-of-leading-trivia, end), includes JSDoc only if requested
2637func GetNodeAtPosition(file *SourceFile, position int, includeJSDoc bool) *Node {
2638 current := file.AsNode()
2639 for {
2640 var child *Node
2641 if includeJSDoc {
2642 for _, jsdoc := range current.JSDoc(file) {
2643 if nodeContainsPosition(jsdoc, position) {
2644 child = jsdoc
2645 break
2646 }
2647 }
2648 }
2649 if child == nil {
2650 current.ForEachChild(func(node *Node) bool {
2651 if nodeContainsPosition(node, position) {
2652 child = node
2653 return true
2654 }
2655 return false
2656 })
2657 }
2658 if child == nil || IsMetaProperty(child) {
2659 return current
2660 }
2661 current = child
2662 }
2663}
2664
2665func nodeContainsPosition(node *Node, position int) bool {
2666 return node.Kind >= KindFirstNode && node.Pos() <= position && (position < node.End() || position == node.End() && node.Kind == KindEndOfFile)

Callers 2

getSymbolAtLocationMethod · 0.92

Calls 5

nodeContainsPositionFunction · 0.85
IsMetaPropertyFunction · 0.85
JSDocMethod · 0.80
AsNodeMethod · 0.65
ForEachChildMethod · 0.65

Tested by

no test coverage detected