Skip to content

Commit ca380af

Browse files
authored
At starts jsdoc tag only after whitespace (microsoft#42364)
* Update tests * @ begins JSDoc tag only after whitespace Previously, inside a JSDoc tag's comment, @ would start a tag unless it was surrounded by backticks. However, looking at real code showed that only whitespace-preceded uses of @ were intended to start tags.
1 parent 89c173f commit ca380af

11 files changed

+294
-14
lines changed

src/compiler/parser.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7532,6 +7532,7 @@ namespace ts {
75327532
function parseTagComments(indent: number, initialMargin?: string): string | undefined {
75337533
const comments: string[] = [];
75347534
let state = JSDocState.BeginningOfLine;
7535+
let previousWhitespace = true;
75357536
let margin: number | undefined;
75367537
function pushComment(text: string) {
75377538
if (!margin) {
@@ -7557,7 +7558,8 @@ namespace ts {
75577558
indent = 0;
75587559
break;
75597560
case SyntaxKind.AtToken:
7560-
if (state === JSDocState.SavingBackticks) {
7561+
if (state === JSDocState.SavingBackticks || !previousWhitespace && state === JSDocState.SavingComments) {
7562+
// @ doesn't start a new tag inside ``, and inside a comment, only after whitespace
75617563
comments.push(scanner.getTokenText());
75627564
break;
75637565
}
@@ -7614,6 +7616,7 @@ namespace ts {
76147616
pushComment(scanner.getTokenText());
76157617
break;
76167618
}
7619+
previousWhitespace = token() === SyntaxKind.WhitespaceTrivia;
76177620
tok = nextTokenJSDoc();
76187621
}
76197622

src/testRunner/unittests/jsDocParsing.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ namespace ts {
331331
* Comments
332332
* @author Early Close Caret > <a@b>
333333
* @author No Line Breaks:
334-
* <the.email@address> must be on the same line to parse
334+
* <the email @address> must be on the same line to parse
335335
* @author Long Comment <[email protected]> I
336336
* want to keep commenting down here, I dunno.
337337
*/`);
@@ -340,6 +340,22 @@ namespace ts {
340340
`/**
341341
* @example
342342
* Some\n\n * text\r\n * with newlines.
343+
*/`);
344+
parsesCorrectly("Chained tags, no leading whitespace", `/**@a @b @c@d*/`);
345+
parsesCorrectly("Initial star is not a tag", `/***@a*/`);
346+
parsesCorrectly("Initial star space is not a tag", `/*** @a*/`);
347+
parsesCorrectly("Initial email address is not a tag", `/**[email protected]*/`);
348+
parsesCorrectly("no space before @ is not a new tag",
349+
`/**
350+
* @param this (@is@)
351+
* @param fine its@fine
352+
@zerowidth
353+
*@singlestar
354+
**@doublestar
355+
*/`);
356+
parsesCorrectly("@@ does not start a new tag",
357+
`/**
358+
* @param this is (@@fine@@and) is one comment
343359
*/`);
344360
});
345361
});
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"kind": "JSDocComment",
3+
"pos": 0,
4+
"end": 54,
5+
"flags": "JSDoc",
6+
"modifierFlagsCache": 0,
7+
"transformFlags": 0,
8+
"tags": {
9+
"0": {
10+
"kind": "JSDocParameterTag",
11+
"pos": 7,
12+
"end": 52,
13+
"modifierFlagsCache": 0,
14+
"transformFlags": 0,
15+
"tagName": {
16+
"kind": "Identifier",
17+
"pos": 8,
18+
"end": 13,
19+
"modifierFlagsCache": 0,
20+
"transformFlags": 0,
21+
"escapedText": "param"
22+
},
23+
"comment": "is (@@fine@@and) is one comment",
24+
"name": {
25+
"kind": "Identifier",
26+
"pos": 14,
27+
"end": 18,
28+
"modifierFlagsCache": 0,
29+
"transformFlags": 0,
30+
"originalKeywordKind": "ThisKeyword",
31+
"escapedText": "this"
32+
},
33+
"isNameFirst": true,
34+
"isBracketed": false
35+
},
36+
"length": 1,
37+
"pos": 7,
38+
"end": 52,
39+
"hasTrailingComma": false,
40+
"transformFlags": 0
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
{
2+
"kind": "JSDocComment",
3+
"pos": 0,
4+
"end": 15,
5+
"flags": "JSDoc",
6+
"modifierFlagsCache": 0,
7+
"transformFlags": 0,
8+
"tags": {
9+
"0": {
10+
"kind": "JSDocTag",
11+
"pos": 3,
12+
"end": 6,
13+
"modifierFlagsCache": 0,
14+
"transformFlags": 0,
15+
"tagName": {
16+
"kind": "Identifier",
17+
"pos": 4,
18+
"end": 5,
19+
"modifierFlagsCache": 0,
20+
"transformFlags": 0,
21+
"escapedText": "a"
22+
}
23+
},
24+
"1": {
25+
"kind": "JSDocTag",
26+
"pos": 6,
27+
"end": 9,
28+
"modifierFlagsCache": 0,
29+
"transformFlags": 0,
30+
"tagName": {
31+
"kind": "Identifier",
32+
"pos": 7,
33+
"end": 8,
34+
"modifierFlagsCache": 0,
35+
"transformFlags": 0,
36+
"escapedText": "b"
37+
}
38+
},
39+
"2": {
40+
"kind": "JSDocTag",
41+
"pos": 9,
42+
"end": 11,
43+
"modifierFlagsCache": 0,
44+
"transformFlags": 0,
45+
"tagName": {
46+
"kind": "Identifier",
47+
"pos": 10,
48+
"end": 11,
49+
"modifierFlagsCache": 0,
50+
"transformFlags": 0,
51+
"escapedText": "c"
52+
}
53+
},
54+
"3": {
55+
"kind": "JSDocTag",
56+
"pos": 11,
57+
"end": 13,
58+
"modifierFlagsCache": 0,
59+
"transformFlags": 0,
60+
"tagName": {
61+
"kind": "Identifier",
62+
"pos": 12,
63+
"end": 13,
64+
"modifierFlagsCache": 0,
65+
"transformFlags": 0,
66+
"escapedText": "d"
67+
}
68+
},
69+
"length": 4,
70+
"pos": 3,
71+
"end": 13,
72+
"hasTrailingComma": false,
73+
"transformFlags": 0
74+
}
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"kind": "JSDocComment",
3+
"pos": 0,
4+
"end": 21,
5+
"flags": "JSDoc",
6+
"modifierFlagsCache": 0,
7+
"transformFlags": 0,
8+
"comment": "[email protected]"
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"kind": "JSDocComment",
3+
"pos": 0,
4+
"end": 8,
5+
"flags": "JSDoc",
6+
"modifierFlagsCache": 0,
7+
"transformFlags": 0,
8+
"comment": "*@a"
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"kind": "JSDocComment",
3+
"pos": 0,
4+
"end": 8,
5+
"flags": "JSDoc",
6+
"modifierFlagsCache": 0,
7+
"transformFlags": 0,
8+
"comment": "*@a"
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"kind": "JSDocComment",
3+
"pos": 0,
4+
"end": 9,
5+
"flags": "JSDoc",
6+
"modifierFlagsCache": 0,
7+
"transformFlags": 0,
8+
"comment": "* @a"
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"kind": "JSDocComment",
3+
"pos": 0,
4+
"end": 9,
5+
"flags": "JSDoc",
6+
"modifierFlagsCache": 0,
7+
"transformFlags": 0,
8+
"comment": "* @a"
9+
}

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"kind": "JSDocComment",
33
"pos": 0,
4-
"end": 738,
4+
"end": 739,
55
"flags": "JSDoc",
66
"modifierFlagsCache": 0,
77
"transformFlags": 0,
@@ -262,7 +262,7 @@
262262
"16": {
263263
"kind": "JSDocAuthorTag",
264264
"pos": 559,
265-
"end": 598,
265+
"end": 599,
266266
"modifierFlagsCache": 0,
267267
"transformFlags": 0,
268268
"tagName": {
@@ -273,18 +273,18 @@
273273
"transformFlags": 0,
274274
"escapedText": "author"
275275
},
276-
"comment": "No Line Breaks:<the.email"
276+
"comment": "No Line Breaks:<the email"
277277
},
278278
"17": {
279279
"kind": "JSDocTag",
280-
"pos": 598,
281-
"end": 606,
280+
"pos": 599,
281+
"end": 607,
282282
"modifierFlagsCache": 0,
283283
"transformFlags": 0,
284284
"tagName": {
285285
"kind": "Identifier",
286-
"pos": 599,
287-
"end": 606,
286+
"pos": 600,
287+
"end": 607,
288288
"modifierFlagsCache": 0,
289289
"transformFlags": 0,
290290
"escapedText": "address"
@@ -293,14 +293,14 @@
293293
},
294294
"18": {
295295
"kind": "JSDocAuthorTag",
296-
"pos": 645,
297-
"end": 736,
296+
"pos": 646,
297+
"end": 737,
298298
"modifierFlagsCache": 0,
299299
"transformFlags": 0,
300300
"tagName": {
301301
"kind": "Identifier",
302-
"pos": 646,
303-
"end": 652,
302+
"pos": 647,
303+
"end": 653,
304304
"modifierFlagsCache": 0,
305305
"transformFlags": 0,
306306
"escapedText": "author"
@@ -309,7 +309,7 @@
309309
},
310310
"length": 19,
311311
"pos": 7,
312-
"end": 736,
312+
"end": 737,
313313
"hasTrailingComma": false,
314314
"transformFlags": 0
315315
}

0 commit comments

Comments
 (0)