Skip to content
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++;
}
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) {
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));
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