Skip to content

Commit 2f69eb6

Browse files
author
Andy Hanson
committed
Catch illegal jsdoc tags on constructors
1 parent 09d0a67 commit 2f69eb6

File tree

5 files changed

+79
-4
lines changed

5 files changed

+79
-4
lines changed

src/compiler/checker.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25686,14 +25686,17 @@ namespace ts {
2568625686
}
2568725687

2568825688
function checkGrammarConstructorTypeParameters(node: ConstructorDeclaration) {
25689-
if (node.typeParameters) {
25690-
return grammarErrorAtPos(node, node.typeParameters.pos, node.typeParameters.end - node.typeParameters.pos, Diagnostics.Type_parameters_cannot_appear_on_a_constructor_declaration);
25689+
const typeParameters = getEffectiveTypeParameterDeclarations(node);
25690+
if (typeParameters) {
25691+
const { pos, end } = isNodeArray(typeParameters) ? typeParameters : first(typeParameters);
25692+
return grammarErrorAtPos(node, pos, end - pos, Diagnostics.Type_parameters_cannot_appear_on_a_constructor_declaration);
2569125693
}
2569225694
}
2569325695

2569425696
function checkGrammarConstructorTypeAnnotation(node: ConstructorDeclaration) {
25695-
if (node.type) {
25696-
return grammarErrorOnNode(node.type, Diagnostics.Type_annotation_cannot_appear_on_a_constructor_declaration);
25697+
const type = getEffectiveReturnTypeNode(node);
25698+
if (type) {
25699+
return grammarErrorOnNode(type, Diagnostics.Type_annotation_cannot_appear_on_a_constructor_declaration);
2569725700
}
2569825701
}
2569925702

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