Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,8 @@ namespace ts {
Diagnostics.extends_clause_already_seen.code,
Diagnostics.let_declarations_can_only_be_declared_inside_a_block.code,
Diagnostics.let_is_not_allowed_to_be_used_as_a_name_in_let_or_const_declarations.code,
Diagnostics.Class_constructor_may_not_be_a_generator.code,
Diagnostics.Class_constructor_may_not_be_an_accessor.code,
]);

/**
Expand Down
14 changes: 14 additions & 0 deletions tests/baselines/reference/constructorNameInAccessor.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
tests/cases/conformance/salsa/constructorNameInAccessor.ts(2,9): error TS1341: Class constructor may not be an accessor.
tests/cases/conformance/salsa/constructorNameInAccessor.ts(3,9): error TS1341: Class constructor may not be an accessor.


==== tests/cases/conformance/salsa/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.
}

12 changes: 12 additions & 0 deletions tests/baselines/reference/constructorNameInAccessor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//// [constructorNameInAccessor.ts]
class C1 {
get constructor() { return }
set constructor(value) {}
}


//// [constructorNameInAccessor.js]
class C1 {
get constructor() { return; }
set constructor(value) { }
}
12 changes: 12 additions & 0 deletions tests/baselines/reference/constructorNameInAccessor.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
=== tests/cases/conformance/salsa/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))
}

12 changes: 12 additions & 0 deletions tests/baselines/reference/constructorNameInAccessor.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
=== tests/cases/conformance/salsa/constructorNameInAccessor.ts ===
class C1 {
>C1 : C1

get constructor() { return }
>constructor : void

set constructor(value) {}
>constructor : void
>value : void
}

10 changes: 10 additions & 0 deletions tests/baselines/reference/constructorNameInGenerator.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
tests/cases/conformance/salsa/constructorNameInGenerator.ts(2,6): error TS1360: Class constructor may not be a generator.


==== tests/cases/conformance/salsa/constructorNameInGenerator.ts (1 errors) ====
class C2 {
*constructor() {}
~~~~~~~~~~~
!!! error TS1360: Class constructor may not be a generator.
}

10 changes: 10 additions & 0 deletions tests/baselines/reference/constructorNameInGenerator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//// [constructorNameInGenerator.ts]
class C2 {
*constructor() {}
}


//// [constructorNameInGenerator.js]
class C2 {
*constructor() { }
}
8 changes: 8 additions & 0 deletions tests/baselines/reference/constructorNameInGenerator.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
=== tests/cases/conformance/salsa/constructorNameInGenerator.ts ===
class C2 {
>C2 : Symbol(C2, Decl(constructorNameInGenerator.ts, 0, 0))

*constructor() {}
>constructor : Symbol(C2.constructor, Decl(constructorNameInGenerator.ts, 0, 10))
}

8 changes: 8 additions & 0 deletions tests/baselines/reference/constructorNameInGenerator.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
=== tests/cases/conformance/salsa/constructorNameInGenerator.ts ===
class C2 {
>C2 : C2

*constructor() {}
>constructor : () => Generator<never, void, unknown>
}

5 changes: 5 additions & 0 deletions tests/cases/conformance/salsa/constructorNameInAccessor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// @target: esnext
class C1 {
get constructor() { return }
set constructor(value) {}
}
4 changes: 4 additions & 0 deletions tests/cases/conformance/salsa/constructorNameInGenerator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// @target: esnext
class C2 {
*constructor() {}
}