Skip to content

Commit a0db193

Browse files
author
Andy Hanson
committed
Use nextToken() after parsing a tag name so we can parse type keywords
1 parent 4ac8976 commit a0db193

14 files changed

+34
-32
lines changed

src/compiler/checker.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29593,10 +29593,11 @@ namespace ts {
2959329593
}
2959429594

2959529595
function checkGrammarConstructorTypeParameters(node: ConstructorDeclaration) {
29596-
const jsdocTypeParameters = isInJavaScriptFile(node) && getJSDocTypeParameterDeclarations(node);
29597-
if (node.typeParameters || jsdocTypeParameters && jsdocTypeParameters.length) {
29598-
const { pos, end } = node.typeParameters || jsdocTypeParameters && jsdocTypeParameters[0] || node;
29599-
return grammarErrorAtPos(node, pos, end - pos, Diagnostics.Type_parameters_cannot_appear_on_a_constructor_declaration);
29596+
const jsdocTypeParameters = isInJavaScriptFile(node) ? getJSDocTypeParameterDeclarations(node) : undefined;
29597+
const range = node.typeParameters || jsdocTypeParameters && firstOrUndefined(jsdocTypeParameters);
29598+
if (range) {
29599+
const pos = getStartPositionOfRange(range, getSourceFileOfNode(node));
29600+
return grammarErrorAtPos(node, pos, range.end - pos, Diagnostics.Type_parameters_cannot_appear_on_a_constructor_declaration);
2960029601
}
2960129602
}
2960229603

src/compiler/parser.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6513,7 +6513,7 @@ namespace ts {
65136513
}
65146514
}
65156515

