Skip to content

Commit

Permalink
Catch illegal jsdoc tags on constructors (#20045)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy authored Nov 22, 2017
1 parent 739097a commit 013ce8e
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26248,14 +26248,17 @@ namespace ts {
}

function checkGrammarConstructorTypeParameters(node: ConstructorDeclaration) {
if (node.typeParameters) {
return grammarErrorAtPos(node, node.typeParameters.pos, node.typeParameters.end - node.typeParameters.pos, Diagnostics.Type_parameters_cannot_appear_on_a_constructor_declaration);
const typeParameters = getEffectiveTypeParameterDeclarations(node);
if (typeParameters) {
const { pos, end } = isNodeArray(typeParameters) ? typeParameters : first(typeParameters);
return grammarErrorAtPos(node, pos, end - pos, Diagnostics.Type_parameters_cannot_appear_on_a_constructor_declaration);
}
}

function checkGrammarConstructorTypeAnnotation(node: ConstructorDeclaration) {
if (node.type) {
return grammarErrorOnNode(node.type, Diagnostics.Type_annotation_cannot_appear_on_a_constructor_declaration);
const type = getEffectiveReturnTypeNode(node);
if (type) {
return grammarErrorOnNode(type, Diagnostics.Type_annotation_cannot_appear_on_a_constructor_declaration);
}
}

Expand Down
18 changes: 18 additions & 0 deletions tests/baselines/reference/jsdocIllegalTags.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/a.js(2,19): error TS1092: Type parameters cannot appear on a constructor declaration.
/a.js(6,18): error TS1093: Type annotation cannot appear on a constructor declaration.


==== /a.js (2 errors) ====
class C {
/** @template T */
~
!!! error TS1092: Type parameters cannot appear on a constructor declaration.
constructor() { }
}
class D {
/** @return {number} */
~~~~~~
!!! error TS1093: Type annotation cannot appear on a constructor declaration.
constructor() {}
}

14 changes: 14 additions & 0 deletions tests/baselines/reference/jsdocIllegalTags.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
=== /a.js ===
class C {
>C : Symbol(C, Decl(a.js, 0, 0))

/** @template T */
constructor() { }
}
class D {
>D : Symbol(D, Decl(a.js, 3, 1))

/** @return {number} */
constructor() {}
}

14 changes: 14 additions & 0 deletions tests/baselines/reference/jsdocIllegalTags.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
=== /a.js ===
class C {
>C : C

/** @template T */
constructor() { }
}
class D {
>D : D

/** @return {number} */
constructor() {}
}

13 changes: 13 additions & 0 deletions tests/cases/compiler/jsdocIllegalTags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// @allowJs: true
// @checkJs: true
// @noEmit: true
// @Filename: /a.js

class C {
/** @template T */
constructor() { }
}
class D {
/** @return {number} */
constructor() {}
}

0 comments on commit 013ce8e

Please sign in to comment.