Skip to content
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

Truncating lineText in references #51137

Closed
wants to merge 1 commit into from
Closed

Truncating lineText in references #51137

wants to merge 1 commit into from

Conversation

navya9singh
Copy link
Member

Fixes #50134

@typescript-bot typescript-bot added Author: Team For Milestone Bug PRs that fix a bug with a specific milestone labels Oct 10, 2022
Copy link
Member

@amcasey amcasey left a comment

Choose a reason for hiding this comment

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

Added some notes. Let me know if you'd like to discuss offline.

@@ -3522,7 +3522,26 @@ namespace ts.server {
const scriptInfo = Debug.checkDefined(projectService.getScriptInfo(fileName));
const span = toProtocolTextSpanWithContext(textSpan, contextSpan, scriptInfo);
const lineSpan = scriptInfo.lineToTextSpan(span.start.line - 1);
const lineText = scriptInfo.getSnapshot().getText(lineSpan.start, textSpanEnd(lineSpan)).replace(/\r|\n/g, "");
if (lineSpan.length > 1024) {
var start = (span.start.offset > 500) ? (span.start.offset - 501 + lineSpan.start) : lineSpan.start;
Copy link
Member

Choose a reason for hiding this comment

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

We never use var because it has weird scoping (e.g. lineText below is visible outside the if block).

@@ -3522,7 +3522,26 @@ namespace ts.server {
const scriptInfo = Debug.checkDefined(projectService.getScriptInfo(fileName));
const span = toProtocolTextSpanWithContext(textSpan, contextSpan, scriptInfo);
const lineSpan = scriptInfo.lineToTextSpan(span.start.line - 1);
const lineText = scriptInfo.getSnapshot().getText(lineSpan.start, textSpanEnd(lineSpan)).replace(/\r|\n/g, "");
if (lineSpan.length > 1024) {
Copy link
Member

Choose a reason for hiding this comment

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

Since 1024 is just a number I made up, it may need to be tuned. That would be easier if it were a constant.

Choose a reason for hiding this comment

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

To avoid using a magic number, it would be better to create a new variable with a proper name and store 1024 into it.

const lineText = scriptInfo.getSnapshot().getText(lineSpan.start, textSpanEnd(lineSpan)).replace(/\r|\n/g, "");
if (lineSpan.length > 1024) {
var start = (span.start.offset > 500) ? (span.start.offset - 501 + lineSpan.start) : lineSpan.start;
var end = (lineSpan.length -2 - span.end.offset > 500) ? (span.end.offset + 499 + lineSpan.start) : ts.textSpanEnd(lineSpan)-2; // need to ask why length is 2 more
Copy link
Member

Choose a reason for hiding this comment

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

I probably said "500", but I meant "half of the max length" (easy, if you have a constant for the max length). Also, you may discover after playing with it, that you want less text before and more text after the reference.

Copy link
Member

Choose a reason for hiding this comment

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

Usually when we subtract two from the length of a line, it's for \r and \n, but line endings vary by platform, so it's usually necessary to use a helper (we probably have one in core.ts or utilities.ts.

let tokenAtPosition = getTokenAtPosition(sourceFile, start);
let tokenAtPositionEnd = getTokenAtPosition(sourceFile, end - 1);
if (tokenAtPosition.getStart() < start) {
const nextToken = findNextToken(tokenAtPosition, tokenAtPosition.parent, sourceFile)?.getStart()!;
Copy link
Member

Choose a reason for hiding this comment

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

I might call this "nextTokenStart", to make it clearer that it's not a token.

Copy link
Member

Choose a reason for hiding this comment

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

Does getStart return the position before or after the leading trivia?

Copy link
Member Author

Choose a reason for hiding this comment

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

In here, getStart returns the start position of the token that was a result of findNextToken

Copy link
Member

Choose a reason for hiding this comment

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

But if the token looks like /*comment*/identifier, is getStart before or after the comment?

Copy link
Member

Choose a reason for hiding this comment

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

Also, the ?. followed by the ! is very questionable. Please handle the undefined explicitly.

Also, if you are calling getStart, feed in the optional sourceFile parameter.

let tokenAtPositionEnd = getTokenAtPosition(sourceFile, end - 1);
if (tokenAtPosition.getStart() < start) {
const nextToken = findNextToken(tokenAtPosition, tokenAtPosition.parent, sourceFile)?.getStart()!;
start = (nextToken - start) > 10 ? start : nextToken;
Copy link
Member

Choose a reason for hiding this comment

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

As this gets more polished, it might help to add comments.

const precedingToken = findPrecedingToken(end, sourceFile, tokenAtPositionEnd)?.getStart()!;
end = (end - precedingToken) > 10 ? end: precedingToken;
}
lineText = scriptInfo.getSnapshot().getText(start, end).replace(/\r|\n/g, "")!;
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if we want to add "..." or something when we truncate?

Copy link
Member

Choose a reason for hiding this comment

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

Aside from the ... issue, we have a trimString (or possibly trimStringEnd) helper which you should probably use instead.

const sourceFile = scriptInfo.getDefaultProject().getLanguageService().getProgram()!.getSourceFile(fileName)!;
let tokenAtPosition = getTokenAtPosition(sourceFile, start);
let tokenAtPositionEnd = getTokenAtPosition(sourceFile, end - 1);
if (tokenAtPosition.getStart() < start) {
Copy link
Member

Choose a reason for hiding this comment

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

feed in the optional sourceFile parameter to getStart.

start = (nextToken - start) > 10 ? start : nextToken;
}
if (tokenAtPositionEnd.getEnd() > end) {
const precedingToken = findPrecedingToken(end, sourceFile, tokenAtPositionEnd)?.getStart()!;
Copy link
Member

Choose a reason for hiding this comment

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

Same comment with ?./! and the sourceFile argument to getStart

Copy link

@hossein13m hossein13m left a comment

Choose a reason for hiding this comment

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

There are conflicts which needs to be resolved for this PR to be landed

@jakebailey
Copy link
Member

Closing this; #51081 was merged instead.

@jakebailey jakebailey closed this Jan 21, 2023
@DanielRosenwasser DanielRosenwasser deleted the fix(50134) branch January 21, 2023 05:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Milestone Bug PRs that fix a bug with a specific milestone
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Truncate lineText in references responses to avoid RangeError
6 participants