Skip to content

Commit 0414dee

Browse files
Merge pull request #48954 from a-tarasyuk/fix/48948
fix(48948): constructor can't be the name of class accessors and generators
2 parents 90b1321 + d8ebeeb commit 0414dee

13 files changed

+112
-0
lines changed

src/compiler/checker.ts

+7
Original file line numberDiff line numberDiff line change
@@ -35389,6 +35389,10 @@ namespace ts {
3538935389
// Grammar checking
3539035390
if (!checkGrammarMethod(node)) checkGrammarComputedPropertyName(node.name);
3539135391

35392+
if (isMethodDeclaration(node) && node.asteriskToken && isIdentifier(node.name) && idText(node.name) === "constructor") {
35393+
error(node.name, Diagnostics.Class_constructor_may_not_be_a_generator);
35394+
}
35395+
3539235396
// Grammar checking for modifiers is done inside the function checkGrammarFunctionLikeDeclaration
3539335397
checkFunctionOrMethodDeclaration(node);
3539435398

@@ -35541,6 +35545,9 @@ namespace ts {
3554135545
}
3554235546

3554335547
function checkAccessorDeclaration(node: AccessorDeclaration) {
35548+
if (isIdentifier(node.name) && idText(node.name) === "constructor") {
35549+
error(node.name, Diagnostics.Class_constructor_may_not_be_an_accessor);
35550+
}
3554435551
addLazyDiagnostic(checkAccessorDeclarationDiagnostics);
3554535552
checkSourceElement(node.body);
3554635553
setNodeLinksForPrivateIdentifierScope(node);

src/compiler/diagnosticMessages.json

+8
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,10 @@
10201020
"category": "Error",
10211021
"code": 1340
10221022
},
1023+
"Class constructor may not be an accessor.": {
1024+
"category": "Error",
1025+
"code": 1341
1026+
},
10231027
"Type arguments cannot be used here.": {
10241028
"category": "Error",
10251029
"code": 1342
@@ -1092,6 +1096,10 @@
10921096
"category": "Error",
10931097
"code": 1359
10941098
},
1099+
"Class constructor may not be a generator.": {
1100+
"category": "Error",
1101+
"code": 1360
1102+
},
10951103
"'{0}' cannot be used as a value because it was imported using 'import type'.": {
10961104
"category": "Error",
10971105
"code": 1361

src/compiler/program.ts

+2
Original file line numberDiff line numberDiff line change
@@ -966,6 +966,8 @@ namespace ts {
966966
Diagnostics.extends_clause_already_seen.code,
967967
Diagnostics.let_declarations_can_only_be_declared_inside_a_block.code,
968968
Diagnostics.let_is_not_allowed_to_be_used_as_a_name_in_let_or_const_declarations.code,
969+
Diagnostics.Class_constructor_may_not_be_a_generator.code,
970+
Diagnostics.Class_constructor_may_not_be_an_accessor.code,
969971
]);
970972

971973
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
tests/cases/conformance/salsa/constructorNameInAccessor.ts(2,9): error TS1341: Class constructor may not be an accessor.
2+
tests/cases/conformance/salsa/constructorNameInAccessor.ts(3,9): error TS1341: Class constructor may not be an accessor.
3+
4+
5+
==== tests/cases/conformance/salsa/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+
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
=== tests/cases/conformance/salsa/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+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
=== tests/cases/conformance/salsa/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+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
tests/cases/conformance/salsa/constructorNameInGenerator.ts(2,6): error TS1360: Class constructor may not be a generator.
2+
3+
4+
==== tests/cases/conformance/salsa/constructorNameInGenerator.ts (1 errors) ====
5+
class C2 {
6+
*constructor() {}
7+
~~~~~~~~~~~
8+
!!! error TS1360: Class constructor may not be a generator.
9+
}
10+
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=== tests/cases/conformance/salsa/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+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=== tests/cases/conformance/salsa/constructorNameInGenerator.ts ===
2+
class C2 {
3+
>C2 : C2
4+
5+
*constructor() {}
6+
>constructor : () => Generator<never, void, unknown>
7+
}
8+
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+
}
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)