Skip to content

fix(1342): adjust node ranges to ignore trivia #1355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion internal/ls/findallreferences.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ func (l *LanguageService) convertSymbolAndEntryToLocation(s *SymbolAndEntries) [
for _, ref := range s.references {
if ref.textRange == nil {
sourceFile := ast.GetSourceFileOfNode(ref.node)
ref.textRange = l.createLspRangeFromNode(ref.node, ast.GetSourceFileOfNode(ref.node))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't tell if this is the right fix (createLspRangeFromNode -> getRangeOfNode) or if this is the right fix: https://github.com/microsoft/typescript-go/pull/1326/files#diff-4cd7fcdded6dbd561658f34fe1abb4030739555b30d90785a6ba6f08aec0d523R393

Copy link
Contributor Author

@a-tarasyuk a-tarasyuk Jul 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The proposed fix is to apply the same normalization used by ts in toReferenceEntry for non-span entries.

function getTextSpan(node: Node, sourceFile: SourceFile, endNode?: Node): TextSpan {
    let start = node.getStart(sourceFile);
    let end = (endNode || node).getEnd();
    if (isStringLiteralLike(node) && (end - start) > 2) {
        Debug.assert(endNode === undefined);
        start += 1;
        end -= 1;
    }
    if (endNode?.kind === SyntaxKind.CaseBlock) {
        end = endNode.getFullStart();
    }
    return createTextSpanFromBounds(start, end);
}

While tsgo has a similar utility, it wasn't used in the find-all-references results.

func (l *LanguageService) getRangeOfNode(node *ast.Node, sourceFile *ast.SourceFile, endNode *ast.Node) *lsproto.Range {
if sourceFile == nil {
sourceFile = ast.GetSourceFileOfNode(node)
}
start := scanner.GetTokenPosOfNode(node, sourceFile, false /*includeJsDoc*/)
end := core.IfElse(endNode != nil, endNode, node).End()
if ast.IsStringLiteralLike(node) && (end-start) > 2 {
if endNode != nil {
panic("endNode is not nil for stringLiteralLike")
}
start += 1
end -= 1
}
if endNode != nil && endNode.Kind == ast.KindCaseBlock {
end = endNode.Pos()
}
return l.createLspRangeFromBounds(start, end, sourceFile)
}

It would be helpful to see cases involving CaseBlock or StringLiteralLike...

ref.textRange = l.getRangeOfNode(ref.node, sourceFile, nil /*endNode*/)
ref.fileName = sourceFile.FileName()
}
locations = append(locations, &lsproto.Location{
Expand Down
13 changes: 13 additions & 0 deletions internal/ls/findallreferences_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,19 @@ class C extends [|/*3*/Base|] { }`,
"3": collections.NewSetFromItems("2", "3"),
},
},
{
title: "findAllRefsTrivia",
input: `export interface A {
/** Comment */
[|/*m1*/method|](): string;
/** Comment */
[|/*m2*/method|](format: string): string;
}`,
expectedLocations: map[string]*collections.Set[string]{
"m1": collections.NewSetFromItems("m1", "m2"),
"m2": collections.NewSetFromItems("m1", "m2"),
},
},
}

for _, testCase := range testCases {
Expand Down
2 changes: 1 addition & 1 deletion internal/ls/findallreferencesexport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func (l *LanguageService) GetExpectedReferenceFromMarker(fileName string, pos in
node := astnav.GetTouchingPropertyName(sourceFile, pos)
return &lsproto.Location{
Uri: FileNameToDocumentURI(fileName),
Range: *l.createLspRangeFromNode(node, sourceFile),
Range: *l.getRangeOfNode(node, sourceFile, nil /*endNode*/),
}
}

Expand Down
Loading