Skip to content

Commit 1e48cbe

Browse files
authored
Fix jsdoc comment parsing initial state (microsoft#36661)
* Fix jsdoc comment parsing initial state Jsdoc comment parsing can be invoked in two modes: 1. top-level parsing, for comments not inside a tag. 2. tag parsing, for comment that occur after the semantic parts of a tag. Top-level parsing skips an initial * because it assumes that it is starting at the very beginning of a JSDoc comment. Tag parsing does not. The two modes are distinguished by an optional second parameter named `margin`. When `margin` is provided, it provides an initial indent used for comment alignment. Previously, the check for `margin` was a truthy check `if (margin)`. This check incorrectly treats `margin=""` the same as `margin=undefined`. This PR changes the check to `if (margin !== undefined)`, which correctly treats `margin=""` the same as `margin=" "`. * Fixes for broken tests 1. Use SawAsterisk start state. 2. @template needs to skip asterisk in addition to whitespace while parsing type parameter names. * undo code move
1 parent 2cc5856 commit 1e48cbe

File tree

3 files changed

+39
-30
lines changed

3 files changed

+39
-30
lines changed

src/compiler/parser.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -7017,10 +7017,12 @@ namespace ts {
70177017
comments.push(text);
70187018
indent += text.length;
70197019
}
7020-
if (initialMargin) {
7020+
if (initialMargin !== undefined) {
70217021
// jump straight to saving comments if there is some initial indentation
7022-
pushComment(initialMargin);
7023-
state = JSDocState.SavingComments;
7022+
if (initialMargin !== "") {
7023+
pushComment(initialMargin);
7024+
}
7025+
state = JSDocState.SawAsterisk;
70247026
}
70257027
let tok = token() as JSDocSyntaxKind;
70267028
loop: while (true) {
@@ -7558,7 +7560,7 @@ namespace ts {
75587560
const typeParameter = <TypeParameterDeclaration>createNode(SyntaxKind.TypeParameter);
75597561
typeParameter.name = parseJSDocIdentifierName(Diagnostics.Unexpected_token_A_type_parameter_name_was_expected_without_curly_braces);
75607562
finishNode(typeParameter);
7561-
skipWhitespace();
7563+
skipWhitespaceOrAsterisk();
75627564
typeParameters.push(typeParameter);
75637565
} while (parseOptionalJsdoc(SyntaxKind.CommaToken));
75647566

tests/baselines/reference/quickInfoDisplayPartsParameters.baseline

+25-19
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
{
33
"marker": {
44
"fileName": "/tests/cases/fourslash/quickInfoDisplayPartsParameters.ts",
5-
"position": 9
5+
"position": 33
66
},
77
"quickInfo": {
88
"kind": "function",
99
"kindModifiers": "",
1010
"textSpan": {
11-
"start": 9,
11+
"start": 33,
1212
"length": 3
1313
},
1414
"displayParts": [
@@ -153,19 +153,25 @@
153153
"kind": "keyword"
154154
}
155155
],
156-
"documentation": []
156+
"documentation": [],
157+
"tags": [
158+
{
159+
"name": "return",
160+
"text": "*crunch*"
161+
}
162+
]
157163
}
158164
},
159165
{
160166
"marker": {
161167
"fileName": "/tests/cases/fourslash/quickInfoDisplayPartsParameters.ts",
162-
"position": 13
168+
"position": 37
163169
},
164170
"quickInfo": {
165171
"kind": "parameter",
166172
"kindModifiers": "",
167173
"textSpan": {
168-
"start": 13,
174+
"start": 37,
169175
"length": 5
170176
},
171177
"displayParts": [
@@ -208,13 +214,13 @@
208214
{
209215
"marker": {
210216
"fileName": "/tests/cases/fourslash/quickInfoDisplayPartsParameters.ts",
211-
"position": 28
217+
"position": 52
212218
},
213219
"quickInfo": {
214220
"kind": "parameter",
215221
"kindModifiers": "",
216222
"textSpan": {
217-
"start": 28,
223+
"start": 52,
218224
"length": 13
219225
},
220226
"displayParts": [
@@ -257,13 +263,13 @@
257263
{
258264
"marker": {
259265
"fileName": "/tests/cases/fourslash/quickInfoDisplayPartsParameters.ts",
260-
"position": 52
266+
"position": 76
261267
},
262268
"quickInfo": {
263269
"kind": "parameter",
264270
"kindModifiers": "",
265271
"textSpan": {
266-
"start": 52,
272+
"start": 76,
267273
"length": 20
268274
},
269275
"displayParts": [
@@ -306,13 +312,13 @@
306312
{
307313
"marker": {
308314
"fileName": "/tests/cases/fourslash/quickInfoDisplayPartsParameters.ts",
309-
"position": 87
315+
"position": 111
310316
},
311317
"quickInfo": {
312318
"kind": "parameter",
313319
"kindModifiers": "",
314320
"textSpan": {
315-
"start": 87,
321+
"start": 111,
316322
"length": 9
317323
},
318324
"displayParts": [
@@ -363,13 +369,13 @@
363369
{
364370
"marker": {
365371
"fileName": "/tests/cases/fourslash/quickInfoDisplayPartsParameters.ts",
366-
"position": 114
372+
"position": 138
367373
},
368374
"quickInfo": {
369375
"kind": "parameter",
370376
"kindModifiers": "",
371377
"textSpan": {
372-
"start": 114,
378+
"start": 138,
373379
"length": 5
374380
},
375381
"displayParts": [
@@ -412,13 +418,13 @@
412418
{
413419
"marker": {
414420
"fileName": "/tests/cases/fourslash/quickInfoDisplayPartsParameters.ts",
415-
"position": 135
421+
"position": 159
416422
},
417423
"quickInfo": {
418424
"kind": "parameter",
419425
"kindModifiers": "",
420426
"textSpan": {
421-
"start": 135,
427+
"start": 159,
422428
"length": 13
423429
},
424430
"displayParts": [
@@ -461,13 +467,13 @@
461467
{
462468
"marker": {
463469
"fileName": "/tests/cases/fourslash/quickInfoDisplayPartsParameters.ts",
464-
"position": 164
470+
"position": 188
465471
},
466472
"quickInfo": {
467473
"kind": "parameter",
468474
"kindModifiers": "",
469475
"textSpan": {
470-
"start": 164,
476+
"start": 188,
471477
"length": 20
472478
},
473479
"displayParts": [
@@ -510,13 +516,13 @@
510516
{
511517
"marker": {
512518
"fileName": "/tests/cases/fourslash/quickInfoDisplayPartsParameters.ts",
513-
"position": 200
519+
"position": 224
514520
},
515521
"quickInfo": {
516522
"kind": "parameter",
517523
"kindModifiers": "",
518524
"textSpan": {
519-
"start": 200,
525+
"start": 224,
520526
"length": 9
521527
},
522528
"displayParts": [
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
/// <reference path='fourslash.ts'/>
22

3-
////function /*1*/foo(/*2*/param: string, /*3*/optionalParam?: string, /*4*/paramWithInitializer = "hello", .../*5*/restParam: string[]) {
4-
//// /*6*/param = "Hello";
5-
//// /*7*/optionalParam = "World";
6-
//// /*8*/paramWithInitializer = "Hello";
7-
//// /*9*/restParam[0] = "World";
8-
////}
3+
//// /** @return *crunch* */
4+
//// function /*1*/foo(/*2*/param: string, /*3*/optionalParam?: string, /*4*/paramWithInitializer = "hello", .../*5*/restParam: string[]) {
5+
//// /*6*/param = "Hello";
6+
//// /*7*/optionalParam = "World";
7+
//// /*8*/paramWithInitializer = "Hello";
8+
//// /*9*/restParam[0] = "World";
9+
//// }
910

10-
verify.baselineQuickInfo();
11+
verify.baselineQuickInfo();

0 commit comments

Comments
 (0)