Skip to content

Commit f71d600

Browse files
author
Andy
authored
Use nextToken() after parsing a tag name so we can parse type keywords (#26915)
* Use nextToken() after parsing a tag name so we can parse type keywords * Make callback to skipWhitespaceOrAsterisk non-optional
1 parent ebfcc1b commit f71d600

14 files changed

+37
-35
lines changed

src/compiler/checker.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29938,10 +29938,11 @@ namespace ts {
2993829938
}
2993929939

2994029940
function checkGrammarConstructorTypeParameters(node: ConstructorDeclaration) {
29941-
const jsdocTypeParameters = isInJSFile(node) && getJSDocTypeParameterDeclarations(node);
29942-
if (node.typeParameters || jsdocTypeParameters && jsdocTypeParameters.length) {
29943-
const { pos, end } = node.typeParameters || jsdocTypeParameters && jsdocTypeParameters[0] || node;
29944-
return grammarErrorAtPos(node, pos, end - pos, Diagnostics.Type_parameters_cannot_appear_on_a_constructor_declaration);
29941+
const jsdocTypeParameters = isInJSFile(node) ? getJSDocTypeParameterDeclarations(node) : undefined;
29942+
const range = node.typeParameters || jsdocTypeParameters && firstOrUndefined(jsdocTypeParameters);
29943+
if (range) {
29944+
const pos = range.pos === range.end ? range.pos : skipTrivia(getSourceFileOfNode(node).text, range.pos);
29945+
return grammarErrorAtPos(node, pos, range.end - pos, Diagnostics.Type_parameters_cannot_appear_on_a_constructor_declaration);
2994529946
}
2994629947
}
2994729948

src/compiler/parser.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6517,7 +6517,7 @@ namespace ts {
65176517
}
65186518
}
65196519

6520-
function skipWhitespaceOrAsterisk(): void {
6520+
function skipWhitespaceOrAsterisk(next: () => void): void {
65216521
if (token() === SyntaxKind.WhitespaceTrivia || token() === SyntaxKind.NewLineTrivia) {
65226522
if (lookAhead(isNextNonwhitespaceTokenEndOfFile)) {
65236523
return; // Don't skip whitespace prior to EoF (or end of comment) - that shouldn't be included in any node's range
@@ -6532,7 +6532,7 @@ namespace ts {
65326532
else if (token() === SyntaxKind.AsteriskToken) {
65336533
precedingLineBreak = false;
65346534
}
6535-
nextJSDocToken();
6535+
next();
65366536
}
65376537
}
65386538

@@ -6542,8 +6542,9 @@ namespace ts {
65426542
atToken.end = scanner.getTextPos();
65436543
nextJSDocToken();
65446544

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

65486549
let tag: JSDocTag | undefined;
65496550
switch (tagName.escapedText) {
@@ -6687,7 +6688,7 @@ namespace ts {
66876688
}
66886689

66896690
function tryParseTypeExpression(): JSDocTypeExpression | undefined {
6690-
skipWhitespaceOrAsterisk();
6691+
skipWhitespaceOrAsterisk(nextJSDocToken);
66916692
return token() === SyntaxKind.OpenBraceToken ? parseJSDocTypeExpression() : undefined;
66926693
}
66936694

@@ -6727,7 +6728,7 @@ namespace ts {
67276728
function parseParameterOrPropertyTag(atToken: AtToken, tagName: Identifier, target: PropertyLikeParse, indent: number): JSDocParameterTag | JSDocPropertyTag {
67286729
let typeExpression = tryParseTypeExpression();
67296730
let isNameFirst = !typeExpression;
6730-
skipWhitespaceOrAsterisk();
6731+
skipWhitespaceOrAsterisk(nextJSDocToken);
67316732

67326733
const { name, isBracketed } = parseBracketNameInPropertyAndParamTag();
67336734
skipWhitespace();
@@ -6861,7 +6862,7 @@ namespace ts {
68616862

68626863
function parseTypedefTag(atToken: AtToken, tagName: Identifier, indent: number): JSDocTypedefTag {
68636864
const typeExpression = tryParseTypeExpression();
6864-
skipWhitespaceOrAsterisk();
6865+
skipWhitespaceOrAsterisk(nextJSDocToken);
68656866

68666867
const typedefTag = <JSDocTypedefTag>createNode(SyntaxKind.JSDocTypedefTag, atToken.pos);
68676868
typedefTag.atToken = atToken;
@@ -7114,7 +7115,7 @@ namespace ts {
71147115
return entity;
71157116
}
71167117

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

7128-
nextJSDocToken();
7129+
next();
71297130
return result;
71307131
}
71317132
}

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)