Skip to content

Simplify parseJSDocIdentifierName #24660

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 45 additions & 70 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6496,54 +6496,42 @@ namespace ts {

const tagName = parseJSDocIdentifierName();
skipWhitespace();
if (!tagName) {
return;
}

let tag: JSDocTag | undefined;
if (tagName) {
switch (tagName.escapedText) {
case "augments":
case "extends":
tag = parseAugmentsTag(atToken, tagName);
break;
case "class":
case "constructor":
tag = parseClassTag(atToken, tagName);
break;
case "arg":
case "argument":
case "param":
return parseParameterOrPropertyTag(atToken, tagName, PropertyLikeParse.Parameter, indent);
case "return":
case "returns":
tag = parseReturnTag(atToken, tagName);
break;
case "template":
tag = parseTemplateTag(atToken, tagName);
break;
case "type":
tag = parseTypeTag(atToken, tagName);
break;
case "typedef":
tag = parseTypedefTag(atToken, tagName, indent);
break;
case "callback":
tag = parseCallbackTag(atToken, tagName, indent);
break;
default:
tag = parseUnknownTag(atToken, tagName);
break;
}
}
else {
tag = parseUnknownTag(atToken, tagName);
switch (tagName.escapedText) {
case "augments":
case "extends":
tag = parseAugmentsTag(atToken, tagName);
break;
case "class":
case "constructor":
tag = parseClassTag(atToken, tagName);
break;
case "arg":
case "argument":
case "param":
return parseParameterOrPropertyTag(atToken, tagName, PropertyLikeParse.Parameter, indent);
case "return":
case "returns":
tag = parseReturnTag(atToken, tagName);
break;
case "template":
tag = parseTemplateTag(atToken, tagName);
break;
case "type":
tag = parseTypeTag(atToken, tagName);
break;
case "typedef":
tag = parseTypedefTag(atToken, tagName, indent);
break;
case "callback":
tag = parseCallbackTag(atToken, tagName, indent);
break;
default:
tag = parseUnknownTag(atToken, tagName);
break;
}

if (!tag) {
// a badly malformed tag should not be added to the list of tags
return;
}
if (!tag.comment) {
// some tags, like typedef and callback, have already parsed their comments earlier
tag.comment = parseTagComments(indent + tag.end - tag.pos);
Expand Down Expand Up @@ -6773,11 +6761,11 @@ namespace ts {
}

function parsePropertyAccessEntityNameExpression() {
let node: Identifier | PropertyAccessEntityNameExpression = parseJSDocIdentifierName(/*createIfMissing*/ true);
let node: Identifier | PropertyAccessEntityNameExpression = parseJSDocIdentifierName();
while (parseOptional(SyntaxKind.DotToken)) {
const prop: PropertyAccessEntityNameExpression = createNode(SyntaxKind.PropertyAccessExpression, node.pos) as PropertyAccessEntityNameExpression;
prop.expression = node;
prop.name = parseJSDocIdentifierName()!; // TODO: GH#18217
prop.name = parseJSDocIdentifierName();
node = finishNode(prop);
}
return node;
Expand Down Expand Up @@ -6842,9 +6830,11 @@ namespace ts {

function parseJSDocTypeNameWithNamespace(nested?: boolean) {
const pos = scanner.getTokenPos();
if (!tokenIsIdentifierOrKeyword(token())) {
return undefined;
}
const typeNameOrNamespaceName = parseJSDocIdentifierName();

if (typeNameOrNamespaceName && parseOptional(SyntaxKind.DotToken)) {
if (parseOptional(SyntaxKind.DotToken)) {
const jsDocNamespaceNode = <JSDocNamespaceDeclaration>createNode(SyntaxKind.ModuleDeclaration, pos);
if (nested) {
jsDocNamespaceNode.flags |= NodeFlags.NestedNamespace;
Expand All @@ -6854,7 +6844,7 @@ namespace ts {
return finishNode(jsDocNamespaceNode);
}

if (typeNameOrNamespaceName && nested) {
if (nested) {
typeNameOrNamespaceName.isInJSDocNamespace = true;
}
return typeNameOrNamespaceName;
Expand Down Expand Up @@ -6965,9 +6955,6 @@ namespace ts {

const tagName = parseJSDocIdentifierName();
skipWhitespace();
if (!tagName) {
return false;
}
let t: PropertyLikeParse;
switch (tagName.escapedText) {
case "type":
Expand All @@ -6992,7 +6979,7 @@ namespace ts {
return tag;
}

function parseTemplateTag(atToken: AtToken, tagName: Identifier): JSDocTemplateTag | undefined {
function parseTemplateTag(atToken: AtToken, tagName: Identifier): JSDocTemplateTag {
// the template tag looks like '@template {Constraint} T,U,V'
let constraint: JSDocTypeExpression | undefined;
if (token() === SyntaxKind.OpenBraceToken) {
Expand All @@ -7004,11 +6991,7 @@ namespace ts {
const typeParametersPos = getNodePos();
while (true) {
const typeParameter = <TypeParameterDeclaration>createNode(SyntaxKind.TypeParameter);
if (!tokenIsIdentifierOrKeyword(token())) {
parseErrorAtCurrentToken(Diagnostics.Unexpected_token_A_type_parameter_name_was_expected_without_curly_braces);
return undefined;
}
typeParameter.name = parseJSDocIdentifierName()!;
typeParameter.name = parseJSDocIdentifierName(Diagnostics.Unexpected_token_A_type_parameter_name_was_expected_without_curly_braces);
skipWhitespace();
finishNode(typeParameter);
typeParameters.push(typeParameter);
Expand Down Expand Up @@ -7039,15 +7022,15 @@ namespace ts {
}

function parseJSDocEntityName(): EntityName {
let entity: EntityName = parseJSDocIdentifierName(/*createIfMissing*/ true);
let entity: EntityName = parseJSDocIdentifierName();
if (parseOptional(SyntaxKind.OpenBracketToken)) {
parseExpected(SyntaxKind.CloseBracketToken);
// Note that y[] is accepted as an entity name, but the postfix brackets are not saved for checking.
// Technically usejsdoc.org requires them for specifying a property of a type equivalent to Array<{ x: ...}>
// but it's not worth it to enforce that restriction.
}
while (parseOptional(SyntaxKind.DotToken)) {
const name = parseJSDocIdentifierName(/*createIfMissing*/ true);
const name = parseJSDocIdentifierName();
if (parseOptional(SyntaxKind.OpenBracketToken)) {
parseExpected(SyntaxKind.CloseBracketToken);
}
Expand All @@ -7056,17 +7039,9 @@ namespace ts {
return entity;
}

function parseJSDocIdentifierName(): Identifier | undefined;
function parseJSDocIdentifierName(createIfMissing: true): Identifier;
function parseJSDocIdentifierName(createIfMissing = false): Identifier | undefined {
function parseJSDocIdentifierName(message?: DiagnosticMessage): Identifier {
if (!tokenIsIdentifierOrKeyword(token())) {
if (createIfMissing) {
return createMissingNode<Identifier>(SyntaxKind.Identifier, /*reportAtCurrentPosition*/ true, Diagnostics.Identifier_expected);
}
else {
parseErrorAtCurrentToken(Diagnostics.Identifier_expected);
return undefined;
}
return createMissingNode<Identifier>(SyntaxKind.Identifier, /*reportAtCurrentPosition*/ !message, message || Diagnostics.Identifier_expected);
}

const pos = scanner.getTokenPos();
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/jsdocTemplateTag3.types
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ f({ a: 12 }, undefined, undefined, 101, 'nope');
* @param {T} x
*/
function g(x) { }
>g : <T>(x: T) => void
>g : <(Missing) extends any, T>(x: T) => void
>x : T