-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Fix some bad jsdoc comment indent #30838
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
Changes from all commits
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 |
---|---|---|
|
@@ -6441,7 +6441,6 @@ namespace ts { | |
// for malformed examples like `/** @param {string} x @returns {number} the length */` | ||
state = JSDocState.BeginningOfLine; | ||
margin = undefined; | ||
indent++; | ||
} | ||
else { | ||
pushComment(scanner.getTokenText()); | ||
|
@@ -6536,32 +6535,38 @@ namespace ts { | |
} | ||
} | ||
|
||
function skipWhitespaceOrAsterisk(): void { | ||
function skipWhitespaceOrAsterisk(): string { | ||
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 | ||
return ""; // Don't skip whitespace prior to EoF (or end of comment) - that shouldn't be included in any node's range | ||
} | ||
} | ||
|
||
let precedingLineBreak = scanner.hasPrecedingLineBreak(); | ||
let seenLineBreak = false; | ||
let indentText = ""; | ||
while ((precedingLineBreak && token() === SyntaxKind.AsteriskToken) || token() === SyntaxKind.WhitespaceTrivia || token() === SyntaxKind.NewLineTrivia) { | ||
indentText += scanner.getTokenText(); | ||
if (token() === SyntaxKind.NewLineTrivia) { | ||
precedingLineBreak = true; | ||
seenLineBreak = true; | ||
indentText = ""; | ||
} | ||
else if (token() === SyntaxKind.AsteriskToken) { | ||
precedingLineBreak = false; | ||
} | ||
nextJSDocToken(); | ||
} | ||
return seenLineBreak ? indentText : ""; | ||
} | ||
|
||
function parseTag(indent: number) { | ||
function parseTag(margin: number) { | ||
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. margin is the thing that's fixed, whereas indent goes up for each line. So this name was wrong before. |
||
Debug.assert(token() === SyntaxKind.AtToken); | ||
const start = scanner.getTokenPos(); | ||
nextJSDocToken(); | ||
|
||
const tagName = parseJSDocIdentifierName(/*message*/ undefined); | ||
skipWhitespaceOrAsterisk(); | ||
const indentText = skipWhitespaceOrAsterisk(); | ||
|
||
let tag: JSDocTag | undefined; | ||
switch (tagName.escapedText) { | ||
|
@@ -6582,7 +6587,7 @@ namespace ts { | |
case "arg": | ||
case "argument": | ||
case "param": | ||
return parseParameterOrPropertyTag(start, tagName, PropertyLikeParse.Parameter, indent); | ||
return parseParameterOrPropertyTag(start, tagName, PropertyLikeParse.Parameter, margin); | ||
case "return": | ||
case "returns": | ||
tag = parseReturnTag(start, tagName); | ||
|
@@ -6594,10 +6599,10 @@ namespace ts { | |
tag = parseTypeTag(start, tagName); | ||
break; | ||
case "typedef": | ||
tag = parseTypedefTag(start, tagName, indent); | ||
tag = parseTypedefTag(start, tagName, margin); | ||
break; | ||
case "callback": | ||
tag = parseCallbackTag(start, tagName, indent); | ||
tag = parseCallbackTag(start, tagName, margin); | ||
break; | ||
default: | ||
tag = parseUnknownTag(start, tagName); | ||
|
@@ -6606,12 +6611,15 @@ namespace ts { | |
|
||
if (!tag.comment) { | ||
// some tags, like typedef and callback, have already parsed their comments earlier | ||
tag.comment = parseTagComments(indent + tag.end - tag.pos); | ||
if (!indentText) { | ||
margin += tag.end - tag.pos; | ||
} | ||
tag.comment = parseTagComments(margin, indentText.slice(margin)); | ||
} | ||
return tag; | ||
} | ||
|
||
function parseTagComments(indent: number): string | undefined { | ||
function parseTagComments(indent: number, initialMargin?: string): string | undefined { | ||
const comments: string[] = []; | ||
let state = JSDocState.BeginningOfLine; | ||
let margin: number | undefined; | ||
|
@@ -6622,6 +6630,11 @@ namespace ts { | |
comments.push(text); | ||
indent += text.length; | ||
} | ||
if (initialMargin) { | ||
// jump straight to saving comments if there is some initial indentation | ||
pushComment(initialMargin); | ||
state = JSDocState.SavingComments; | ||
} | ||
let tok = token() as JsDocSyntaxKind; | ||
loop: while (true) { | ||
switch (tok) { | ||
|
@@ -6646,7 +6659,7 @@ namespace ts { | |
const whitespace = scanner.getTokenText(); | ||
// if the whitespace crosses the margin, take only the whitespace that passes the margin | ||
if (margin !== undefined && indent + whitespace.length > margin) { | ||
comments.push(whitespace.slice(margin - indent - 1)); | ||
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. this math was also wrong before, for the same reason as the |
||
comments.push(whitespace.slice(margin - indent)); | ||
} | ||
indent += whitespace.length; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,283 @@ | ||
[ | ||
{ | ||
"marker": { | ||
"fileName": "/tests/cases/fourslash/quickInfoForJSDocUnknownTag.ts", | ||
"position": 64 | ||
}, | ||
"quickInfo": { | ||
"kind": "function", | ||
"kindModifiers": "", | ||
"textSpan": { | ||
"start": 62, | ||
"length": 3 | ||
}, | ||
"displayParts": [ | ||
{ | ||
"text": "function", | ||
"kind": "keyword" | ||
}, | ||
{ | ||
"text": " ", | ||
"kind": "space" | ||
}, | ||
{ | ||
"text": "foo", | ||
"kind": "functionName" | ||
}, | ||
{ | ||
"text": "(", | ||
"kind": "punctuation" | ||
}, | ||
{ | ||
"text": ")", | ||
"kind": "punctuation" | ||
}, | ||
{ | ||
"text": ":", | ||
"kind": "punctuation" | ||
}, | ||
{ | ||
"text": " ", | ||
"kind": "space" | ||
}, | ||
{ | ||
"text": "string", | ||
"kind": "keyword" | ||
} | ||
], | ||
"documentation": [], | ||
"tags": [ | ||
{ | ||
"name": "example", | ||
"text": "if (true) {\n foo()\n}" | ||
} | ||
] | ||
} | ||
}, | ||
{ | ||
"marker": { | ||
"fileName": "/tests/cases/fourslash/quickInfoForJSDocUnknownTag.ts", | ||
"position": 134 | ||
}, | ||
"quickInfo": { | ||
"kind": "function", | ||
"kindModifiers": "", | ||
"textSpan": { | ||
"start": 132, | ||
"length": 4 | ||
}, | ||
"displayParts": [ | ||
{ | ||
"text": "function", | ||
"kind": "keyword" | ||
}, | ||
{ | ||
"text": " ", | ||
"kind": "space" | ||
}, | ||
{ | ||
"text": "foo2", | ||
"kind": "functionName" | ||
}, | ||
{ | ||
"text": "(", | ||
"kind": "punctuation" | ||
}, | ||
{ | ||
"text": ")", | ||
"kind": "punctuation" | ||
}, | ||
{ | ||
"text": ":", | ||
"kind": "punctuation" | ||
}, | ||
{ | ||
"text": " ", | ||
"kind": "space" | ||
}, | ||
{ | ||
"text": "string", | ||
"kind": "keyword" | ||
} | ||
], | ||
"documentation": [], | ||
"tags": [ | ||
{ | ||
"name": "example", | ||
"text": "{\n foo()\n}" | ||
} | ||
] | ||
} | ||
}, | ||
{ | ||
"marker": { | ||
"fileName": "/tests/cases/fourslash/quickInfoForJSDocUnknownTag.ts", | ||
"position": 219 | ||
}, | ||
"quickInfo": { | ||
"kind": "function", | ||
"kindModifiers": "", | ||
"textSpan": { | ||
"start": 218, | ||
"length": 3 | ||
}, | ||
"displayParts": [ | ||
{ | ||
"text": "function", | ||
"kind": "keyword" | ||
}, | ||
{ | ||
"text": " ", | ||
"kind": "space" | ||
}, | ||
{ | ||
"text": "moo", | ||
"kind": "functionName" | ||
}, | ||
{ | ||
"text": "(", | ||
"kind": "punctuation" | ||
}, | ||
{ | ||
"text": ")", | ||
"kind": "punctuation" | ||
}, | ||
{ | ||
"text": ":", | ||
"kind": "punctuation" | ||
}, | ||
{ | ||
"text": " ", | ||
"kind": "space" | ||
}, | ||
{ | ||
"text": "string", | ||
"kind": "keyword" | ||
} | ||
], | ||
"documentation": [], | ||
"tags": [ | ||
{ | ||
"name": "example", | ||
"text": " x y\n 12345\n b" | ||
} | ||
] | ||
} | ||
}, | ||
{ | ||
"marker": { | ||
"fileName": "/tests/cases/fourslash/quickInfoForJSDocUnknownTag.ts", | ||
"position": 313 | ||
}, | ||
"quickInfo": { | ||
"kind": "function", | ||
"kindModifiers": "", | ||
"textSpan": { | ||
"start": 312, | ||
"length": 3 | ||
}, | ||
"displayParts": [ | ||
{ | ||
"text": "function", | ||
"kind": "keyword" | ||
}, | ||
{ | ||
"text": " ", | ||
"kind": "space" | ||
}, | ||
{ | ||
"text": "boo", | ||
"kind": "functionName" | ||
}, | ||
{ | ||
"text": "(", | ||
"kind": "punctuation" | ||
}, | ||
{ | ||
"text": ")", | ||
"kind": "punctuation" | ||
}, | ||
{ | ||
"text": ":", | ||
"kind": "punctuation" | ||
}, | ||
{ | ||
"text": " ", | ||
"kind": "space" | ||
}, | ||
{ | ||
"text": "string", | ||
"kind": "keyword" | ||
} | ||
], | ||
"documentation": [], | ||
"tags": [ | ||
{ | ||
"name": "func" | ||
}, | ||
{ | ||
"name": "example", | ||
"text": " x y\n 12345\n b" | ||
} | ||
] | ||
} | ||
}, | ||
{ | ||
"marker": { | ||
"fileName": "/tests/cases/fourslash/quickInfoForJSDocUnknownTag.ts", | ||
"position": 426 | ||
}, | ||
"quickInfo": { | ||
"kind": "function", | ||
"kindModifiers": "", | ||
"textSpan": { | ||
"start": 424, | ||
"length": 3 | ||
}, | ||
"displayParts": [ | ||
{ | ||
"text": "function", | ||
"kind": "keyword" | ||
}, | ||
{ | ||
"text": " ", | ||
"kind": "space" | ||
}, | ||
{ | ||
"text": "goo", | ||
"kind": "functionName" | ||
}, | ||
{ | ||
"text": "(", | ||
"kind": "punctuation" | ||
}, | ||
{ | ||
"text": ")", | ||
"kind": "punctuation" | ||
}, | ||
{ | ||
"text": ":", | ||
"kind": "punctuation" | ||
}, | ||
{ | ||
"text": " ", | ||
"kind": "space" | ||
}, | ||
{ | ||
"text": "string", | ||
"kind": "keyword" | ||
} | ||
], | ||
"documentation": [], | ||
"tags": [ | ||
{ | ||
"name": "func" | ||
}, | ||
{ | ||
"name": "example", | ||
"text": "x y\n12345\n b" | ||
} | ||
] | ||
} | ||
} | ||
] |
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.
this was just wrong, as far as I can tell, but only was used for tags followed by a newline:
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.
That is, all our previous tests cases looked like this:
Never like this:
so this erroneous increment was hidden by the fact that
indent
had already passedmargin
.