From 3046a544014865f8a30f2cf1a6aef3a74846c820 Mon Sep 17 00:00:00 2001 From: Alexander T Date: Tue, 3 Mar 2020 22:26:41 +0200 Subject: [PATCH] =?UTF-8?q?fix(36883):=20accessor=20allows=20`this`=20para?= =?UTF-8?q?meter=20but=20is=20not=20checke=E2=80=A6=20(#36889)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/compiler/checker.ts | 3 + src/compiler/diagnosticMessages.json | 4 ++ .../reference/thisTypeInAccessors.errors.txt | 63 +++++++++++++++++++ .../thisTypeInAccessorsNegative.errors.txt | 8 ++- 4 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 tests/baselines/reference/thisTypeInAccessors.errors.txt diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index e2d45690e57..12ccaef8e7f 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -28787,6 +28787,9 @@ namespace ts { if (func.kind === SyntaxKind.ArrowFunction) { error(node, Diagnostics.An_arrow_function_cannot_have_a_this_parameter); } + if (func.kind === SyntaxKind.GetAccessor || func.kind === SyntaxKind.SetAccessor) { + error(node, Diagnostics.get_and_set_accessors_cannot_declare_this_parameters); + } } // Only check rest parameter type if it's not a binding pattern. Since binding patterns are diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 3916debb95c..76cd6be59a1 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -2899,6 +2899,10 @@ "category": "Error", "code": 2783 }, + "'get' and 'set' accessors cannot declare 'this' parameters.": { + "category": "Error", + "code": 2784 + }, "Import declaration '{0}' is using private name '{1}'.": { "category": "Error", diff --git a/tests/baselines/reference/thisTypeInAccessors.errors.txt b/tests/baselines/reference/thisTypeInAccessors.errors.txt new file mode 100644 index 00000000000..cd19ae7ddf1 --- /dev/null +++ b/tests/baselines/reference/thisTypeInAccessors.errors.txt @@ -0,0 +1,63 @@ +tests/cases/conformance/types/thisType/thisTypeInAccessors.ts(8,11): error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters. +tests/cases/conformance/types/thisType/thisTypeInAccessors.ts(9,11): error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters. +tests/cases/conformance/types/thisType/thisTypeInAccessors.ts(13,11): error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters. +tests/cases/conformance/types/thisType/thisTypeInAccessors.ts(19,11): error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters. +tests/cases/conformance/types/thisType/thisTypeInAccessors.ts(23,11): error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters. +tests/cases/conformance/types/thisType/thisTypeInAccessors.ts(24,11): error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters. +tests/cases/conformance/types/thisType/thisTypeInAccessors.ts(29,11): error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters. +tests/cases/conformance/types/thisType/thisTypeInAccessors.ts(30,11): error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters. + + +==== tests/cases/conformance/types/thisType/thisTypeInAccessors.ts (8 errors) ==== + interface Foo { + n: number; + x: number; + } + + const explicit = { + n: 12, + get x(this: Foo): number { return this.n; }, + ~~~~~~~~~ +!!! error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters. + set x(this: Foo, n: number) { this.n = n; } + ~~~~~~~~~ +!!! error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters. + } + const copiedFromGetter = { + n: 14, + get x(this: Foo): number { return this.n; }, + ~~~~~~~~~ +!!! error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters. + set x(n) { this.n = n; } + } + const copiedFromSetter = { + n: 15, + get x() { return this.n }, + set x(this: Foo, n: number) { this.n = n; } + ~~~~~~~~~ +!!! error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters. + } + const copiedFromGetterUnannotated = { + n: 16, + get x(this: Foo) { return this.n }, + ~~~~~~~~~ +!!! error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters. + set x(this, n) { this.n = n; } + ~~~~ +!!! error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters. + } + + class Explicit { + n = 17; + get x(this: Foo): number { return this.n; } + ~~~~~~~~~ +!!! error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters. + set x(this: Foo, n: number) { this.n = n; } + ~~~~~~~~~ +!!! error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters. + } + class Contextual { + n = 21; + get x() { return this.n } // inside a class, so already correct + } + \ No newline at end of file diff --git a/tests/baselines/reference/thisTypeInAccessorsNegative.errors.txt b/tests/baselines/reference/thisTypeInAccessorsNegative.errors.txt index cd609673723..d7e4ae0628c 100644 --- a/tests/baselines/reference/thisTypeInAccessorsNegative.errors.txt +++ b/tests/baselines/reference/thisTypeInAccessorsNegative.errors.txt @@ -1,8 +1,10 @@ tests/cases/conformance/types/thisType/thisTypeInAccessorsNegative.ts(10,9): error TS2682: 'get' and 'set' accessor must have the same 'this' type. +tests/cases/conformance/types/thisType/thisTypeInAccessorsNegative.ts(10,11): error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters. tests/cases/conformance/types/thisType/thisTypeInAccessorsNegative.ts(11,9): error TS2682: 'get' and 'set' accessor must have the same 'this' type. +tests/cases/conformance/types/thisType/thisTypeInAccessorsNegative.ts(11,11): error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters. -==== tests/cases/conformance/types/thisType/thisTypeInAccessorsNegative.ts (2 errors) ==== +==== tests/cases/conformance/types/thisType/thisTypeInAccessorsNegative.ts (4 errors) ==== interface Foo { n: number; x: number; @@ -15,9 +17,13 @@ tests/cases/conformance/types/thisType/thisTypeInAccessorsNegative.ts(11,9): err get x(this: Foo) { return this.n; }, ~ !!! error TS2682: 'get' and 'set' accessor must have the same 'this' type. + ~~~~~~~~~ +!!! error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters. set x(this: Bar, n) { this.wrong = "method"; } ~ !!! error TS2682: 'get' and 'set' accessor must have the same 'this' type. + ~~~~~~~~~ +!!! error TS2784: 'get' and 'set' accessors cannot declare 'this' parameters. } const contextual: Foo = { n: 16,