@@ -125,10 +125,8 @@ func getTokenAtPosition(
125
125
return nodeList
126
126
}
127
127
128
- nodeVisitor := getNodeVisitor (visitNode , visitNodeList )
129
-
130
128
for {
131
- VisitEachChildAndJSDoc (current , sourceFile , nodeVisitor )
129
+ VisitEachChildAndJSDoc (current , sourceFile , visitNode , visitNodeList )
132
130
// If prevSubtree was set on the last iteration, it ends at the target position.
133
131
// Check if the rightmost token of prevSubtree should be returned based on the
134
132
// `includePrecedingTokenAtEndPosition` callback.
@@ -146,7 +144,7 @@ func getTokenAtPosition(
146
144
// we can in the AST. We've either found a token, or we need to run the scanner
147
145
// to construct one that isn't stored in the AST.
148
146
if next == nil {
149
- if ast .IsTokenKind (current .Kind ) || ast . IsJSDocCommentContainingNode (current ) {
147
+ if ast .IsTokenKind (current .Kind ) || shouldSkipChild (current ) {
150
148
return current
151
149
}
152
150
scanner := scanner .GetScannerForSourceFile (sourceFile , left )
@@ -217,7 +215,13 @@ func findRightmostNode(node *ast.Node) *ast.Node {
217
215
}
218
216
}
219
217
220
- func VisitEachChildAndJSDoc (node * ast.Node , sourceFile * ast.SourceFile , visitor * ast.NodeVisitor ) {
218
+ func VisitEachChildAndJSDoc (
219
+ node * ast.Node ,
220
+ sourceFile * ast.SourceFile ,
221
+ visitNode func (* ast.Node , * ast.NodeVisitor ) * ast.Node ,
222
+ visitNodes func (* ast.NodeList , * ast.NodeVisitor ) * ast.NodeList ,
223
+ ) {
224
+ visitor := getNodeVisitor (visitNode , visitNodes )
221
225
if node .Flags & ast .NodeFlagsHasJSDoc != 0 {
222
226
for _ , jsdoc := range node .JSDoc (sourceFile ) {
223
227
if visitor .Hooks .VisitNode != nil {
@@ -275,9 +279,6 @@ func FindPrecedingTokenEx(sourceFile *ast.SourceFile, position int, startNode *a
275
279
}
276
280
if nodeList != nil && len (nodeList .Nodes ) > 0 {
277
281
nodes := nodeList .Nodes
278
- if ast .IsJSDocSingleCommentNodeList (n , nodeList ) {
279
- return nodeList
280
- }
281
282
index , match := core .BinarySearchUniqueFunc (nodes , func (middle int , _ * ast.Node ) int {
282
283
// synthetic jsdoc nodes should have jsdocNode.End() <= n.Pos()
283
284
if nodes [middle ].Flags & ast .NodeFlagsReparsed != 0 {
@@ -308,8 +309,7 @@ func FindPrecedingTokenEx(sourceFile *ast.SourceFile, position int, startNode *a
308
309
}
309
310
return nodeList
310
311
}
311
- nodeVisitor := getNodeVisitor (visitNode , visitNodes )
312
- VisitEachChildAndJSDoc (n , sourceFile , nodeVisitor )
312
+ VisitEachChildAndJSDoc (n , sourceFile , visitNode , visitNodes )
313
313
314
314
if foundChild != nil {
315
315
// Note that the span of a node's tokens is [getStartOfNode(node, ...), node.end).
@@ -420,9 +420,6 @@ func findRightmostValidToken(endPos int, sourceFile *ast.SourceFile, containingN
420
420
}
421
421
visitNodes := func (nodeList * ast.NodeList , _ * ast.NodeVisitor ) * ast.NodeList {
422
422
if nodeList != nil && len (nodeList .Nodes ) > 0 {
423
- if ast .IsJSDocSingleCommentNodeList (n , nodeList ) {
424
- return nodeList
425
- }
426
423
hasChildren = true
427
424
index , _ := core .BinarySearchUniqueFunc (nodeList .Nodes , func (middle int , node * ast.Node ) int {
428
425
if node .End () > endPos {
@@ -450,16 +447,15 @@ func findRightmostValidToken(endPos int, sourceFile *ast.SourceFile, containingN
450
447
}
451
448
return nodeList
452
449
}
453
- nodeVisitor := getNodeVisitor (visitNode , visitNodes )
454
- VisitEachChildAndJSDoc (n , sourceFile , nodeVisitor )
450
+ VisitEachChildAndJSDoc (n , sourceFile , visitNode , visitNodes )
455
451
456
452
// Three cases:
457
453
// 1. The answer is a token of `rightmostValidNode`.
458
454
// 2. The answer is one of the unvisited tokens that occur after the rightmost valid node.
459
455
// 3. The current node is a childless, token-less node. The answer is the current node.
460
456
461
457
// Case 2: Look at unvisited trailing tokens that occur in between the rightmost visited nodes.
462
- if ! ast . IsJSDocCommentContainingNode (n ) { // JSDoc nodes don't include trivia tokens as children.
458
+ if ! shouldSkipChild (n ) { // JSDoc nodes don't include trivia tokens as children.
463
459
var startPos int
464
460
if rightmostValidNode != nil {
465
461
startPos = rightmostValidNode .End ()
@@ -563,8 +559,7 @@ func FindNextToken(previousToken *ast.Node, parent *ast.Node, file *ast.SourceFi
563
559
}
564
560
return nodeList
565
561
}
566
- nodeVisitor := getNodeVisitor (visitNode , visitNodes )
567
- VisitEachChildAndJSDoc (n , file , nodeVisitor )
562
+ VisitEachChildAndJSDoc (n , file , visitNode , visitNodes )
568
563
// Cases:
569
564
// 1. no answer exists
570
565
// 2. answer is an unvisited token
@@ -597,15 +592,44 @@ func getNodeVisitor(
597
592
visitNode func (* ast.Node , * ast.NodeVisitor ) * ast.Node ,
598
593
visitNodes func (* ast.NodeList , * ast.NodeVisitor ) * ast.NodeList ,
599
594
) * ast.NodeVisitor {
595
+ var wrappedVisitNode func (* ast.Node , * ast.NodeVisitor ) * ast.Node
596
+ var wrappedVisitNodes func (* ast.NodeList , * ast.NodeVisitor ) * ast.NodeList
597
+ if visitNode != nil {
598
+ wrappedVisitNode = func (n * ast.Node , v * ast.NodeVisitor ) * ast.Node {
599
+ if ast .IsJSDocSingleCommentNodeComment (n ) {
600
+ return n
601
+ }
602
+ return visitNode (n , v )
603
+ }
604
+ }
605
+
606
+ if visitNodes != nil {
607
+ wrappedVisitNodes = func (n * ast.NodeList , v * ast.NodeVisitor ) * ast.NodeList {
608
+ if ast .IsJSDocSingleCommentNodeList (n ) {
609
+ return n
610
+ }
611
+ return visitNodes (n , v )
612
+ }
613
+ }
614
+
600
615
return ast .NewNodeVisitor (core .Identity , nil , ast.NodeVisitorHooks {
601
- VisitNode : visitNode ,
602
- VisitToken : visitNode ,
603
- VisitNodes : visitNodes ,
616
+ VisitNode : wrappedVisitNode ,
617
+ VisitToken : wrappedVisitNode ,
618
+ VisitNodes : wrappedVisitNodes ,
604
619
VisitModifiers : func (modifiers * ast.ModifierList , visitor * ast.NodeVisitor ) * ast.ModifierList {
605
620
if modifiers != nil {
606
- visitNodes (& modifiers .NodeList , visitor )
621
+ wrappedVisitNodes (& modifiers .NodeList , visitor )
607
622
}
608
623
return modifiers
609
624
},
610
625
})
611
626
}
627
+
628
+ func shouldSkipChild (node * ast.Node ) bool {
629
+ return node .Kind == ast .KindJSDoc ||
630
+ node .Kind == ast .KindJSDocText ||
631
+ node .Kind == ast .KindJSDocTypeLiteral ||
632
+ node .Kind == ast .KindJSDocSignature ||
633
+ ast .IsJSDocLinkLike (node ) ||
634
+ ast .IsJSDocTag (node )
635
+ }
0 commit comments