Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions tsc/internal/astnav/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,9 @@ func getTokenAtPosition(
return nodeList
}

visitor := getNodeVisitor(visitNode, visitNodeList)
for {
VisitEachChildAndJSDoc(current, sourceFile, visitNode, visitNodeList)
visitEachChildAndJSDoc(current, sourceFile, visitor)
// If prevSubtree was set on the last iteration, it ends at the target position.
// Check if the rightmost token of prevSubtree should be returned based on the
// `includePrecedingTokenAtEndPosition` callback.
Expand Down Expand Up @@ -315,7 +316,10 @@ func VisitEachChildAndJSDoc(
visitNode func(*ast.Node, *ast.NodeVisitor) *ast.Node,
visitNodes func(*ast.NodeList, *ast.NodeVisitor) *ast.NodeList,
) {
visitor := getNodeVisitor(visitNode, visitNodes)
visitEachChildAndJSDoc(node, sourceFile, getNodeVisitor(visitNode, visitNodes))
}

func visitEachChildAndJSDoc(node *ast.Node, sourceFile *ast.SourceFile, visitor *ast.NodeVisitor) {
for _, jsdoc := range node.JSDoc(sourceFile) {
if visitor.Hooks.VisitNode != nil {
visitor.Hooks.VisitNode(jsdoc, visitor)
Expand Down
12 changes: 12 additions & 0 deletions tsc/internal/astnav/tokens_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,3 +620,15 @@ func tsFindPrecedingTokens(t *testing.T, fileText string, positions []int) []*to
assert.NilError(t, err)
return info
}

func BenchmarkGetTokenAtPositionNested(b *testing.B) {
text := "const value = " + strings.Repeat("call(", 24) + "target" + strings.Repeat(")", 24) + ";"
file := parser.ParseSourceFile(ast.SourceFileParseOptions{FileName: "/test.ts", PathKey: "/test.ts"}, text, core.ScriptKindTS)
position := strings.Index(text, "target")
b.ReportAllocs()
for b.Loop() {
if token := astnav.GetTokenAtPosition(file, position); token == nil || token.Kind != ast.KindIdentifier {
b.Fatal("target token not found")
}
}
}