Skip to content

Commit f561856

Browse files
Check for missing type parameter names in JSDoc template tags (#42017)
* Check for missing type parameter names in JSDoc template tags Fixes #36692 * Update baselines
1 parent 412ecbc commit f561856

3 files changed

Lines changed: 12 additions & 2 deletions

File tree

src/compiler/parser.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8043,6 +8043,9 @@ namespace ts {
80438043
function parseTemplateTagTypeParameter() {
80448044
const typeParameterPos = getNodePos();
80458045
const name = parseJSDocIdentifierName(Diagnostics.Unexpected_token_A_type_parameter_name_was_expected_without_curly_braces);
8046+
if (nodeIsMissing(name)) {
8047+
return undefined;
8048+
}
80468049
return finishNode(factory.createTypeParameterDeclaration(name, /*constraint*/ undefined, /*defaultType*/ undefined), typeParameterPos);
80478050
}
80488051

@@ -8051,7 +8054,10 @@ namespace ts {
80518054
const typeParameters = [];
80528055
do {
80538056
skipWhitespace();
8054-
typeParameters.push(parseTemplateTagTypeParameter());
8057+
const node = parseTemplateTagTypeParameter();
8058+
if (node !== undefined) {
8059+
typeParameters.push(node);
8060+
}
80558061
skipWhitespaceOrAsterisk();
80568062
} while (parseOptionalJsdoc(SyntaxKind.CommaToken));
80578063
return createNodeArray(typeParameters, pos);

src/testRunner/unittests/jsDocParsing.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,5 +382,9 @@ namespace ts {
382382
const root = createSourceFile("foo.ts", "/** */var a = true;", ScriptTarget.ES5, /*setParentNodes*/ false);
383383
root.statements[0].getStart(root, /*includeJsdocComment*/ true);
384384
});
385+
describe("missing type parameter in jsDoc doesn't create a 1-element array", () => {
386+
const doc = parseIsolatedJSDocComment("/**\n @template\n*/");
387+
assert.equal((doc?.jsDoc.tags?.[0] as JSDocTemplateTag).typeParameters.length, 0);
388+
});
385389
});
386390
}

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 : <(Missing) extends any, T>(x: T) => void
92+
>g : <T>(x: T) => void
9393
>x : T
9494

9595

0 commit comments

Comments
 (0)