6516-
function skipWhitespaceOrAsterisk(): void {
6516+
function skipWhitespaceOrAsterisk(next: () => void = nextJSDocToken): void {
65176517
if (token() === SyntaxKind.WhitespaceTrivia || token() === SyntaxKind.NewLineTrivia) {
65186518
if (lookAhead(isNextNonwhitespaceTokenEndOfFile)) {
65196519
return; // Don't skip whitespace prior to EoF (or end of comment) - that shouldn't be included in any node's range
@@ -6528,7 +6528,7 @@ namespace ts {
65286528
else if (token() === SyntaxKind.AsteriskToken) {
65296529
precedingLineBreak = false;
65306530
}
6531-
nextJSDocToken();
6531+
next();
65326532
}
65336533
}
65346534

@@ -6538,8 +6538,9 @@ namespace ts {
65386538
atToken.end = scanner.getTextPos();
65396539
nextJSDocToken();
65406540

6541-
const tagName = parseJSDocIdentifierName();
6542-
skipWhitespaceOrAsterisk();
6541+
// Use 'nextToken' instead of 'nextJsDocToken' so we can parse a type like 'number' in `@enum number`
6542+
const tagName = parseJSDocIdentifierName(/*message*/ undefined, nextToken);
6543+
skipWhitespaceOrAsterisk(nextToken);
65436544

65446545
let tag: JSDocTag | undefined;
65456546
switch (tagName.escapedText) {
@@ -7113,7 +7114,7 @@ namespace ts {
71137114
return entity;
71147115
}
71157116

7116-
function parseJSDocIdentifierName(message?: DiagnosticMessage): Identifier {
7117+
function parseJSDocIdentifierName(message?: DiagnosticMessage, next: () => void = nextJSDocToken): Identifier {
71177118
if (!tokenIsIdentifierOrKeyword(token())) {
71187119
return createMissingNode<Identifier>(SyntaxKind.Identifier, /*reportAtCurrentPosition*/ !message, message || Diagnostics.Identifier_expected);
71197120
}
@@ -7124,7 +7125,7 @@ namespace ts {
71247125
result.escapedText = escapeLeadingUnderscores(scanner.getTokenText());
71257126
finishNode(result, end);
71267127

7127-
nextJSDocToken();
7128+
next();
71287129
return result;
71297130
}
71307131
}

tests/baselines/reference/JSDocParsing/DocComments.parsesCorrectly.@link tags.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"0": {
77
"kind": "JSDocTag",
88
"pos": 63,
9-
"end": 68,
9+
"end": 67,
1010
"atToken": {
1111
"kind": "AtToken",
1212
"pos": 63,
@@ -22,7 +22,7 @@
2222
},
2323
"length": 1,
2424
"pos": 63,
25-
"end": 68
25+
"end": 67
2626
},
2727
"comment": "{@link first link}\nInside {@link link text} thing"
2828
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"typeParameters": {
2222
"0": {
2323
"kind": "TypeParameter",
24-
"pos": 18,
24+
"pos": 17,
2525
"end": 19,
2626
"name": {
2727
"kind": "Identifier",
@@ -31,7 +31,7 @@
3131
}
3232
},
3333
"length": 1,
34-
"pos": 18,
34+
"pos": 17,
3535
"end": 19
3636
}
3737
},

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"typeParameters": {
2222
"0": {
2323
"kind": "TypeParameter",
24-
"pos": 18,
24+
"pos": 17,
2525
"end": 19,
2626
"name": {
2727
"kind": "Identifier",
@@ -42,7 +42,7 @@
4242
}
4343
},
4444
"length": 2,
45-
"pos": 18,
45+
"pos": 17,
4646
"end": 21
4747
}
4848
},

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"typeParameters": {
2222
"0": {
2323
"kind": "TypeParameter",
24-
"pos": 18,
24+
"pos": 17,
2525
"end": 19,
2626
"name": {
2727
"kind": "Identifier",
@@ -42,7 +42,7 @@
4242
}
4343
},
4444
"length": 2,
45-
"pos": 18,
45+
"pos": 17,
4646
"end": 22
4747
}
4848
},

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"typeParameters": {
2222
"0": {
2323
"kind": "TypeParameter",
24-
"pos": 18,
24+
"pos": 17,
2525
"end": 19,
2626
"name": {
2727
"kind": "Identifier",
@@ -42,7 +42,7 @@
4242
}
4343
},
4444
"length": 2,
45-
"pos": 18,
45+
"pos": 17,
4646
"end": 22
4747
}
4848
},

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"typeParameters": {
2222
"0": {
2323
"kind": "TypeParameter",
24-
"pos": 18,
24+
"pos": 17,
2525
"end": 19,
2626
"name": {
2727
"kind": "Identifier",
@@ -42,7 +42,7 @@
4242
}
4343
},
4444
"length": 2,
45-
"pos": 18,
45+
"pos": 17,
4646
"end": 23
4747
}
4848
},

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"typeParameters": {
2222
"0": {
2323
"kind": "TypeParameter",
24-
"pos": 18,
24+
"pos": 17,
2525
"end": 19,
2626
"name": {
2727
"kind": "Identifier",
@@ -42,7 +42,7 @@
4242
}
4343
},
4444
"length": 2,
45-
"pos": 18,
45+
"pos": 17,
4646
"end": 24
4747
},
4848
"comment": "Description of type parameters."

tests/baselines/reference/enumTag.errors.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ tests/cases/conformance/jsdoc/a.js(37,16): error TS2339: Property 'UNKNOWN' does
1515
/** @type {number} */
1616
OK_I_GUESS: 2
1717
}
18-
/** @enum {number} */
18+
/** @enum number */
1919
const Second = {
2020
MISTAKE: "end",
2121
~~~~~~~~~~~~~~

tests/baselines/reference/enumTag.symbols

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const Target = {
1919
OK_I_GUESS: 2
2020
>OK_I_GUESS : Symbol(OK_I_GUESS, Decl(a.js, 5, 15))
2121
}
22-
/** @enum {number} */
22+
/** @enum number */
2323
const Second = {
2424
>Second : Symbol(Second, Decl(a.js, 10, 5))
2525

tests/baselines/reference/enumTag.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const Target = {
2525
>OK_I_GUESS : number
2626
>2 : 2
2727
}
28-
/** @enum {number} */
28+
/** @enum number */
2929
const Second = {
3030
>Second : { MISTAKE: string; OK: number; FINE: number; }
3131
>{ MISTAKE: "end", OK: 1, /** @type {number} */ FINE: 2,} : { MISTAKE: string; OK: number; FINE: number; }

tests/baselines/reference/paramTagWrapping.errors.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
tests/cases/conformance/jsdoc/bad.js(2,11): error TS1003: Identifier expected.
2-
tests/cases/conformance/jsdoc/bad.js(2,11): error TS8024: JSDoc '@param' tag has name '', but there is no parameter with that name.
1+
tests/cases/conformance/jsdoc/bad.js(2,10): error TS1003: Identifier expected.
2+
tests/cases/conformance/jsdoc/bad.js(2,10): error TS8024: JSDoc '@param' tag has name '', but there is no parameter with that name.
33
tests/cases/conformance/jsdoc/bad.js(5,4): error TS1003: Identifier expected.
44
tests/cases/conformance/jsdoc/bad.js(5,4): error TS8024: JSDoc '@param' tag has name '', but there is no parameter with that name.
55
tests/cases/conformance/jsdoc/bad.js(6,19): error TS1003: Identifier expected.
@@ -27,9 +27,9 @@ tests/cases/conformance/jsdoc/bad.js(9,20): error TS7006: Parameter 'z' implicit
2727
==== tests/cases/conformance/jsdoc/bad.js (9 errors) ====
2828
/**
2929
* @param *
30-
30+
3131
!!! error TS1003: Identifier expected.
32-
32+
3333
!!! error TS8024: JSDoc '@param' tag has name '', but there is no parameter with that name.
3434
* {number} x Arg x.
3535
* @param {number}

tests/cases/conformance/jsdoc/enumTag.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const Target = {
1111
/** @type {number} */
1212
OK_I_GUESS: 2
1313
}
14-
/** @enum {number} */
14+
/** @enum number */
1515
const Second = {
1616
MISTAKE: "end",
1717
OK: 1,

0 commit comments

Comments
 (0)