-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Set startPos at EOF in jsdoc token scanner so node end positions for nodes terminated at EoF are right #24184
Changes from 7 commits
02f2081
525d091
de173ee
238cb59
f3afefe
880ab4a
8eabd89
f37086d
9318641
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6481,7 +6481,25 @@ namespace ts { | |
return finishNode(result, end); | ||
} | ||
|
||
function isNextNonwhitespaceTokenEndOfFile(): boolean { | ||
// We must use infinite lookahead, as there could be any number of newlines :( | ||
while (true) { | ||
nextJSDocToken(); | ||
if (token() === SyntaxKind.EndOfFileToken) { | ||
return true; | ||
} | ||
if (!(token() === SyntaxKind.WhitespaceTrivia || token() === SyntaxKind.NewLineTrivia)) { | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
function skipWhitespace(): void { | ||
if (token() === SyntaxKind.WhitespaceTrivia || token() === SyntaxKind.NewLineTrivia) { | ||
if (lookAhead(isNextNonwhitespaceTokenEndOfFile)) { | ||
return; // Don't skip whitespace prior to EoF (or end of comment) - that shouldn't be included in any node's range | ||
} | ||
} | ||
while (token() === SyntaxKind.WhitespaceTrivia || token() === SyntaxKind.NewLineTrivia) { | ||
nextJSDocToken(); | ||
} | ||
|
@@ -6802,6 +6820,7 @@ namespace ts { | |
typedefTag.comment = parseTagComments(indent); | ||
|
||
typedefTag.typeExpression = typeExpression; | ||
let end: number; | ||
if (!typeExpression || isObjectOrObjectArrayTypeReference(typeExpression.type)) { | ||
let child: JSDocTypeTag | JSDocPropertyTag | false; | ||
let jsdocTypeLiteral: JSDocTypeLiteral; | ||
|
@@ -6830,10 +6849,12 @@ namespace ts { | |
typedefTag.typeExpression = childTypeTag && childTypeTag.typeExpression && !isObjectOrObjectArrayTypeReference(childTypeTag.typeExpression.type) ? | ||
childTypeTag.typeExpression : | ||
finishNode(jsdocTypeLiteral); | ||
end = jsdocTypeLiteral.end; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is the indent on this statement correct? It looks weird to me. |
||
} | ||
} | ||
|
||
return finishNode(typedefTag); | ||
// Only include the characters between the name end and the next token if a comment was actually parsed out - otherwise it's just whitespace | ||
return finishNode(typedefTag, end || typedefTag.comment !== undefined ? scanner.getStartPos() : (typedefTag.fullName || typedefTag.typeExpression || typedefTag.tagName).end); | ||
} | ||
|
||
function parseJSDocTypeNameWithNamespace(nested?: boolean) { | ||
|
@@ -7075,7 +7096,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(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -706,16 +706,17 @@ namespace ts { | |
break; | ||
case SyntaxKind.JSDocTemplateTag: | ||
processJSDocTemplateTag(<JSDocTemplateTag>tag); | ||
pos = tag.end; | ||
break; | ||
case SyntaxKind.JSDocTypeTag: | ||
processElement((<JSDocTypeTag>tag).typeExpression); | ||
pos = tag.end; | ||
break; | ||
case SyntaxKind.JSDocReturnTag: | ||
processElement((<JSDocReturnTag>tag).typeExpression); | ||
pos = tag.end; | ||
break; | ||
} | ||
|
||
pos = tag.end; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what prompts this change? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's a test expecting a single classification for the trailing whitespace and There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. /**
* @param x Comment starts here
* - indented
* - indented
*/ Needs to produce a comment text that looks like
That is, the second lines need to be indented two spaces, not 0 and not 17 and not 16. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But... why are we doing that indentation analysis with token scanning and not with string manipulation on the actual comment text? We already manipulate it to strip leading and trailing newlines. |
||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
"0": { | ||
"kind": "JSDocReturnTag", | ||
"pos": 8, | ||
"end": 16, | ||
"end": 15, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
@@ -21,6 +21,6 @@ | |
}, | ||
"length": 1, | ||
"pos": 8, | ||
"end": 16 | ||
"end": 15 | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
"0": { | ||
"kind": "JSDocParameterTag", | ||
"pos": 8, | ||
"end": 62, | ||
"end": 64, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Per your other comment - why does this one get longer then? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
@@ -40,6 +40,6 @@ | |
}, | ||
"length": 1, | ||
"pos": 8, | ||
"end": 62 | ||
"end": 64 | ||
} | ||
} |
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.
does this quirk apply to callback tags too?
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.
From our discussion: yes, and nested
@param
types too, but the problem isn't bad enough to fix right now. Instead we should fix it by fixing the jsdoc scanner to ignore whitespace (and change the way we generate tag descriptions).