-
Notifications
You must be signed in to change notification settings - Fork 659
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
base: main
Are you sure you want to change the base?
Conversation
@@ -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)) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
typescript-go/internal/ls/findallreferences.go
Lines 257 to 274 in 15def02
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
...
Fixes #1342
https://github.com/microsoft/TypeScript/blob/833a8d492c728d606454865e8c0fee84842f9f10/src/services/findAllReferences.ts#L745
https://github.com/microsoft/TypeScript/blob/833a8d492c728d606454865e8c0fee84842f9f10/src/services/findAllReferences.ts#L872-L884
Screen.Recording.2025-07-03.at.17.14.13.mov