From 7eca4bc9be0940f4213d13a9b91606d1df93d3b6 Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Wed, 7 Dec 2016 15:48:23 -0800 Subject: [PATCH] Fix decorator emit for accessors --- src/compiler/transformers/ts.ts | 5 +- .../decoratorOnClassAccessor7.errors.txt | 41 ++++++ .../reference/decoratorOnClassAccessor7.js | 125 ++++++++++++++++++ .../accessor/decoratorOnClassAccessor7.ts | 34 +++++ 4 files changed, 203 insertions(+), 2 deletions(-) create mode 100644 tests/baselines/reference/decoratorOnClassAccessor7.errors.txt create mode 100644 tests/baselines/reference/decoratorOnClassAccessor7.js create mode 100644 tests/cases/conformance/decorators/class/accessor/decoratorOnClassAccessor7.ts diff --git a/src/compiler/transformers/ts.ts b/src/compiler/transformers/ts.ts index 749ea46c8f..77af854269 100644 --- a/src/compiler/transformers/ts.ts +++ b/src/compiler/transformers/ts.ts @@ -1223,11 +1223,12 @@ namespace ts { } const { firstAccessor, secondAccessor, setAccessor } = getAllAccessorDeclarations(node.members, accessor); - if (accessor !== firstAccessor) { + const firstAccessorWithDecorators = firstAccessor.decorators ? firstAccessor : secondAccessor && secondAccessor.decorators ? secondAccessor : undefined; + if (!firstAccessorWithDecorators || accessor !== firstAccessorWithDecorators) { return undefined; } - const decorators = firstAccessor.decorators || (secondAccessor && secondAccessor.decorators); + const decorators = firstAccessorWithDecorators.decorators; const parameters = getDecoratorsOfParameters(setAccessor); if (!decorators && !parameters) { return undefined; diff --git a/tests/baselines/reference/decoratorOnClassAccessor7.errors.txt b/tests/baselines/reference/decoratorOnClassAccessor7.errors.txt new file mode 100644 index 0000000000..d776297fcf --- /dev/null +++ b/tests/baselines/reference/decoratorOnClassAccessor7.errors.txt @@ -0,0 +1,41 @@ +tests/cases/conformance/decorators/class/accessor/decoratorOnClassAccessor7.ts(26,5): error TS1207: Decorators cannot be applied to multiple get/set accessors of the same name. +tests/cases/conformance/decorators/class/accessor/decoratorOnClassAccessor7.ts(31,5): error TS1207: Decorators cannot be applied to multiple get/set accessors of the same name. + + +==== tests/cases/conformance/decorators/class/accessor/decoratorOnClassAccessor7.ts (2 errors) ==== + declare function dec1(target: any, propertyKey: string, descriptor: TypedPropertyDescriptor): TypedPropertyDescriptor; + declare function dec2(target: any, propertyKey: string, descriptor: TypedPropertyDescriptor): TypedPropertyDescriptor; + + class A { + @dec1 get x() { return 0; } + set x(value: number) { } + } + + class B { + get x() { return 0; } + @dec2 set x(value: number) { } + } + + class C { + @dec1 set x(value: number) { } + get x() { return 0; } + } + + class D { + set x(value: number) { } + @dec2 get x() { return 0; } + } + + class E { + @dec1 get x() { return 0; } + @dec2 set x(value: number) { } + ~ +!!! error TS1207: Decorators cannot be applied to multiple get/set accessors of the same name. + } + + class F { + @dec1 set x(value: number) { } + @dec2 get x() { return 0; } + ~ +!!! error TS1207: Decorators cannot be applied to multiple get/set accessors of the same name. + } \ No newline at end of file diff --git a/tests/baselines/reference/decoratorOnClassAccessor7.js b/tests/baselines/reference/decoratorOnClassAccessor7.js new file mode 100644 index 0000000000..5ae0c29c14 --- /dev/null +++ b/tests/baselines/reference/decoratorOnClassAccessor7.js @@ -0,0 +1,125 @@ +//// [decoratorOnClassAccessor7.ts] +declare function dec1(target: any, propertyKey: string, descriptor: TypedPropertyDescriptor): TypedPropertyDescriptor; +declare function dec2(target: any, propertyKey: string, descriptor: TypedPropertyDescriptor): TypedPropertyDescriptor; + +class A { + @dec1 get x() { return 0; } + set x(value: number) { } +} + +class B { + get x() { return 0; } + @dec2 set x(value: number) { } +} + +class C { + @dec1 set x(value: number) { } + get x() { return 0; } +} + +class D { + set x(value: number) { } + @dec2 get x() { return 0; } +} + +class E { + @dec1 get x() { return 0; } + @dec2 set x(value: number) { } +} + +class F { + @dec1 set x(value: number) { } + @dec2 get x() { return 0; } +} + +//// [decoratorOnClassAccessor7.js] +var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { + var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; + if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); + else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; + return c > 3 && r && Object.defineProperty(target, key, r), r; +}; +var A = (function () { + function A() { + } + Object.defineProperty(A.prototype, "x", { + get: function () { return 0; }, + set: function (value) { }, + enumerable: true, + configurable: true + }); + return A; +}()); +__decorate([ + dec1 +], A.prototype, "x", null); +var B = (function () { + function B() { + } + Object.defineProperty(B.prototype, "x", { + get: function () { return 0; }, + set: function (value) { }, + enumerable: true, + configurable: true + }); + return B; +}()); +__decorate([ + dec2 +], B.prototype, "x", null); +var C = (function () { + function C() { + } + Object.defineProperty(C.prototype, "x", { + get: function () { return 0; }, + set: function (value) { }, + enumerable: true, + configurable: true + }); + return C; +}()); +__decorate([ + dec1 +], C.prototype, "x", null); +var D = (function () { + function D() { + } + Object.defineProperty(D.prototype, "x", { + get: function () { return 0; }, + set: function (value) { }, + enumerable: true, + configurable: true + }); + return D; +}()); +__decorate([ + dec2 +], D.prototype, "x", null); +var E = (function () { + function E() { + } + Object.defineProperty(E.prototype, "x", { + get: function () { return 0; }, + set: function (value) { }, + enumerable: true, + configurable: true + }); + return E; +}()); +__decorate([ + dec1 +], E.prototype, "x", null); +var F = (function () { + function F() { + } + Object.defineProperty(F.prototype, "x", { + get: function () { return 0; }, + set: function (value) { }, + enumerable: true, + configurable: true + }); + return F; +}()); +__decorate([ + dec1 +], F.prototype, "x", null); diff --git a/tests/cases/conformance/decorators/class/accessor/decoratorOnClassAccessor7.ts b/tests/cases/conformance/decorators/class/accessor/decoratorOnClassAccessor7.ts new file mode 100644 index 0000000000..b20a5a85df --- /dev/null +++ b/tests/cases/conformance/decorators/class/accessor/decoratorOnClassAccessor7.ts @@ -0,0 +1,34 @@ +// @target:es5 +// @experimentaldecorators: true +declare function dec1(target: any, propertyKey: string, descriptor: TypedPropertyDescriptor): TypedPropertyDescriptor; +declare function dec2(target: any, propertyKey: string, descriptor: TypedPropertyDescriptor): TypedPropertyDescriptor; + +class A { + @dec1 get x() { return 0; } + set x(value: number) { } +} + +class B { + get x() { return 0; } + @dec2 set x(value: number) { } +} + +class C { + @dec1 set x(value: number) { } + get x() { return 0; } +} + +class D { + set x(value: number) { } + @dec2 get x() { return 0; } +} + +class E { + @dec1 get x() { return 0; } + @dec2 set x(value: number) { } +} + +class F { + @dec1 set x(value: number) { } + @dec2 get x() { return 0; } +} \ No newline at end of file