Skip to content

Commit 64a0010

Browse files
authored
Limit decorator use-before-def check to ES decorators only (#57933)
1 parent 0d4414d commit 64a0010

File tree

3 files changed

+55
-70
lines changed

3 files changed

+55
-70
lines changed

src/compiler/checker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2838,15 +2838,15 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
28382838
const container = findAncestor(usage, n =>
28392839
n === declaration ? "quit" :
28402840
isComputedPropertyName(n) ? n.parent.parent === declaration :
2841-
isDecorator(n) && (n.parent === declaration ||
2841+
!legacyDecorators && isDecorator(n) && (n.parent === declaration ||
28422842
isMethodDeclaration(n.parent) && n.parent.parent === declaration ||
28432843
isGetOrSetAccessorDeclaration(n.parent) && n.parent.parent === declaration ||
28442844
isPropertyDeclaration(n.parent) && n.parent.parent === declaration ||
28452845
isParameter(n.parent) && n.parent.parent.parent === declaration));
28462846
if (!container) {
28472847
return true;
28482848
}
2849-
if (isDecorator(container)) {
2849+
if (!legacyDecorators && isDecorator(container)) {
28502850
return !!findAncestor(usage, n => n === container ? "quit" : isFunctionLike(n) && !getImmediatelyInvokedFunctionExpression(n));
28512851
}
28522852
return false;
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,37 @@
11
useBeforeDeclaration_classDecorators.2.ts(7,6): error TS2449: Class 'C2' used before its declaration.
22
useBeforeDeclaration_classDecorators.2.ts(10,13): error TS2449: Class 'C3' used before its declaration.
3-
useBeforeDeclaration_classDecorators.2.ts(16,5): error TS1207: Decorators cannot be applied to multiple get/set accessors of the same name.
4-
useBeforeDeclaration_classDecorators.2.ts(22,5): error TS1207: Decorators cannot be applied to multiple get/set accessors of the same name.
3+
useBeforeDeclaration_classDecorators.2.ts(26,17): error TS1206: Decorators are not valid here.
4+
useBeforeDeclaration_classDecorators.2.ts(27,20): error TS1206: Decorators are not valid here.
5+
useBeforeDeclaration_classDecorators.2.ts(28,13): error TS1206: Decorators are not valid here.
56
useBeforeDeclaration_classDecorators.2.ts(33,10): error TS2449: Class 'C5' used before its declaration.
67
useBeforeDeclaration_classDecorators.2.ts(34,10): error TS2449: Class 'C5' used before its declaration.
7-
useBeforeDeclaration_classDecorators.2.ts(35,5): error TS1207: Decorators cannot be applied to multiple get/set accessors of the same name.
88
useBeforeDeclaration_classDecorators.2.ts(35,10): error TS2449: Class 'C5' used before its declaration.
99
useBeforeDeclaration_classDecorators.2.ts(36,10): error TS2449: Class 'C5' used before its declaration.
1010
useBeforeDeclaration_classDecorators.2.ts(37,10): error TS2449: Class 'C5' used before its declaration.
1111
useBeforeDeclaration_classDecorators.2.ts(39,10): error TS2449: Class 'C5' used before its declaration.
1212
useBeforeDeclaration_classDecorators.2.ts(40,10): error TS2449: Class 'C5' used before its declaration.
13-
useBeforeDeclaration_classDecorators.2.ts(41,5): error TS1207: Decorators cannot be applied to multiple get/set accessors of the same name.
1413
useBeforeDeclaration_classDecorators.2.ts(41,10): error TS2449: Class 'C5' used before its declaration.
1514
useBeforeDeclaration_classDecorators.2.ts(42,10): error TS2449: Class 'C5' used before its declaration.
1615
useBeforeDeclaration_classDecorators.2.ts(43,10): error TS2449: Class 'C5' used before its declaration.
17-
useBeforeDeclaration_classDecorators.2.ts(46,22): error TS2449: Class 'C5' used before its declaration.
18-
useBeforeDeclaration_classDecorators.2.ts(47,25): error TS2449: Class 'C5' used before its declaration.
19-
useBeforeDeclaration_classDecorators.2.ts(48,18): error TS2449: Class 'C5' used before its declaration.
16+
useBeforeDeclaration_classDecorators.2.ts(46,17): error TS1206: Decorators are not valid here.
17+
useBeforeDeclaration_classDecorators.2.ts(47,20): error TS1206: Decorators are not valid here.
18+
useBeforeDeclaration_classDecorators.2.ts(48,13): error TS1206: Decorators are not valid here.
2019
useBeforeDeclaration_classDecorators.2.ts(53,17): error TS2449: Class 'C6' used before its declaration.
2120
useBeforeDeclaration_classDecorators.2.ts(54,17): error TS2449: Class 'C6' used before its declaration.
22-
useBeforeDeclaration_classDecorators.2.ts(55,5): error TS1207: Decorators cannot be applied to multiple get/set accessors of the same name.
2321
useBeforeDeclaration_classDecorators.2.ts(55,17): error TS2449: Class 'C6' used before its declaration.
2422
useBeforeDeclaration_classDecorators.2.ts(56,17): error TS2449: Class 'C6' used before its declaration.
2523
useBeforeDeclaration_classDecorators.2.ts(57,17): error TS2449: Class 'C6' used before its declaration.
2624
useBeforeDeclaration_classDecorators.2.ts(59,17): error TS2449: Class 'C6' used before its declaration.
2725
useBeforeDeclaration_classDecorators.2.ts(60,17): error TS2449: Class 'C6' used before its declaration.
28-
useBeforeDeclaration_classDecorators.2.ts(61,5): error TS1207: Decorators cannot be applied to multiple get/set accessors of the same name.
2926
useBeforeDeclaration_classDecorators.2.ts(61,17): error TS2449: Class 'C6' used before its declaration.
3027
useBeforeDeclaration_classDecorators.2.ts(62,17): error TS2449: Class 'C6' used before its declaration.
3128
useBeforeDeclaration_classDecorators.2.ts(63,17): error TS2449: Class 'C6' used before its declaration.
32-
useBeforeDeclaration_classDecorators.2.ts(66,29): error TS2449: Class 'C6' used before its declaration.
33-
useBeforeDeclaration_classDecorators.2.ts(67,32): error TS2449: Class 'C6' used before its declaration.
34-
useBeforeDeclaration_classDecorators.2.ts(68,25): error TS2449: Class 'C6' used before its declaration.
29+
useBeforeDeclaration_classDecorators.2.ts(66,17): error TS1206: Decorators are not valid here.
30+
useBeforeDeclaration_classDecorators.2.ts(67,20): error TS1206: Decorators are not valid here.
31+
useBeforeDeclaration_classDecorators.2.ts(68,13): error TS1206: Decorators are not valid here.
3532

3633

37-
==== useBeforeDeclaration_classDecorators.2.ts (34 errors) ====
34+
==== useBeforeDeclaration_classDecorators.2.ts (31 errors) ====
3835
declare const dec: any;
3936

4037
// ok
@@ -55,24 +52,26 @@ useBeforeDeclaration_classDecorators.2.ts(68,25): error TS2449: Class 'C6' used
5552
// ok
5653
class C4 {
5754
@dec(() => C4) static method() {}
58-
@dec(() => C4) static get x() { return this.y; }
59-
@dec(() => C4) static set x(v) {}
60-
~
61-
!!! error TS1207: Decorators cannot be applied to multiple get/set accessors of the same name.
55+
@dec(() => C4) static get w() { return this.y; }
56+
@dec(() => C4) static set x(v: any) {}
6257
@dec(() => C4) static y: any;
6358
@dec(() => C4) static accessor z: any;
6459

6560
@dec(() => C4) method() {}
66-
@dec(() => C4) get x() { return this.y; }
67-
@dec(() => C4) set x(v) {}
68-
~
69-
!!! error TS1207: Decorators cannot be applied to multiple get/set accessors of the same name.
61+
@dec(() => C4) get w() { return this.y; }
62+
@dec(() => C4) set x(v: any) {}
7063
@dec(() => C4) y: any;
7164
@dec(() => C4) accessor z: any;
7265

7366
constructor(@dec(() => C4) a: any) {}
67+
~
68+
!!! error TS1206: Decorators are not valid here.
7469
static method2(@dec(() => C4) a: any) {}
70+
~
71+
!!! error TS1206: Decorators are not valid here.
7572
method2(@dec(() => C4) a: any) {}
73+
~
74+
!!! error TS1206: Decorators are not valid here.
7675
}
7776

7877
// error
@@ -81,13 +80,11 @@ useBeforeDeclaration_classDecorators.2.ts(68,25): error TS2449: Class 'C6' used
8180
~~
8281
!!! error TS2449: Class 'C5' used before its declaration.
8382
!!! related TS2728 useBeforeDeclaration_classDecorators.2.ts:32:7: 'C5' is declared here.
84-
@dec(C5) static get x() { return this.y; }
83+
@dec(C5) static get w() { return this.y; }
8584
~~
8685
!!! error TS2449: Class 'C5' used before its declaration.
8786
!!! related TS2728 useBeforeDeclaration_classDecorators.2.ts:32:7: 'C5' is declared here.
88-
@dec(C5) static set x(v) {}
89-
~
90-
!!! error TS1207: Decorators cannot be applied to multiple get/set accessors of the same name.
87+
@dec(C5) static set x(v: any) {}
9188
~~
9289
!!! error TS2449: Class 'C5' used before its declaration.
9390
!!! related TS2728 useBeforeDeclaration_classDecorators.2.ts:32:7: 'C5' is declared here.
@@ -104,13 +101,11 @@ useBeforeDeclaration_classDecorators.2.ts(68,25): error TS2449: Class 'C6' used
104101
~~
105102
!!! error TS2449: Class 'C5' used before its declaration.
106103
!!! related TS2728 useBeforeDeclaration_classDecorators.2.ts:32:7: 'C5' is declared here.
107-
@dec(C5) get x() { return this.y; }
104+
@dec(C5) get w() { return this.y; }
108105
~~
109106
!!! error TS2449: Class 'C5' used before its declaration.
110107
!!! related TS2728 useBeforeDeclaration_classDecorators.2.ts:32:7: 'C5' is declared here.
111-
@dec(C5) set x(v) {}
112-
~
113-
!!! error TS1207: Decorators cannot be applied to multiple get/set accessors of the same name.
108+
@dec(C5) set x(v: any) {}
114109
~~
115110
!!! error TS2449: Class 'C5' used before its declaration.
116111
!!! related TS2728 useBeforeDeclaration_classDecorators.2.ts:32:7: 'C5' is declared here.
@@ -125,17 +120,14 @@ useBeforeDeclaration_classDecorators.2.ts(68,25): error TS2449: Class 'C6' used
125120

126121

127122
constructor(@dec(C5) a: any) {}
128-
~~
129-
!!! error TS2449: Class 'C5' used before its declaration.
130-
!!! related TS2728 useBeforeDeclaration_classDecorators.2.ts:32:7: 'C5' is declared here.
123+
~
124+
!!! error TS1206: Decorators are not valid here.
131125
static method2(@dec(C5) a: any) {}
132-
~~
133-
!!! error TS2449: Class 'C5' used before its declaration.
134-
!!! related TS2728 useBeforeDeclaration_classDecorators.2.ts:32:7: 'C5' is declared here.
126+
~
127+
!!! error TS1206: Decorators are not valid here.
135128
method2(@dec(C5) a: any) {}
136-
~~
137-
!!! error TS2449: Class 'C5' used before its declaration.
138-
!!! related TS2728 useBeforeDeclaration_classDecorators.2.ts:32:7: 'C5' is declared here.
129+
~
130+
!!! error TS1206: Decorators are not valid here.
139131
}
140132

141133
// error
@@ -144,13 +136,11 @@ useBeforeDeclaration_classDecorators.2.ts(68,25): error TS2449: Class 'C6' used
144136
~~
145137
!!! error TS2449: Class 'C6' used before its declaration.
146138
!!! related TS2728 useBeforeDeclaration_classDecorators.2.ts:52:7: 'C6' is declared here.
147-
@dec((() => C6)()) static get x() { return this.y; }
139+
@dec((() => C6)()) static get w() { return this.y; }
148140
~~
149141
!!! error TS2449: Class 'C6' used before its declaration.
150142
!!! related TS2728 useBeforeDeclaration_classDecorators.2.ts:52:7: 'C6' is declared here.
151-
@dec((() => C6)()) static set x(v) {}
152-
~
153-
!!! error TS1207: Decorators cannot be applied to multiple get/set accessors of the same name.
143+
@dec((() => C6)()) static set x(v: any) {}
154144
~~
155145
!!! error TS2449: Class 'C6' used before its declaration.
156146
!!! related TS2728 useBeforeDeclaration_classDecorators.2.ts:52:7: 'C6' is declared here.
@@ -167,13 +157,11 @@ useBeforeDeclaration_classDecorators.2.ts(68,25): error TS2449: Class 'C6' used
167157
~~
168158
!!! error TS2449: Class 'C6' used before its declaration.
169159
!!! related TS2728 useBeforeDeclaration_classDecorators.2.ts:52:7: 'C6' is declared here.
170-
@dec((() => C6)()) get x() { return this.y; }
160+
@dec((() => C6)()) get w() { return this.y; }
171161
~~
172162
!!! error TS2449: Class 'C6' used before its declaration.
173163
!!! related TS2728 useBeforeDeclaration_classDecorators.2.ts:52:7: 'C6' is declared here.
174-
@dec((() => C6)()) set x(v) {}
175-
~
176-
!!! error TS1207: Decorators cannot be applied to multiple get/set accessors of the same name.
164+
@dec((() => C6)()) set x(v: any) {}
177165
~~
178166
!!! error TS2449: Class 'C6' used before its declaration.
179167
!!! related TS2728 useBeforeDeclaration_classDecorators.2.ts:52:7: 'C6' is declared here.
@@ -188,16 +176,13 @@ useBeforeDeclaration_classDecorators.2.ts(68,25): error TS2449: Class 'C6' used
188176

189177

190178
constructor(@dec((() => C6)()) a: any) {}
191-
~~
192-
!!! error TS2449: Class 'C6' used before its declaration.
193-
!!! related TS2728 useBeforeDeclaration_classDecorators.2.ts:52:7: 'C6' is declared here.
179+
~
180+
!!! error TS1206: Decorators are not valid here.
194181
static method2(@dec((() => C6)()) a: any) {}
195-
~~
196-
!!! error TS2449: Class 'C6' used before its declaration.
197-
!!! related TS2728 useBeforeDeclaration_classDecorators.2.ts:52:7: 'C6' is declared here.
182+
~
183+
!!! error TS1206: Decorators are not valid here.
198184
method2(@dec((() => C6)()) a: any) {}
199-
~~
200-
!!! error TS2449: Class 'C6' used before its declaration.
201-
!!! related TS2728 useBeforeDeclaration_classDecorators.2.ts:52:7: 'C6' is declared here.
185+
~
186+
!!! error TS1206: Decorators are not valid here.
202187
}
203188

tests/cases/compiler/useBeforeDeclaration_classDecorators.2.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// @target: esnext
22
// @noEmit: true
33
// @noTypesAndSymbols: true
4-
// @experimentalDecorators: true
4+
// @experimentalDecorators: *
55

66
declare const dec: any;
77

@@ -17,14 +17,14 @@ declare const dec: any;
1717
// ok
1818
class C4 {
1919
@dec(() => C4) static method() {}
20-
@dec(() => C4) static get x() { return this.y; }
21-
@dec(() => C4) static set x(v) {}
20+
@dec(() => C4) static get w() { return this.y; }
21+
@dec(() => C4) static set x(v: any) {}
2222
@dec(() => C4) static y: any;
2323
@dec(() => C4) static accessor z: any;
2424

2525
@dec(() => C4) method() {}
26-
@dec(() => C4) get x() { return this.y; }
27-
@dec(() => C4) set x(v) {}
26+
@dec(() => C4) get w() { return this.y; }
27+
@dec(() => C4) set x(v: any) {}
2828
@dec(() => C4) y: any;
2929
@dec(() => C4) accessor z: any;
3030

@@ -36,14 +36,14 @@ class C4 {
3636
// error
3737
class C5 {
3838
@dec(C5) static method() {}
39-
@dec(C5) static get x() { return this.y; }
40-
@dec(C5) static set x(v) {}
39+
@dec(C5) static get w() { return this.y; }
40+
@dec(C5) static set x(v: any) {}
4141
@dec(C5) static y: any;
4242
@dec(C5) static accessor z: any;
4343

4444
@dec(C5) method() {}
45-
@dec(C5) get x() { return this.y; }
46-
@dec(C5) set x(v) {}
45+
@dec(C5) get w() { return this.y; }
46+
@dec(C5) set x(v: any) {}
4747
@dec(C5) y: any;
4848
@dec(C5) accessor z: any;
4949

@@ -56,14 +56,14 @@ class C5 {
5656
// error
5757
class C6 {
5858
@dec((() => C6)()) static method() {}
59-
@dec((() => C6)()) static get x() { return this.y; }
60-
@dec((() => C6)()) static set x(v) {}
59+
@dec((() => C6)()) static get w() { return this.y; }
60+
@dec((() => C6)()) static set x(v: any) {}
6161
@dec((() => C6)()) static y: any;
6262
@dec((() => C6)()) static accessor z: any;
6363

6464
@dec((() => C6)()) method() {}
65-
@dec((() => C6)()) get x() { return this.y; }
66-
@dec((() => C6)()) set x(v) {}
65+
@dec((() => C6)()) get w() { return this.y; }
66+
@dec((() => C6)()) set x(v: any) {}
6767
@dec((() => C6)()) y: any;
6868
@dec((() => C6)()) accessor z: any;
6969

0 commit comments

Comments
 (0)