Skip to content

Commit af26984

Browse files
author
Andy Hanson
committed
Fix comment parsing bug -- back up after seeing @ character
1 parent 4ab446c commit af26984

25 files changed

+77
-93
lines changed

src/compiler/parser.ts

+5-11
Original file line numberDiff line numberDiff line change
@@ -6268,7 +6268,6 @@ namespace ts {
62686268
scanner.scanRange(start + 3, length - 5, () => {
62696269
// Initially we can parse out a tag. We also have seen a starting asterisk.
62706270
// This is so that /** * @type */ doesn't parse.
6271-
let advanceToken = true;
62726271
let state = JSDocState.SawAsterisk;
62736272
let margin: number | undefined = undefined;
62746273
// + 4 for leading '/** '
@@ -6300,7 +6299,6 @@ namespace ts {
63006299
// Real-world comments may break this rule, so "BeginningOfLine" will not be a real line beginning
63016300
// for malformed examples like `/** @param {string} x @returns {number} the length */`
63026301
state = JSDocState.BeginningOfLine;
6303-
advanceToken = false;
63046302
margin = undefined;
63056303
indent++;
63066304
}
@@ -6352,13 +6350,7 @@ namespace ts {
63526350
pushComment(scanner.getTokenText());
63536351
break;
63546352
}
6355-
if (advanceToken) {
6356-
t = nextJSDocToken();
6357-
}
6358-
else {
6359-
advanceToken = true;
6360-
t = currentToken as JsDocSyntaxKind;
6361-
}
6353+
t = nextJSDocToken();
63626354
}
63636355
removeLeadingNewlines(comments);
63646356
removeTrailingNewlines(comments);
@@ -6458,7 +6450,7 @@ namespace ts {
64586450
addTag(tag);
64596451
}
64606452

6461-
function parseTagComments(indent: number): string {
6453+
function parseTagComments(indent: number): string | undefined {
64626454
const comments: string[] = [];
64636455
let state = JSDocState.BeginningOfLine;
64646456
let margin: number | undefined;
@@ -6480,6 +6472,8 @@ namespace ts {
64806472
indent = 0;
64816473
break;
64826474
case SyntaxKind.AtToken:
6475+
scanner.setTextPos(scanner.getTextPos() - 1);
6476+
// falls through
64836477
case SyntaxKind.EndOfFileToken:
64846478
// Done
64856479
break loop;
@@ -6515,7 +6509,7 @@ namespace ts {
65156509

65166510
removeLeadingNewlines(comments);
65176511
removeTrailingNewlines(comments);
6518-
return comments.join("");
6512+
return comments.length === 0 ? undefined : comments.join("");
65196513
}
65206514

65216515
function parseUnknownTag(atToken: AtToken, tagName: Identifier) {

src/compiler/utilities.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1614,7 +1614,7 @@ namespace ts {
16141614
(node as ModuleDeclaration).body;
16151615
}
16161616

1617-
export function getJSDocCommentsAndTags(node: Node): (JSDoc | JSDocTag)[] {
1617+
export function getJSDocCommentsAndTags(node: Node): ReadonlyArray<JSDoc | JSDocTag> {
16181618
let result: (JSDoc | JSDocTag)[] | undefined;
16191619
getJSDocCommentsAndTagsWorker(node);
16201620
return result || emptyArray;

src/services/jsDoc.ts

+15-15
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ namespace ts.JsDoc {
5454
// from Array<T> - Array<string> and Array<number>
5555
const documentationComment: SymbolDisplayPart[] = [];
5656
forEachUnique(declarations, declaration => {
57-
for (const comment of getCommentsFromDeclaration(declaration)) {
57+
for (const { comment } of getCommentHavingNodes(declaration)) {
58+
if (comment === undefined) continue;
5859
if (documentationComment.length) {
5960
documentationComment.push(lineBreakPart());
6061
}
@@ -64,20 +65,15 @@ namespace ts.JsDoc {
6465
return documentationComment;
6566
}
6667

67-
function getCommentsFromDeclaration(declaration: Declaration): string[] {
68+
function getCommentHavingNodes(declaration: Declaration): ReadonlyArray<JSDoc | JSDocTag> {
6869
switch (declaration.kind) {
6970
case SyntaxKind.JSDocPropertyTag:
70-
return [(declaration as JSDocPropertyTag).comment];
71+
return [declaration as JSDocPropertyTag];
72+
case SyntaxKind.JSDocTypedefTag:
73+
return [(declaration as JSDocTypedefTag).parent];
7174
default:
72-
return mapDefined(getAllJSDocs(declaration), doc => doc.comment);
73-
}
74-
}
75-
76-
function getAllJSDocs(node: Node): (JSDoc | JSDocTag)[] {
77-
if (isJSDocTypedefTag(node)) {
78-
return [node.parent];
75+
return getJSDocCommentsAndTags(declaration);
7976
}
80-
return getJSDocCommentsAndTags(node);
8177
}
8278

8379
export function getJsDocTagsFromDeclarations(declarations?: Declaration[]): JSDocTagInfo[] {
@@ -91,7 +87,7 @@ namespace ts.JsDoc {
9187
return tags;
9288
}
9389

94-
function getCommentText(tag: JSDocTag): string {
90+
function getCommentText(tag: JSDocTag): string | undefined {
9591
const { comment } = tag;
9692
switch (tag.kind) {
9793
case SyntaxKind.JSDocAugmentsTag:
@@ -106,15 +102,19 @@ namespace ts.JsDoc {
106102
const { name } = tag as JSDocTypedefTag | JSDocPropertyTag | JSDocParameterTag;
107103
return name ? withNode(name) : comment;
108104
default:
109-
return comment;
105+
return comment || "";
110106
}
111107

112108
function withNode(node: Node) {
113-
return `${node.getText()} ${comment}`;
109+
return addComment(node.getText());
114110
}
115111

116112
function withList(list: NodeArray<Node>): string {
117-
return `${list.map(x => x.getText())} ${comment}`;
113+
return addComment(list.map(x => x.getText()).join(", "));
114+
}
115+
116+
function addComment(s: string) {
117+
return comment === undefined ? s : `${s} ${comment}`;
118118
}
119119
}
120120

tests/baselines/reference/JSDocParsing/DocComments.parsesCorrectly.leadingAsterisk.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727
"pos": 15,
2828
"end": 21
2929
}
30-
},
31-
"comment": ""
30+
}
3231
},
3332
"length": 1,
3433
"pos": 8,

tests/baselines/reference/JSDocParsing/DocComments.parsesCorrectly.noLeadingAsterisk.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727
"pos": 15,
2828
"end": 21
2929
}
30-
},
31-
"comment": ""
30+
}
3231
},
3332
"length": 1,
3433
"pos": 8,

tests/baselines/reference/JSDocParsing/DocComments.parsesCorrectly.noReturnType.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
"pos": 9,
1818
"end": 15,
1919
"escapedText": "return"
20-
},
21-
"comment": ""
20+
}
2221
},
2322
"length": 1,
2423
"pos": 8,

tests/baselines/reference/JSDocParsing/DocComments.parsesCorrectly.oneParamTag.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@
3535
"escapedText": "name1"
3636
},
3737
"isNameFirst": false,
38-
"isBracketed": false,
39-
"comment": ""
38+
"isBracketed": false
4039
},
4140
"length": 1,
4241
"pos": 8,

tests/baselines/reference/JSDocParsing/DocComments.parsesCorrectly.paramTagNameThenType1.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@
3535
"escapedText": "name1"
3636
},
3737
"isNameFirst": true,
38-
"isBracketed": false,
39-
"comment": ""
38+
"isBracketed": false
4039
},
4140
"length": 1,
4241
"pos": 8,

tests/baselines/reference/JSDocParsing/DocComments.parsesCorrectly.paramWithoutType.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525
"escapedText": "foo"
2626
},
2727
"isNameFirst": true,
28-
"isBracketed": false,
29-
"comment": ""
28+
"isBracketed": false
3029
},
3130
"length": 1,
3231
"pos": 8,

