Skip to content

Commit 2379148

Browse files
authored
Update end position in findRightmostValidToken (#837)
1 parent 3978f50 commit 2379148

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

internal/astnav/tokens.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -418,8 +418,8 @@ func findRightmostValidToken(endPos int, sourceFile *ast.SourceFile, containingN
418418
if position == -1 {
419419
position = containingNode.End()
420420
}
421-
var find func(n *ast.Node) *ast.Node
422-
find = func(n *ast.Node) *ast.Node {
421+
var find func(n *ast.Node, endPos int) *ast.Node
422+
find = func(n *ast.Node, endPos int) *ast.Node {
423423
if n == nil {
424424
return nil
425425
}
@@ -555,10 +555,13 @@ func findRightmostValidToken(endPos int, sourceFile *ast.SourceFile, containingN
555555
return n
556556
}
557557
// Case 1: recur on rightmostValidNode.
558-
return find(rightmostValidNode)
558+
if rightmostValidNode != nil {
559+
endPos = rightmostValidNode.End()
560+
}
561+
return find(rightmostValidNode, endPos)
559562
}
560563

561-
return find(containingNode)
564+
return find(containingNode, endPos)
562565
}
563566

564567
// !!!

internal/astnav/tokens_test.go

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,15 @@ func TestFindPrecedingToken(t *testing.T) {
354354

355355
func TestUnitFindPrecedingToken(t *testing.T) {
356356
t.Parallel()
357-
fileContent := `import {
357+
testCases := []struct {
358+
name string
359+
fileContent string
360+
position int
361+
expectedKind ast.Kind
362+
}{
363+
{
364+
name: "after dot in jsdoc",
365+
fileContent: `import {
358366
CharacterCodes,
359367
compareStringsCaseInsensitive,
360368
compareStringsCaseSensitive,
@@ -399,11 +407,25 @@ backslashRegExp.
399407
*/
400408
export function isAnyDirectorySeparator(charCode: number): boolean {
401409
return charCode === CharacterCodes.slash || charCode === CharacterCodes.backslash;
402-
}`
403-
file := parser.ParseSourceFile("/file.ts", "/file.ts", fileContent, core.ScriptTargetLatest, scanner.JSDocParsingModeParseAll)
404-
position := 839
405-
token := astnav.FindPrecedingToken(file, position)
406-
assert.Equal(t, token.Kind, ast.KindDotToken)
410+
}`,
411+
position: 839,
412+
expectedKind: ast.KindDotToken,
413+
},
414+
{
415+
name: "after comma in parameter list",
416+
fileContent: `takesCb((n, s, ))`,
417+
position: 15,
418+
expectedKind: ast.KindCommaToken,
419+
},
420+
}
421+
for _, testCase := range testCases {
422+
t.Run(testCase.name, func(t *testing.T) {
423+
t.Parallel()
424+
file := parser.ParseSourceFile("/file.ts", "/file.ts", testCase.fileContent, core.ScriptTargetLatest, scanner.JSDocParsingModeParseAll)
425+
token := astnav.FindPrecedingToken(file, testCase.position)
426+
assert.Equal(t, token.Kind, testCase.expectedKind)
427+
})
428+
}
407429
}
408430

409431
func tsFindPrecedingTokens(t *testing.T, fileText string, positions []int) []*tokenInfo {

0 commit comments

Comments
 (0)