Skip to content

Commit c76c418

Browse files
authored
Fix 'accessor' crash for invalid modifier locations (#58963)
1 parent 3743fbc commit c76c418

File tree

6 files changed

+240
-3
lines changed

6 files changed

+240
-3
lines changed

src/compiler/transformers/classFields.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,8 +464,6 @@ export function transformClassFields(context: TransformationContext): (x: Source
464464
}
465465

466466
switch (node.kind) {
467-
case SyntaxKind.AccessorKeyword:
468-
return Debug.fail("Use `modifierVisitor` instead.");
469467
case SyntaxKind.ClassDeclaration:
470468
return visitClassDeclaration(node as ClassDeclaration);
471469
case SyntaxKind.ClassExpression:
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
//// [tests/cases/conformance/classes/propertyMemberDeclarations/autoAccessorDisallowedModifiers.ts] ////
2+
3+
//// [autoAccessorDisallowedModifiers.ts]
4+
abstract class C1 {
5+
accessor accessor a: any;
6+
readonly accessor b: any;
7+
declare accessor c: any;
8+
accessor public d: any;
9+
accessor private e: any;
10+
accessor protected f: any;
11+
accessor abstract g: any;
12+
accessor static h: any;
13+
accessor i() {}
14+
accessor get j() { return false; }
15+
accessor set k(v: any) {}
16+
accessor constructor() {}
17+
accessor l?: any;
18+
accessor readonly m: any;
19+
accessor declare n: any;
20+
}
21+
22+
class C2 extends C1 {
23+
accessor override g: any;
24+
}
25+
26+
interface I1 {
27+
accessor a: number;
28+
}
29+
30+
accessor class C3 {}
31+
accessor interface I2 {}
32+
accessor namespace N1 {}
33+
accessor enum E1 {}
34+
accessor var V1: any;
35+
accessor type T1 = never;
36+
accessor function F1() {}
37+
accessor import "x";
38+
accessor import {} from "x";
39+
accessor export { V1 };
40+
accessor export default V1;
41+
accessor import N2 = N1;
42+
43+
44+
//// [autoAccessorDisallowedModifiers.js]
45+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
46+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
47+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
48+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
49+
};
50+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
51+
if (kind === "m") throw new TypeError("Private method is not writable");
52+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
53+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
54+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
55+
};
56+
var _a, _C1_a_accessor_storage, _C1_b_accessor_storage, _C1_d_accessor_storage, _C1_e_accessor_storage, _C1_f_accessor_storage, _C1_h_accessor_storage, _C1_l_accessor_storage, _C1_m_accessor_storage, _C2_g_accessor_storage;
57+
class C1 {
58+
get a() { return __classPrivateFieldGet(this, _C1_a_accessor_storage, "f"); }
59+
set a(value) { __classPrivateFieldSet(this, _C1_a_accessor_storage, value, "f"); }
60+
get b() { return __classPrivateFieldGet(this, _C1_b_accessor_storage, "f"); }
61+
set b(value) { __classPrivateFieldSet(this, _C1_b_accessor_storage, value, "f"); }
62+
get d() { return __classPrivateFieldGet(this, _C1_d_accessor_storage, "f"); }
63+
set d(value) { __classPrivateFieldSet(this, _C1_d_accessor_storage, value, "f"); }
64+
get e() { return __classPrivateFieldGet(this, _C1_e_accessor_storage, "f"); }
65+
set e(value) { __classPrivateFieldSet(this, _C1_e_accessor_storage, value, "f"); }
66+
get f() { return __classPrivateFieldGet(this, _C1_f_accessor_storage, "f"); }
67+
set f(value) { __classPrivateFieldSet(this, _C1_f_accessor_storage, value, "f"); }
68+
static get h() { return __classPrivateFieldGet(_a, _a, "f", _C1_h_accessor_storage); }
69+
static set h(value) { __classPrivateFieldSet(_a, _a, value, "f", _C1_h_accessor_storage); }
70+
i() { }
71+
get j() { return false; }
72+
set k(v) { }
73+
constructor() {
74+
_C1_a_accessor_storage.set(this, void 0);
75+
_C1_b_accessor_storage.set(this, void 0);
76+
_C1_d_accessor_storage.set(this, void 0);
77+
_C1_e_accessor_storage.set(this, void 0);
78+
_C1_f_accessor_storage.set(this, void 0);
79+
_C1_l_accessor_storage.set(this, void 0);
80+
_C1_m_accessor_storage.set(this, void 0);
81+
}
82+
get l() { return __classPrivateFieldGet(this, _C1_l_accessor_storage, "f"); }
83+
set l(value) { __classPrivateFieldSet(this, _C1_l_accessor_storage, value, "f"); }
84+
get m() { return __classPrivateFieldGet(this, _C1_m_accessor_storage, "f"); }
85+
set m(value) { __classPrivateFieldSet(this, _C1_m_accessor_storage, value, "f"); }
86+
}
87+
_a = C1, _C1_a_accessor_storage = new WeakMap(), _C1_b_accessor_storage = new WeakMap(), _C1_d_accessor_storage = new WeakMap(), _C1_e_accessor_storage = new WeakMap(), _C1_f_accessor_storage = new WeakMap(), _C1_l_accessor_storage = new WeakMap(), _C1_m_accessor_storage = new WeakMap();
88+
_C1_h_accessor_storage = { value: void 0 };
89+
class C2 extends C1 {
90+
constructor() {
91+
super(...arguments);
92+
_C2_g_accessor_storage.set(this, void 0);
93+
}
94+
get g() { return __classPrivateFieldGet(this, _C2_g_accessor_storage, "f"); }
95+
set g(value) { __classPrivateFieldSet(this, _C2_g_accessor_storage, value, "f"); }
96+
}
97+
_C2_g_accessor_storage = new WeakMap();
98+
class C3 {
99+
}
100+
accessor var E1;
101+
(function (E1) {
102+
})(E1 || (E1 = {}));
103+
accessor var V1;
104+
accessor function F1() { }
105+
accessor import "x";
106+
export { V1 };
107+
export default V1;
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
autoAccessorDisallowedModifiers.ts(2,14): error TS1030: 'accessor' modifier already seen.
2+
autoAccessorDisallowedModifiers.ts(3,14): error TS1243: 'accessor' modifier cannot be used with 'readonly' modifier.
3+
autoAccessorDisallowedModifiers.ts(4,13): error TS1243: 'accessor' modifier cannot be used with 'declare' modifier.
4+
autoAccessorDisallowedModifiers.ts(5,14): error TS1029: 'public' modifier must precede 'accessor' modifier.
5+
autoAccessorDisallowedModifiers.ts(6,14): error TS1029: 'private' modifier must precede 'accessor' modifier.
6+
autoAccessorDisallowedModifiers.ts(7,14): error TS1029: 'protected' modifier must precede 'accessor' modifier.
7+
autoAccessorDisallowedModifiers.ts(8,14): error TS1029: 'abstract' modifier must precede 'accessor' modifier.
8+
autoAccessorDisallowedModifiers.ts(9,14): error TS1029: 'static' modifier must precede 'accessor' modifier.
9+
autoAccessorDisallowedModifiers.ts(10,5): error TS1275: 'accessor' modifier can only appear on a property declaration.
10+
autoAccessorDisallowedModifiers.ts(11,5): error TS1275: 'accessor' modifier can only appear on a property declaration.
11+
autoAccessorDisallowedModifiers.ts(12,5): error TS1275: 'accessor' modifier can only appear on a property declaration.
12+
autoAccessorDisallowedModifiers.ts(13,5): error TS1275: 'accessor' modifier can only appear on a property declaration.
13+
autoAccessorDisallowedModifiers.ts(14,15): error TS1276: An 'accessor' property cannot be declared optional.
14+
autoAccessorDisallowedModifiers.ts(15,14): error TS1243: 'readonly' modifier cannot be used with 'accessor' modifier.
15+
autoAccessorDisallowedModifiers.ts(16,14): error TS1243: 'declare' modifier cannot be used with 'accessor' modifier.
16+
autoAccessorDisallowedModifiers.ts(20,14): error TS1029: 'override' modifier must precede 'accessor' modifier.
17+
autoAccessorDisallowedModifiers.ts(24,5): error TS1070: 'accessor' modifier cannot appear on a type member.
18+
autoAccessorDisallowedModifiers.ts(27,1): error TS1275: 'accessor' modifier can only appear on a property declaration.
19+
autoAccessorDisallowedModifiers.ts(28,1): error TS1275: 'accessor' modifier can only appear on a property declaration.
20+
autoAccessorDisallowedModifiers.ts(29,1): error TS1275: 'accessor' modifier can only appear on a property declaration.
21+
autoAccessorDisallowedModifiers.ts(30,1): error TS1275: 'accessor' modifier can only appear on a property declaration.
22+
autoAccessorDisallowedModifiers.ts(31,1): error TS1275: 'accessor' modifier can only appear on a property declaration.
23+
autoAccessorDisallowedModifiers.ts(32,1): error TS1275: 'accessor' modifier can only appear on a property declaration.
24+
autoAccessorDisallowedModifiers.ts(33,1): error TS1275: 'accessor' modifier can only appear on a property declaration.
25+
autoAccessorDisallowedModifiers.ts(34,1): error TS1275: 'accessor' modifier can only appear on a property declaration.
26+
autoAccessorDisallowedModifiers.ts(35,1): error TS1275: 'accessor' modifier can only appear on a property declaration.
27+
autoAccessorDisallowedModifiers.ts(35,25): error TS2792: Cannot find module 'x'. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option?
28+
autoAccessorDisallowedModifiers.ts(36,1): error TS1275: 'accessor' modifier can only appear on a property declaration.
29+
autoAccessorDisallowedModifiers.ts(37,1): error TS1275: 'accessor' modifier can only appear on a property declaration.
30+
autoAccessorDisallowedModifiers.ts(38,1): error TS1275: 'accessor' modifier can only appear on a property declaration.
31+
32+
33+
==== autoAccessorDisallowedModifiers.ts (30 errors) ====
34+
abstract class C1 {
35+
accessor accessor a: any;
36+
~~~~~~~~
37+
!!! error TS1030: 'accessor' modifier already seen.
38+
readonly accessor b: any;
39+
~~~~~~~~
40+
!!! error TS1243: 'accessor' modifier cannot be used with 'readonly' modifier.
41+
declare accessor c: any;
42+
~~~~~~~~
43+
!!! error TS1243: 'accessor' modifier cannot be used with 'declare' modifier.
44+
accessor public d: any;
45+
~~~~~~
46+
!!! error TS1029: 'public' modifier must precede 'accessor' modifier.
47+
accessor private e: any;
48+
~~~~~~~
49+
!!! error TS1029: 'private' modifier must precede 'accessor' modifier.
50+
accessor protected f: any;
51+
~~~~~~~~~
52+
!!! error TS1029: 'protected' modifier must precede 'accessor' modifier.
53+
accessor abstract g: any;
54+
~~~~~~~~
55+
!!! error TS1029: 'abstract' modifier must precede 'accessor' modifier.
56+
accessor static h: any;
57+
~~~~~~
58+
!!! error TS1029: 'static' modifier must precede 'accessor' modifier.
59+
accessor i() {}
60+
~~~~~~~~
61+
!!! error TS1275: 'accessor' modifier can only appear on a property declaration.
62+
accessor get j() { return false; }
63+
~~~~~~~~
64+
!!! error TS1275: 'accessor' modifier can only appear on a property declaration.
65+
accessor set k(v: any) {}
66+
~~~~~~~~
67+
!!! error TS1275: 'accessor' modifier can only appear on a property declaration.
68+
accessor constructor() {}
69+
~~~~~~~~
70+
!!! error TS1275: 'accessor' modifier can only appear on a property declaration.
71+
accessor l?: any;
72+
~
73+
!!! error TS1276: An 'accessor' property cannot be declared optional.
74+
accessor readonly m: any;
75+
~~~~~~~~
76+
!!! error TS1243: 'readonly' modifier cannot be used with 'accessor' modifier.
77+
accessor declare n: any;
78+
~~~~~~~
79+
!!! error TS1243: 'declare' modifier cannot be used with 'accessor' modifier.
80+
}
81+
82+
class C2 extends C1 {
83+
accessor override g: any;
84+
~~~~~~~~
85+
!!! error TS1029: 'override' modifier must precede 'accessor' modifier.
86+
}
87+
88+
interface I1 {
89+
accessor a: number;
90+
~~~~~~~~
91+
!!! error TS1070: 'accessor' modifier cannot appear on a type member.
92+
}
93+
94+
accessor class C3 {}
95+
~~~~~~~~
96+
!!! error TS1275: 'accessor' modifier can only appear on a property declaration.
97+
accessor interface I2 {}
98+
~~~~~~~~
99+
!!! error TS1275: 'accessor' modifier can only appear on a property declaration.
100+
accessor namespace N1 {}
101+
~~~~~~~~
102+
!!! error TS1275: 'accessor' modifier can only appear on a property declaration.
103+
accessor enum E1 {}
104+
~~~~~~~~
105+
!!! error TS1275: 'accessor' modifier can only appear on a property declaration.
106+
accessor var V1: any;
107+
~~~~~~~~
108+
!!! error TS1275: 'accessor' modifier can only appear on a property declaration.
109+
accessor type T1 = never;
110+
~~~~~~~~
111+
!!! error TS1275: 'accessor' modifier can only appear on a property declaration.
112+
accessor function F1() {}
113+
~~~~~~~~
114+
!!! error TS1275: 'accessor' modifier can only appear on a property declaration.
115+
accessor import "x";
116+
~~~~~~~~
117+
!!! error TS1275: 'accessor' modifier can only appear on a property declaration.
118+
accessor import {} from "x";
119+
~~~~~~~~
120+
!!! error TS1275: 'accessor' modifier can only appear on a property declaration.
121+
~~~
122+
!!! error TS2792: Cannot find module 'x'. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option?
123+
accessor export { V1 };
124+
~~~~~~~~
125+
!!! error TS1275: 'accessor' modifier can only appear on a property declaration.
126+
accessor export default V1;
127+
~~~~~~~~
128+
!!! error TS1275: 'accessor' modifier can only appear on a property declaration.
129+
accessor import N2 = N1;
130+
~~~~~~~~
131+
!!! error TS1275: 'accessor' modifier can only appear on a property declaration.
132+

tests/cases/conformance/classes/propertyMemberDeclarations/autoAccessorDisallowedModifiers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// @target: esnext
1+
// @target: esnext,es2017
22
// @noTypesAndSymbols: true
33

44
abstract class C1 {

0 commit comments

Comments
 (0)