Skip to content

Commit 538d6ce

Browse files
committed
fix(48948): disallow constructor name in class accessors and generators
1 parent 8f56f6b commit 538d6ce

12 files changed

+110
-0
lines changed

src/compiler/checker.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35325,6 +35325,10 @@ namespace ts {
3532535325
// Grammar checking
3532635326
if (!checkGrammarMethod(node)) checkGrammarComputedPropertyName(node.name);
3532735327

35328+
if (isMethodDeclaration(node) && node.asteriskToken && isIdentifier(node.name) && idText(node.name) === "constructor") {
35329+
error(node.name, Diagnostics.Class_constructor_may_not_be_a_generator);
35330+
}
35331+
3532835332
// Grammar checking for modifiers is done inside the function checkGrammarFunctionLikeDeclaration
3532935333
checkFunctionOrMethodDeclaration(node);
3533035334

@@ -35477,6 +35481,9 @@ namespace ts {
3547735481
}
3547835482

3547935483
function checkAccessorDeclaration(node: AccessorDeclaration) {
35484+
if (isIdentifier(node.name) && idText(node.name) === "constructor") {
35485+
error(node.name, Diagnostics.Class_constructor_may_not_be_an_accessor);
35486+
}
3548035487
addLazyDiagnostic(checkAccessorDeclarationDiagnostics);
3548135488
checkSourceElement(node.body);
3548235489
setNodeLinksForPrivateIdentifierScope(node);

src/compiler/diagnosticMessages.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,10 @@
10121012
"category": "Error",
10131013
"code": 1340
10141014
},
1015+
"Class constructor may not be an accessor.": {
1016+
"category": "Error",
1017+
"code": 1341
1018+
},
10151019
"Type arguments cannot be used here.": {
10161020
"category": "Error",
10171021
"code": 1342
@@ -1084,6 +1088,10 @@
10841088
"category": "Error",
10851089
"code": 1359
10861090
},
1091+
"Class constructor may not be a generator.": {
1092+
"category": "Error",
1093+
"code": 1360
1094+
},
10871095
"'{0}' cannot be used as a value because it was imported using 'import type'.": {
10881096
"category": "Error",
10891097
"code": 1361
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
tests/cases/compiler/constructorNameInAccessor.ts(2,9): error TS1341: Class constructor may not be an accessor.
2+
tests/cases/compiler/constructorNameInAccessor.ts(3,9): error TS1341: Class constructor may not be an accessor.
3+
4+
5+
==== tests/cases/compiler/constructorNameInAccessor.ts (2 errors) ====
6+
class C1 {
7+
get constructor() { return }
8+
~~~~~~~~~~~
9+
!!! error TS1341: Class constructor may not be an accessor.
10+
set constructor(value) {}
11+
~~~~~~~~~~~
12+
!!! error TS1341: Class constructor may not be an accessor.
13+
}
14+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//// [constructorNameInAccessor.ts]
2+
class C1 {
3+
get constructor() { return }
4+
set constructor(value) {}
5+
}
6+
7+
8+
//// [constructorNameInAccessor.js]
9+
class C1 {
10+
get constructor() { return; }
11+
set constructor(value) { }
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
=== tests/cases/compiler/constructorNameInAccessor.ts ===
2+
class C1 {
3+
>C1 : Symbol(C1, Decl(constructorNameInAccessor.ts, 0, 0))
4+
5+
get constructor() { return }
6+
>constructor : Symbol(C1.constructor, Decl(constructorNameInAccessor.ts, 0, 10), Decl(constructorNameInAccessor.ts, 1, 32))
7+
8+
set constructor(value) {}
9+
>constructor : Symbol(C1.constructor, Decl(constructorNameInAccessor.ts, 0, 10), Decl(constructorNameInAccessor.ts, 1, 32))
10+
>value : Symbol(value, Decl(constructorNameInAccessor.ts, 2, 20))
11+
}
12+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
=== tests/cases/compiler/constructorNameInAccessor.ts ===
2+
class C1 {
3+
>C1 : C1
4+
5+
get constructor() { return }
6+
>constructor : void
7+
8+
set constructor(value) {}
9+
>constructor : void
10+
>value : void
11+
}
12+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
tests/cases/compiler/constructorNameInGenerator.ts(2,6): error TS1360: Class constructor may not be a generator.
2+
3+
4+
==== tests/cases/compiler/constructorNameInGenerator.ts (1 errors) ====
5+
class C2 {
6+
*constructor() {}
7+
~~~~~~~~~~~
8+
!!! error TS1360: Class constructor may not be a generator.
9+
}
10+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//// [constructorNameInGenerator.ts]
2+
class C2 {
3+
*constructor() {}
4+
}
5+
6+
7+
//// [constructorNameInGenerator.js]
8+
class C2 {
9+
*constructor() { }
10+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=== tests/cases/compiler/constructorNameInGenerator.ts ===
2+
class C2 {
3+
>C2 : Symbol(C2, Decl(constructorNameInGenerator.ts, 0, 0))
4+
5+
*constructor() {}
6+
>constructor : Symbol(C2.constructor, Decl(constructorNameInGenerator.ts, 0, 10))
7+
}
8+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=== tests/cases/compiler/constructorNameInGenerator.ts ===
2+
class C2 {
3+
>C2 : C2
4+
5+
*constructor() {}
6+
>constructor : () => Generator<never, void, unknown>
7+
}
8+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// @target: esnext
2+
class C1 {
3+
get constructor() { return }
4+
set constructor(value) {}
5+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// @target: esnext
2+
class C2 {
3+
*constructor() {}
4+
}

0 commit comments

Comments
 (0)