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 2 commits into
base: main
Choose a base branch
from

Conversation

a-tarasyuk
Copy link
Contributor

@a-tarasyuk a-tarasyuk commented Jul 3, 2025

@@ -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...

@gabritto gabritto requested a review from iisaduan July 3, 2025 16:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Find-all-references includes all trivia
2 participants