Skip to content

Commit 06fea7b

Browse files
author
Andy Hanson
committed
Catch illegal jsdoc tags on constructors
1 parent f352e46 commit 06fea7b

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
@@ -26159,14 +26159,17 @@ namespace ts {
2615926159
}
2616026160

2616126161
function checkGrammarConstructorTypeParameters(node: ConstructorDeclaration) {
26162-
if (node.typeParameters) {
26163-
return grammarErrorAtPos(node, node.typeParameters.pos, node.typeParameters.end - node.typeParameters.pos, Diagnostics.Type_parameters_cannot_appear_on_a_constructor_declaration);
26162+
const typeParameters = getEffectiveTypeParameterDeclarations(node);
26163+
if (typeParameters) {
26164+
const { pos, end } = isNodeArray(typeParameters) ? typeParameters : first(typeParameters);
26165+
return grammarErrorAtPos(node, pos, end - pos, Diagnostics.Type_parameters_cannot_appear_on_a_constructor_declaration);
2616426166
}
2616526167
}
2616626168

2616726169
function checkGrammarConstructorTypeAnnotation(node: ConstructorDeclaration) {
26168-
if (node.type) {
26169-
return grammarErrorOnNode(node.type, Diagnostics.Type_annotation_cannot_appear_on_a_constructor_declaration);
26170+
const type = getEffectiveReturnTypeNode(node);
26171+
if (type) {
26172+
return grammarErrorOnNode(type, Diagnostics.Type_annotation_cannot_appear_on_a_constructor_declaration);
2617026173
}
2617126174
}
2617226175

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)