Skip to content

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

Merged
merged 5 commits into from
Apr 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 24 additions & 11 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6441,7 +6441,6 @@ namespace ts {
// for malformed examples like `/** @param {string} x @returns {number} the length */`
state = JSDocState.BeginningOfLine;
margin = undefined;
indent++;
Copy link
Member Author

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:

@ex
stuff

Copy link
Member Author

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:

@param x   comment
           comment
              indent

Never like this:

@example
comment
comment
  indent

so this erroneous increment was hidden by the fact that indent had already passed margin.

}
else {
pushComment(scanner.getTokenText());
Expand Down Expand Up @@ -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) {
Copy link
Member Author

Choose a reason for hiding this comment

The 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) {
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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) {
Expand All @@ -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));
Copy link
Member Author

Choose a reason for hiding this comment

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

this math was also wrong before, for the same reason as the indent++

comments.push(whitespace.slice(margin - indent));
}
indent += whitespace.length;
}
Expand Down
283 changes: 283 additions & 0 deletions tests/baselines/reference/quickInfoForJSDocUnknownTag.baseline
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"
}
]
}
}
]
Loading