Skip to content

Set startPos at EOF in jsdoc token scanner so node end positions for nodes terminated at EoF are right #24184

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

Merged
10 changes: 9 additions & 1 deletion src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6471,8 +6471,16 @@ namespace ts {
return finishNode(result, end);
}

function isNextNonwhitespaceTokenEndOfFile() {
nextJSDocToken();
return token() === SyntaxKind.EndOfFileToken || ((token() === SyntaxKind.WhitespaceTrivia || token() === SyntaxKind.NewLineTrivia) && nextJSDocToken() === SyntaxKind.EndOfFileToken);
}

function skipWhitespace(): void {
while (token() === SyntaxKind.WhitespaceTrivia || token() === SyntaxKind.NewLineTrivia) {
if (lookAhead(isNextNonwhitespaceTokenEndOfFile)) {
return; // Don't skip whitespace prior to EoF (or end of comment)
}
nextJSDocToken();
}
}
Expand Down Expand Up @@ -7020,7 +7028,7 @@ namespace ts {
const pos = scanner.getTokenPos();
const end = scanner.getTextPos();
const result = <Identifier>createNode(SyntaxKind.Identifier, pos);
result.escapedText = escapeLeadingUnderscores(content.substring(pos, end));
result.escapedText = escapeLeadingUnderscores(scanner.getTokenText());
finishNode(result, end);

nextJSDocToken();
Expand Down
4 changes: 1 addition & 3 deletions src/compiler/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1928,13 +1928,11 @@ namespace ts {
}

function scanJSDocToken(): JsDocSyntaxKind {
startPos = tokenPos = pos;
if (pos >= end) {
return token = SyntaxKind.EndOfFileToken;
}

startPos = pos;
tokenPos = pos;

const ch = text.charCodeAt(pos);
pos++;
switch (ch) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"0": {
"kind": "JSDocParameterTag",
"pos": 6,
"end": 63,
"end": 64,
"atToken": {
"kind": "AtToken",
"pos": 6,
Expand All @@ -21,11 +21,11 @@
"typeExpression": {
"kind": "JSDocTypeExpression",
"pos": 34,
"end": 63,
"end": 64,
"type": {
"kind": "JSDocTypeLiteral",
"pos": 34,
"end": 63,
"end": 64,
"jsDocPropertyTags": [
{
"kind": "JSDocParameterTag",
Expand Down Expand Up @@ -88,6 +88,6 @@
},
"length": 1,
"pos": 6,
"end": 63
"end": 64
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"0": {
"kind": "JSDocParameterTag",
"pos": 8,
"end": 40,
"end": 42,
"atToken": {
"kind": "AtToken",
"pos": 8,
Expand Down Expand Up @@ -40,6 +40,6 @@
},
"length": 1,
"pos": 8,
"end": 40
"end": 42
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"0": {
"kind": "JSDocParameterTag",
"pos": 8,
"end": 45,
"end": 47,
"atToken": {
"kind": "AtToken",
"pos": 8,
Expand Down Expand Up @@ -40,6 +40,6 @@
},
"length": 1,
"pos": 8,
"end": 45
"end": 47
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"0": {
"kind": "JSDocParameterTag",
"pos": 7,
"end": 58,
"end": 59,
"atToken": {
"kind": "AtToken",
"pos": 7,
Expand All @@ -30,6 +30,6 @@
},
"length": 1,
"pos": 7,
"end": 58
"end": 59
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"0": {
"kind": "JSDocReturnTag",
"pos": 8,
"end": 16,
"end": 15,
Copy link
Member

Choose a reason for hiding this comment

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

Odd to see all these end position shift sometimes +2, sometime +1, and sometimes -1. I assume you've done a spot check and the new positions are (hopefully more) correct in each case?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, the end just no longer includes the trailing whitespace at the end of the comment.

"atToken": {
"kind": "AtToken",
"pos": 8,
Expand All @@ -21,6 +21,6 @@
},
"length": 1,
"pos": 8,
"end": 16
"end": 15
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"0": {
"kind": "JSDocParameterTag",
"pos": 8,
"end": 30,
"end": 32,
"atToken": {
"kind": "AtToken",
"pos": 8,
Expand Down Expand Up @@ -39,6 +39,6 @@
},
"length": 1,
"pos": 8,
"end": 30
"end": 32
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"0": {
"kind": "JSDocParameterTag",
"pos": 8,
"end": 55,
"end": 57,
"atToken": {
"kind": "AtToken",
"pos": 8,
Expand Down Expand Up @@ -40,6 +40,6 @@
},
"length": 1,
"pos": 8,
"end": 55
"end": 57
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"0": {
"kind": "JSDocParameterTag",
"pos": 8,
"end": 57,
"end": 59,
"atToken": {
"kind": "AtToken",
"pos": 8,
Expand Down Expand Up @@ -40,6 +40,6 @@
},
"length": 1,
"pos": 8,
"end": 57
"end": 59
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"0": {
"kind": "JSDocParameterTag",
"pos": 8,
"end": 62,
"end": 64,
Copy link
Member

Choose a reason for hiding this comment

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

Per your other comment - why does this one get longer then?

Copy link
Member Author

Choose a reason for hiding this comment

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

Description scanning is including trailing whitespace now that the comment end token's start pos is fixed - nonfinal descriptions already had positions that included trailing whitespace like this, so.... 🤷‍♂️

TBH, I'd prefer if whitespace was nonsignificant in jsdoc parsing and only handled newlines - that's require some changes to how we handle descriptions, though (we'd need to scan them more like jsx text).

"atToken": {
"kind": "AtToken",
"pos": 8,
Expand Down Expand Up @@ -40,6 +40,6 @@
},
"length": 1,
"pos": 8,
"end": 62
"end": 64
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"0": {
"kind": "JSDocParameterTag",
"pos": 8,
"end": 42,
"end": 44,
"atToken": {
"kind": "AtToken",
"pos": 8,
Expand Down Expand Up @@ -40,6 +40,6 @@
},
"length": 1,
"pos": 8,
"end": 42
"end": 44
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"0": {
"kind": "JSDocParameterTag",
"pos": 8,
"end": 19,
"end": 21,
"atToken": {
"kind": "AtToken",
"pos": 8,
Expand All @@ -29,6 +29,6 @@
},
"length": 1,
"pos": 8,
"end": 19
"end": 21
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"0": {
"kind": "JSDocTemplateTag",
"pos": 8,
"end": 20,
"end": 19,
"atToken": {
"kind": "AtToken",
"pos": 8,
Expand All @@ -22,7 +22,7 @@
"0": {
"kind": "TypeParameter",
"pos": 18,
"end": 20,
"end": 19,
"name": {
"kind": "Identifier",
"pos": 18,
Expand All @@ -32,11 +32,11 @@
},
"length": 1,
"pos": 18,
"end": 20
"end": 19
}
},
"length": 1,
"pos": 8,
"end": 20
"end": 19
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"0": {
"kind": "JSDocTemplateTag",
"pos": 8,
"end": 22,
"end": 21,
"atToken": {
"kind": "AtToken",
"pos": 8,
Expand All @@ -33,7 +33,7 @@
"1": {
"kind": "TypeParameter",
"pos": 20,
"end": 22,
"end": 21,
"name": {
"kind": "Identifier",
"pos": 20,
Expand All @@ -43,11 +43,11 @@
},
"length": 2,
"pos": 18,
"end": 22
"end": 21
}
},
"length": 1,
"pos": 8,
"end": 22
"end": 21
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"0": {
"kind": "JSDocTemplateTag",
"pos": 8,
"end": 23,
"end": 22,
"atToken": {
"kind": "AtToken",
"pos": 8,
Expand All @@ -33,7 +33,7 @@
"1": {
"kind": "TypeParameter",
"pos": 21,
"end": 23,
"end": 22,
"name": {
"kind": "Identifier",
"pos": 21,
Expand All @@ -43,11 +43,11 @@
},
"length": 2,
"pos": 18,
"end": 23
"end": 22
}
},
"length": 1,
"pos": 8,
"end": 23
"end": 22
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"0": {
"kind": "JSDocTemplateTag",
"pos": 8,
"end": 23,
"end": 22,
"atToken": {
"kind": "AtToken",
"pos": 8,
Expand All @@ -33,7 +33,7 @@
"1": {
"kind": "TypeParameter",
"pos": 21,
"end": 23,
"end": 22,
"name": {
"kind": "Identifier",
"pos": 21,
Expand All @@ -43,11 +43,11 @@
},
"length": 2,
"pos": 18,
"end": 23
"end": 22
}
},
"length": 1,
"pos": 8,
"end": 23
"end": 22
}
}
Loading