Skip to content

Commit 013ce8e

Browse files
author
Andy
authored
Catch illegal jsdoc tags on constructors (#20045)
1 parent 739097a commit 013ce8e

File tree

5 files changed

+66
-4
lines changed

5 files changed

+66
-4
lines changed

src/compiler/checker.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26248,14 +26248,17 @@ namespace ts {
2624826248
}
2624926249

2625026250
function checkGrammarConstructorTypeParameters(node: ConstructorDeclaration) {
26251-
if (node.typeParameters) {
26252-
return grammarErrorAtPos(node, node.typeParameters.pos, node.typeParameters.end - node.typeParameters.pos, Diagnostics.Type_parameters_cannot_appear_on_a_constructor_declaration);
26251+
const typeParameters = getEffectiveTypeParameterDeclarations(node);
26252+
if (typeParameters) {
26253+
const { pos, end } = isNodeArray(typeParameters) ? typeParameters : first(typeParameters);
26254+
return grammarErrorAtPos(node, pos, end - pos, Diagnostics.Type_parameters_cannot_appear_on_a_constructor_declaration);
2625326255
}
2625426256
}
2625526257

2625626258
function checkGrammarConstructorTypeAnnotation(node: ConstructorDeclaration) {
26257-
if (node.type) {
26258-
return grammarErrorOnNode(node.type, Diagnostics.Type_annotation_cannot_appear_on_a_constructor_declaration);
26259+
const type = getEffectiveReturnTypeNode(node);
26260+
if (type) {
26261+
return grammarErrorOnNode(type, Diagnostics.Type_annotation_cannot_appear_on_a_constructor_declaration);
2625926262
}
2626026263
}
2626126264

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/a.js(2,19): error TS1092: Type parameters cannot appear on a constructor declaration.
2+
/a.js(6,18): error TS1093: Type annotation cannot appear on a constructor declaration.
3+
4+
5+
==== /a.js (2 errors) ====
6+
class C {
7+
/** @template T */
8+
~
9+
!!! error TS1092: Type parameters cannot appear on a constructor declaration.
10+
constructor() { }
11+
}
12+
class D {
13+
/** @return {number} */
14+
~~~~~~
15+
!!! error TS1093: Type annotation cannot appear on a constructor declaration.
16+
constructor() {}
17+
}
18+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
=== /a.js ===
2+
class C {
3+
>C : Symbol(C, Decl(a.js, 0, 0))
4+
5+
/** @template T */
6+
constructor() { }
7+
}
8+
class D {
9+
>D : Symbol(D, Decl(a.js, 3, 1))
10+
11+
/** @return {number} */
12+
constructor() {}
13+
}
14+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
=== /a.js ===
2+
class C {
3+
>C : C
4+
5+
/** @template T */
6+
constructor() { }
7+
}
8+
class D {
9+
>D : D
10+
11+
/** @return {number} */
12+
constructor() {}
13+
}
14+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// @allowJs: true
2+
// @checkJs: true
3+
// @noEmit: true
4+
// @Filename: /a.js
5+
6+
class C {
7+
/** @template T */
8+
constructor() { }
9+
}
10+
class D {
11+
/** @return {number} */
12+
constructor() {}
13+
}

0 commit comments

Comments
 (0)