tests/baselines/reference/JSDocParsing/DocComments.parsesCorrectly.returnTag1.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727
"pos": 17,
2828
"end": 23
2929
}
30-
},
31-
"comment": ""
30+
}
3231
},
3332
"length": 1,
3433
"pos": 8,

tests/baselines/reference/JSDocParsing/DocComments.parsesCorrectly.returnsTag1.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727
"pos": 18,
2828
"end": 24
2929
}
30-
},
31-
"comment": ""
30+
}
3231
},
3332
"length": 1,
3433
"pos": 8,

tests/baselines/reference/JSDocParsing/DocComments.parsesCorrectly.templateTag.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@
3333
"length": 1,
3434
"pos": 18,
3535
"end": 20
36-
},
37-
"comment": ""
36+
}
3837
},
3938
"length": 1,
4039
"pos": 8,

tests/baselines/reference/JSDocParsing/DocComments.parsesCorrectly.templateTag2.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@
4444
"length": 2,
4545
"pos": 18,
4646
"end": 22
47-
},
48-
"comment": ""
47+
}
4948
},
5049
"length": 1,
5150
"pos": 8,

tests/baselines/reference/JSDocParsing/DocComments.parsesCorrectly.templateTag3.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@
4444
"length": 2,
4545
"pos": 18,
4646
"end": 23
47-
},
48-
"comment": ""
47+
}
4948
},
5049
"length": 1,
5150
"pos": 8,

