Skip to content

Commit 8d737ca

Browse files
authored
Simplify parseJSDocIdentifierName (microsoft#24660)
* Simplify parseJSDocIdentifierName It now always creates a missing node. The one place that depended on it returning undefined, parseJSDocTypeNameWithNamespace, now returns undefined before calling parseJSDocIdentifierName. * Remove assert It is adequately proven at compile time.
1 parent d9b9390 commit 8d737ca

2 files changed

Lines changed: 46 additions & 71 deletions

File tree

src/compiler/parser.ts

Lines changed: 45 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -6486,54 +6486,42 @@ namespace ts {
64866486

64876487
const tagName = parseJSDocIdentifierName();
64886488
skipWhitespace();
6489-
if (!tagName) {
6490-
return;
6491-
}
64926489

64936490
let tag: JSDocTag | undefined;
6494-
if (tagName) {
6495-
switch (tagName.escapedText) {
6496-
case "augments":
6497-
case "extends":
6498-
tag = parseAugmentsTag(atToken, tagName);
6499-
break;
6500-
case "class":
6501-
case "constructor":
6502-
tag = parseClassTag(atToken, tagName);
6503-
break;
6504-
case "arg":
6505-
case "argument":
6506-
case "param":
6507-
return parseParameterOrPropertyTag(atToken, tagName, PropertyLikeParse.Parameter, indent);
6508-
case "return":
6509-
case "returns":
6510-
tag = parseReturnTag(atToken, tagName);
6511-
break;
6512-
case "template":
6513-
tag = parseTemplateTag(atToken, tagName);
6514-
break;
6515-
case "type":
6516-
tag = parseTypeTag(atToken, tagName);
6517-
break;
6518-
case "typedef":
6519-
tag = parseTypedefTag(atToken, tagName, indent);
6520-
break;
6521-
case "callback":
6522-
tag = parseCallbackTag(atToken, tagName, indent);
6523-
break;
6524-
default:
6525-
tag = parseUnknownTag(atToken, tagName);
6526-
break;
6527-
}
6528-
}
6529-
else {
6530-
tag = parseUnknownTag(atToken, tagName);
6491+
switch (tagName.escapedText) {
6492+
case "augments":
6493+
case "extends":
6494+
tag = parseAugmentsTag(atToken, tagName);
6495+
break;
6496+
case "class":
6497+
case "constructor":
6498+
tag = parseClassTag(atToken, tagName);
6499+
break;
6500+
case "arg":
6501+
case "argument":
6502+
case "param":
6503+
return parseParameterOrPropertyTag(atToken, tagName, PropertyLikeParse.Parameter, indent);
6504+
case "return":
6505+
case "returns":
6506+
tag = parseReturnTag(atToken, tagName);
6507+
break;
6508+
case "template":
6509+
tag = parseTemplateTag(atToken, tagName);
6510+
break;
6511+
case "type":
6512+
tag = parseTypeTag(atToken, tagName);
6513+
break;
6514+
case "typedef":
6515+
tag = parseTypedefTag(atToken, tagName, indent);
6516+
break;
6517+
case "callback":
6518+
tag = parseCallbackTag(atToken, tagName, indent);
6519+
break;
6520+
default:
6521+
tag = parseUnknownTag(atToken, tagName);
6522+
break;
65316523
}
65326524

6533-
if (!tag) {
6534-
// a badly malformed tag should not be added to the list of tags
6535-
return;
6536-
}
65376525
if (!tag.comment) {
65386526
// some tags, like typedef and callback, have already parsed their comments earlier
65396527
tag.comment = parseTagComments(indent + tag.end - tag.pos);
@@ -6763,11 +6751,11 @@ namespace ts {
67636751
}
67646752

67656753
function parsePropertyAccessEntityNameExpression() {
6766-
let node: Identifier | PropertyAccessEntityNameExpression = parseJSDocIdentifierName(/*createIfMissing*/ true);
6754+
let node: Identifier | PropertyAccessEntityNameExpression = parseJSDocIdentifierName();
67676755
while (parseOptional(SyntaxKind.DotToken)) {
67686756
const prop: PropertyAccessEntityNameExpression = createNode(SyntaxKind.PropertyAccessExpression, node.pos) as PropertyAccessEntityNameExpression;
67696757
prop.expression = node;
6770-
prop.name = parseJSDocIdentifierName()!; // TODO: GH#18217
6758+
prop.name = parseJSDocIdentifierName();
67716759
node = finishNode(prop);
67726760
}
67736761
return node;
@@ -6832,9 +6820,11 @@ namespace ts {
68326820

68336821
function parseJSDocTypeNameWithNamespace(nested?: boolean) {
68346822
const pos = scanner.getTokenPos();
6823+
if (!tokenIsIdentifierOrKeyword(token())) {
6824+
return undefined;
6825+
}
68356826
const typeNameOrNamespaceName = parseJSDocIdentifierName();
6836-
6837-
if (typeNameOrNamespaceName && parseOptional(SyntaxKind.DotToken)) {
6827+
if (parseOptional(SyntaxKind.DotToken)) {
68386828
const jsDocNamespaceNode = <JSDocNamespaceDeclaration>createNode(SyntaxKind.ModuleDeclaration, pos);
68396829
if (nested) {
68406830
jsDocNamespaceNode.flags |= NodeFlags.NestedNamespace;
@@ -6844,7 +6834,7 @@ namespace ts {
68446834
return finishNode(jsDocNamespaceNode);
68456835
}
68466836

6847-
if (typeNameOrNamespaceName && nested) {
6837+
if (nested) {
68486838
typeNameOrNamespaceName.isInJSDocNamespace = true;
68496839
}
68506840
return typeNameOrNamespaceName;
@@ -6954,9 +6944,6 @@ namespace ts {
69546944

69556945
const tagName = parseJSDocIdentifierName();
69566946
skipWhitespace();
6957-
if (!tagName) {
6958-
return false;
6959-
}
69606947
let t: PropertyLikeParse;
69616948
switch (tagName.escapedText) {
69626949
case "type":
@@ -6981,7 +6968,7 @@ namespace ts {
69816968
return tag;
69826969
}
69836970

6984-
function parseTemplateTag(atToken: AtToken, tagName: Identifier): JSDocTemplateTag | undefined {
6971+
function parseTemplateTag(atToken: AtToken, tagName: Identifier): JSDocTemplateTag {
69856972
// the template tag looks like '@template {Constraint} T,U,V'
69866973
let constraint: JSDocTypeExpression | undefined;
69876974
if (token() === SyntaxKind.OpenBraceToken) {
@@ -6993,11 +6980,7 @@ namespace ts {
69936980
do {
69946981
skipWhitespace();
69956982
const typeParameter = <TypeParameterDeclaration>createNode(SyntaxKind.TypeParameter);
6996-
if (!tokenIsIdentifierOrKeyword(token())) {
6997-
parseErrorAtCurrentToken(Diagnostics.Unexpected_token_A_type_parameter_name_was_expected_without_curly_braces);
6998-
return undefined;
6999-
}
7000-
typeParameter.name = parseJSDocIdentifierName()!;
6983+
typeParameter.name = parseJSDocIdentifierName(Diagnostics.Unexpected_token_A_type_parameter_name_was_expected_without_curly_braces);
70016984
skipWhitespace();
70026985
finishNode(typeParameter);
70036986
typeParameters.push(typeParameter);
@@ -7028,15 +7011,15 @@ namespace ts {
70287011
}
70297012

70307013
function parseJSDocEntityName(): EntityName {
7031-
let entity: EntityName = parseJSDocIdentifierName(/*createIfMissing*/ true);
7014+
let entity: EntityName = parseJSDocIdentifierName();
70327015
if (parseOptional(SyntaxKind.OpenBracketToken)) {
70337016
parseExpected(SyntaxKind.CloseBracketToken);
70347017
// Note that y[] is accepted as an entity name, but the postfix brackets are not saved for checking.
70357018
// Technically usejsdoc.org requires them for specifying a property of a type equivalent to Array<{ x: ...}>
70367019
// but it's not worth it to enforce that restriction.
70377020
}
70387021
while (parseOptional(SyntaxKind.DotToken)) {
7039-
const name = parseJSDocIdentifierName(/*createIfMissing*/ true);
7022+
const name = parseJSDocIdentifierName();
70407023
if (parseOptional(SyntaxKind.OpenBracketToken)) {
70417024
parseExpected(SyntaxKind.CloseBracketToken);
70427025
}
@@ -7045,17 +7028,9 @@ namespace ts {
70457028
return entity;
70467029
}
70477030

7048-
function parseJSDocIdentifierName(): Identifier | undefined;
7049-
function parseJSDocIdentifierName(createIfMissing: true): Identifier;
7050-
function parseJSDocIdentifierName(createIfMissing = false): Identifier | undefined {
7031+
function parseJSDocIdentifierName(message?: DiagnosticMessage): Identifier {
70517032
if (!tokenIsIdentifierOrKeyword(token())) {
7052-
if (createIfMissing) {
7053-
return createMissingNode<Identifier>(SyntaxKind.Identifier, /*reportAtCurrentPosition*/ true, Diagnostics.Identifier_expected);
7054-
}
7055-
else {
7056-
parseErrorAtCurrentToken(Diagnostics.Identifier_expected);
7057-
return undefined;
7058-
}
7033+
return createMissingNode<Identifier>(SyntaxKind.Identifier, /*reportAtCurrentPosition*/ !message, message || Diagnostics.Identifier_expected);
70597034
}
70607035

70617036
const pos = scanner.getTokenPos();

tests/baselines/reference/jsdocTemplateTag3.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ f({ a: 12 }, undefined, undefined, 101, 'nope');
8989
* @param {T} x
9090
*/
9191
function g(x) { }
92-
>g : <T>(x: T) => void
92+
>g : <(Missing) extends any, T>(x: T) => void
9393
>x : T
9494

9595

0 commit comments

Comments
 (0)