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

Function findEnclosingNode

internal/format/span.go:20–44  ·  view source on GitHub ↗

** find node that fully contains given text range */

(r core.TextRange, sourceFile *ast.SourceFile)

Source from the content-addressed store, hash-verified

18
19/** find node that fully contains given text range */
20func findEnclosingNode(r core.TextRange, sourceFile *ast.SourceFile) *ast.Node {
21 var find func(*ast.Node) *ast.Node
22 find = func(n *ast.Node) *ast.Node {
23 var candidate *ast.Node
24 n.ForEachChild(func(c *ast.Node) bool {
25 if c.Flags&ast.NodeFlagsReparsed != 0 {
26 return false
27 }
28 if r.ContainedBy(withTokenStart(c, sourceFile)) {
29 candidate = c
30 return true
31 }
32 return false
33 })
34 if candidate != nil {
35 result := find(candidate)
36 if result != nil {
37 return result
38 }
39 }
40
41 return n
42 }
43 return find(sourceFile.AsNode())
44}
45
46/**
47 * Start of the original range might fall inside the comment - scanner will not yield appropriate results

Callers 1

FormatSpanFunction · 0.85

Calls 5

withTokenStartFunction · 0.85
ContainedByMethod · 0.80
ForEachChildMethod · 0.65
AsNodeMethod · 0.65
findFunction · 0.50

Tested by

no test coverage detected