fix(36247): disallow 'constructor' as a parameter property name (#37285)

This commit is contained in:
Alexander T 2020-03-17 23:48:05 +02:00 committed by GitHub
parent 878f447798
commit 66aa9e77bf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 49 additions and 0 deletions

View file

@ -28912,6 +28912,9 @@ namespace ts {
if (!(func.kind === SyntaxKind.Constructor && nodeIsPresent(func.body))) {
error(node, Diagnostics.A_parameter_property_is_only_allowed_in_a_constructor_implementation);
}
if (func.kind === SyntaxKind.Constructor && isIdentifier(node.name) && node.name.escapedText === "constructor") {
error(node.name, Diagnostics.constructor_cannot_be_used_as_a_parameter_property_name);
}
}
if (node.questionToken && isBindingPattern(node.name) && (func as FunctionLikeDeclaration).body) {
error(node, Diagnostics.A_binding_pattern_parameter_cannot_be_optional_in_an_implementation_signature);

View file

@ -1569,6 +1569,10 @@
"category": "Error",
"code": 2397
},
"'constructor' cannot be used as a parameter property name.": {
"category": "Error",
"code": 2398
},
"Duplicate identifier '_this'. Compiler uses variable declaration '_this' to capture 'this' reference.": {
"category": "Error",
"code": 2399

View file

@ -0,0 +1,10 @@
tests/cases/compiler/parameterPropertyInConstructor3.ts(2,22): error TS2398: 'constructor' cannot be used as a parameter property name.
==== tests/cases/compiler/parameterPropertyInConstructor3.ts (1 errors) ====
class Foo {
constructor(public constructor: string) {}
~~~~~~~~~~~
!!! error TS2398: 'constructor' cannot be used as a parameter property name.
}

View file

@ -0,0 +1,13 @@
//// [parameterPropertyInConstructor3.ts]
class Foo {
constructor(public constructor: string) {}
}
//// [parameterPropertyInConstructor3.js]
var Foo = /** @class */ (function () {
function Foo(constructor) {
this.constructor = constructor;
}
return Foo;
}());

View file

@ -0,0 +1,8 @@
=== tests/cases/compiler/parameterPropertyInConstructor3.ts ===
class Foo {
>Foo : Symbol(Foo, Decl(parameterPropertyInConstructor3.ts, 0, 0))
constructor(public constructor: string) {}
>constructor : Symbol(Foo.constructor, Decl(parameterPropertyInConstructor3.ts, 1, 14))
}

View file

@ -0,0 +1,8 @@
=== tests/cases/compiler/parameterPropertyInConstructor3.ts ===
class Foo {
>Foo : Foo
constructor(public constructor: string) {}
>constructor : string
}

View file

@ -0,0 +1,3 @@
class Foo {
constructor(public constructor: string) {}
}