Skip to content

Commit da4d41f

Browse files
authored
Merge pull request #12740 from Microsoft/fix12727
Fix decorator emit for accessors
2 parents b0bbbcb + 7eca4bc commit da4d41f

File tree

4 files changed

+203
-2
lines changed

4 files changed

+203
-2
lines changed

src/compiler/transformers/ts.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,11 +1223,12 @@ namespace ts {
12231223
}
12241224

12251225
const { firstAccessor, secondAccessor, setAccessor } = getAllAccessorDeclarations(node.members, accessor);
1226-
if (accessor !== firstAccessor) {
1226+
const firstAccessorWithDecorators = firstAccessor.decorators ? firstAccessor : secondAccessor && secondAccessor.decorators ? secondAccessor : undefined;
1227+
if (!firstAccessorWithDecorators || accessor !== firstAccessorWithDecorators) {
12271228
return undefined;
12281229
}
12291230

1230-
const decorators = firstAccessor.decorators || (secondAccessor && secondAccessor.decorators);
1231+
const decorators = firstAccessorWithDecorators.decorators;
12311232
const parameters = getDecoratorsOfParameters(setAccessor);
12321233
if (!decorators && !parameters) {
12331234
return undefined;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
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.
2+
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.
3+
4+
5+
==== tests/cases/conformance/decorators/class/accessor/decoratorOnClassAccessor7.ts (2 errors) ====
6+
declare function dec1<T>(target: any, propertyKey: string, descriptor: TypedPropertyDescriptor<T>): TypedPropertyDescriptor<T>;
7+
declare function dec2<T>(target: any, propertyKey: string, descriptor: TypedPropertyDescriptor<T>): TypedPropertyDescriptor<T>;
8+
9+
class A {
10+
@dec1 get x() { return 0; }
11+
set x(value: number) { }
12+
}
13+
14+
class B {
15+
get x() { return 0; }
16+
@dec2 set x(value: number) { }
17+
}
18+
19+
class C {
20+
@dec1 set x(value: number) { }
21+
get x() { return 0; }
22+
}
23+
24+
class D {
25+
set x(value: number) { }
26+
@dec2 get x() { return 0; }
27+
}
28+
29+
class E {
30+
@dec1 get x() { return 0; }
31+
@dec2 set x(value: number) { }
32+
~
33+
!!! error TS1207: Decorators cannot be applied to multiple get/set accessors of the same name.
34+
}
35+
36+
class F {
37+
@dec1 set x(value: number) { }
38+
@dec2 get x() { return 0; }
39+
~
40+
!!! error TS1207: Decorators cannot be applied to multiple get/set accessors of the same name.
41+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
//// [decoratorOnClassAccessor7.ts]
2+
declare function dec1<T>(target: any, propertyKey: string, descriptor: TypedPropertyDescriptor<T>): TypedPropertyDescriptor<T>;
3+
declare function dec2<T>(target: any, propertyKey: string, descriptor: TypedPropertyDescriptor<T>): TypedPropertyDescriptor<T>;
4+
5+
class A {
6+
@dec1 get x() { return 0; }
7+
set x(value: number) { }
8+
}
9+
10+
class B {
11+
get x() { return 0; }
12+
@dec2 set x(value: number) { }
13+
}
14+
15+
class C {
16+
@dec1 set x(value: number) { }
17+
get x() { return 0; }
18+
}
19+
20+
class D {
21+
set x(value: number) { }
22+
@dec2 get x() { return 0; }
23+
}
24+
25+
class E {
26+
@dec1 get x() { return 0; }
27+
@dec2 set x(value: number) { }
28+
}
29+
30+
class F {
31+
@dec1 set x(value: number) { }
32+
@dec2 get x() { return 0; }
33+
}
34+
35+
//// [decoratorOnClassAccessor7.js]
36+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
37+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
38+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
39+
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;
40+
return c > 3 && r && Object.defineProperty(target, key, r), r;
41+
};
42+
var A = (function () {
43+
function A() {
44+
}
45+
Object.defineProperty(A.prototype, "x", {
46+
get: function () { return 0; },
47+
set: function (value) { },
48+
enumerable: true,
49+
configurable: true
50+
});
51+
return A;
52+
}());
53+
__decorate([
54+
dec1
55+
], A.prototype, "x", null);
56+
var B = (function () {
57+
function B() {
58+
}
59+
Object.defineProperty(B.prototype, "x", {
60+
get: function () { return 0; },
61+
set: function (value) { },
62+
enumerable: true,
63+
configurable: true
64+
});
65+
return B;
66+
}());
67+
__decorate([
68+
dec2
69+
], B.prototype, "x", null);
70+
var C = (function () {
71+
function C() {
72+
}
73+
Object.defineProperty(C.prototype, "x", {
74+
get: function () { return 0; },
75+
set: function (value) { },
76+
enumerable: true,
77+
configurable: true
78+
});
79+
return C;
80+
}());
81+
__decorate([
82+
dec1
83+
], C.prototype, "x", null);
84+
var D = (function () {
85+
function D() {
86+
}
87+
Object.defineProperty(D.prototype, "x", {
88+
get: function () { return 0; },
89+
set: function (value) { },
90+
enumerable: true,
91+
configurable: true
92+
});
93+
return D;
94+
}());
95+
__decorate([
96+
dec2
97+
], D.prototype, "x", null);
98+
var E = (function () {
99+
function E() {
100+
}
101+
Object.defineProperty(E.prototype, "x", {
102+
get: function () { return 0; },
103+
set: function (value) { },
104+
enumerable: true,
105+
configurable: true
106+
});
107+
return E;
108+
}());
109+
__decorate([
110+
dec1
111+
], E.prototype, "x", null);
112+
var F = (function () {
113+
function F() {
114+
}
115+
Object.defineProperty(F.prototype, "x", {
116+
get: function () { return 0; },
117+
set: function (value) { },
118+
enumerable: true,
119+
configurable: true
120+
});
121+
return F;
122+
}());
123+
__decorate([
124+
dec1
125+
], F.prototype, "x", null);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// @target:es5
2+
// @experimentaldecorators: true
3+
declare function dec1<T>(target: any, propertyKey: string, descriptor: TypedPropertyDescriptor<T>): TypedPropertyDescriptor<T>;
4+
declare function dec2<T>(target: any, propertyKey: string, descriptor: TypedPropertyDescriptor<T>): TypedPropertyDescriptor<T>;
5+
6+
class A {
7+
@dec1 get x() { return 0; }
8+
set x(value: number) { }
9+
}
10+
11+
class B {
12+
get x() { return 0; }
13+
@dec2 set x(value: number) { }
14+
}
15+
16+
class C {
17+
@dec1 set x(value: number) { }
18+
get x() { return 0; }
19+
}
20+
21+
class D {
22+
set x(value: number) { }
23+
@dec2 get x() { return 0; }
24+
}
25+
26+
class E {
27+
@dec1 get x() { return 0; }
28+
@dec2 set x(value: number) { }
29+
}
30+
31+
class F {
32+
@dec1 set x(value: number) { }
33+
@dec2 get x() { return 0; }
34+
}

0 commit comments

Comments
 (0)