fix(48948): disallow constructor name in class accessors and generators

This commit is contained in:
Oleksandr T
2022-05-04 21:00:32 +03:00
parent 8f56f6b49d
commit 538d6ce828
12 changed files with 110 additions and 0 deletions

View File

@@ -35325,6 +35325,10 @@ namespace ts {
// Grammar checking
if (!checkGrammarMethod(node)) checkGrammarComputedPropertyName(node.name);
if (isMethodDeclaration(node) && node.asteriskToken && isIdentifier(node.name) && idText(node.name) === "constructor") {
error(node.name, Diagnostics.Class_constructor_may_not_be_a_generator);
}
// Grammar checking for modifiers is done inside the function checkGrammarFunctionLikeDeclaration
checkFunctionOrMethodDeclaration(node);
@@ -35477,6 +35481,9 @@ namespace ts {
}
function checkAccessorDeclaration(node: AccessorDeclaration) {
if (isIdentifier(node.name) && idText(node.name) === "constructor") {
error(node.name, Diagnostics.Class_constructor_may_not_be_an_accessor);
}
addLazyDiagnostic(checkAccessorDeclarationDiagnostics);
checkSourceElement(node.body);
setNodeLinksForPrivateIdentifierScope(node);

View File

@@ -1012,6 +1012,10 @@
"category": "Error",
"code": 1340
},
"Class constructor may not be an accessor.": {
"category": "Error",
"code": 1341
},
"Type arguments cannot be used here.": {
"category": "Error",
"code": 1342
@@ -1084,6 +1088,10 @@
"category": "Error",
"code": 1359
},
"Class constructor may not be a generator.": {
"category": "Error",
"code": 1360
},
"'{0}' cannot be used as a value because it was imported using 'import type'.": {
"category": "Error",
"code": 1361

View File

@@ -0,0 +1,14 @@
tests/cases/compiler/constructorNameInAccessor.ts(2,9): error TS1341: Class constructor may not be an accessor.
tests/cases/compiler/constructorNameInAccessor.ts(3,9): error TS1341: Class constructor may not be an accessor.
==== tests/cases/compiler/constructorNameInAccessor.ts (2 errors) ====
class C1 {
get constructor() { return }
~~~~~~~~~~~
!!! error TS1341: Class constructor may not be an accessor.
set constructor(value) {}
~~~~~~~~~~~
!!! error TS1341: Class constructor may not be an accessor.
}

View File

@@ -0,0 +1,12 @@
//// [constructorNameInAccessor.ts]
class C1 {
get constructor() { return }
set constructor(value) {}
}
//// [constructorNameInAccessor.js]
class C1 {
get constructor() { return; }
set constructor(value) { }
}

View File

@@ -0,0 +1,12 @@
=== tests/cases/compiler/constructorNameInAccessor.ts ===
class C1 {
>C1 : Symbol(C1, Decl(constructorNameInAccessor.ts, 0, 0))
get constructor() { return }
>constructor : Symbol(C1.constructor, Decl(constructorNameInAccessor.ts, 0, 10), Decl(constructorNameInAccessor.ts, 1, 32))
set constructor(value) {}
>constructor : Symbol(C1.constructor, Decl(constructorNameInAccessor.ts, 0, 10), Decl(constructorNameInAccessor.ts, 1, 32))
>value : Symbol(value, Decl(constructorNameInAccessor.ts, 2, 20))
}

View File

@@ -0,0 +1,12 @@
=== tests/cases/compiler/constructorNameInAccessor.ts ===
class C1 {
>C1 : C1
get constructor() { return }
>constructor : void
set constructor(value) {}
>constructor : void
>value : void
}

View File

@@ -0,0 +1,10 @@
tests/cases/compiler/constructorNameInGenerator.ts(2,6): error TS1360: Class constructor may not be a generator.
==== tests/cases/compiler/constructorNameInGenerator.ts (1 errors) ====
class C2 {
*constructor() {}
~~~~~~~~~~~
!!! error TS1360: Class constructor may not be a generator.
}

View File

@@ -0,0 +1,10 @@
//// [constructorNameInGenerator.ts]
class C2 {
*constructor() {}
}
//// [constructorNameInGenerator.js]
class C2 {
*constructor() { }
}

View File

@@ -0,0 +1,8 @@
=== tests/cases/compiler/constructorNameInGenerator.ts ===
class C2 {
>C2 : Symbol(C2, Decl(constructorNameInGenerator.ts, 0, 0))
*constructor() {}
>constructor : Symbol(C2.constructor, Decl(constructorNameInGenerator.ts, 0, 10))
}

View File

@@ -0,0 +1,8 @@
=== tests/cases/compiler/constructorNameInGenerator.ts ===
class C2 {
>C2 : C2
*constructor() {}
>constructor : () => Generator<never, void, unknown>
}

View File

@@ -0,0 +1,5 @@
// @target: esnext
class C1 {
get constructor() { return }
set constructor(value) {}
}

View File

@@ -0,0 +1,4 @@
// @target: esnext
class C2 {
*constructor() {}
}