tests/baselines/reference/JSDocParsing/DocComments.parsesCorrectly.templateTag4.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@
4444
"length": 2,
4545
"pos": 18,
4646
"end": 23
47-
},
48-
"comment": ""
47+
}
4948
},
5049
"length": 1,
5150
"pos": 8,

tests/baselines/reference/JSDocParsing/DocComments.parsesCorrectly.templateTag5.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@
4444
"length": 2,
4545
"pos": 18,
4646
"end": 24
47-
},
48-
"comment": ""
47+
}
4948
},
5049
"length": 1,
5150
"pos": 8,

tests/baselines/reference/JSDocParsing/DocComments.parsesCorrectly.twoParamTag2.json

+2-4
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@
3535
"escapedText": "name1"
3636
},
3737
"isNameFirst": false,
38-
"isBracketed": false,
39-
"comment": ""
38+
"isBracketed": false
4039
},
4140
"1": {
4241
"kind": "JSDocParameterTag",
@@ -70,8 +69,7 @@
7069
"escapedText": "name2"
7170
},
7271
"isNameFirst": false,
73-
"isBracketed": false,
74-
"comment": ""
72+
"isBracketed": false
7573
},
7674
"length": 2,
7775
"pos": 8,

tests/baselines/reference/JSDocParsing/DocComments.parsesCorrectly.twoParamTagOnSameLine.json

+2-4
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@
3535
"escapedText": "name1"
3636
},
3737
"isNameFirst": false,
38-
"isBracketed": false,
39-
"comment": ""
38+
"isBracketed": false
4039
},
4140
"1": {
4241
"kind": "JSDocParameterTag",
@@ -70,8 +69,7 @@
7069
"escapedText": "name2"
7170
},
7271
"isNameFirst": false,
73-
"isBracketed": false,
74-
"comment": ""
72+
"isBracketed": false
7573
},
7674
"length": 2,
7775
"pos": 8,

tests/baselines/reference/JSDocParsing/DocComments.parsesCorrectly.typeTag.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727
"pos": 15,
2828
"end": 21
2929
}
30-
},
31-
"comment": ""
30+
}
3231
},
3332
"length": 1,
3433
"pos": 8,

tests/baselines/reference/JSDocParsing/DocComments.parsesCorrectly.typedefTagWithChildrenTags.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,7 @@
104104
"isBracketed": false
105105
}
106106
]
107-
},
108-
"comment": ""
107+
}
109108
},
110109
"length": 1,
111110
"pos": 8,

0 commit comments

Comments
 (0)