1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
// |jit-test| skip-if: !getBuildConfiguration("decorators")
load(libdir + "asserts.js");
Reflect.parse("class c {@dec1 field = false;}");
Reflect.parse("class c {@dec1 @dec2 @dec3 field = false;}");
Reflect.parse("class c {@dec1 @dec2 @dec3('a') static field = false;}");
Reflect.parse("class c {@dec1 method() {};}");
Reflect.parse("class c {@dec1 @dec2 @dec3 method() {};}");
Reflect.parse("class c {@dec1 @dec2 @dec3 static method() {};}");
Reflect.parse("class c {@dec1 @dec2('a') @dec3 method(...args) {};}");
Reflect.parse("class c {@((a, b, c) => {}) method(a, b) {};}");
Reflect.parse("class c {@((a, b, c) => {}) @dec2('a', 'b') @dec3 method(a, b) {};}");
Reflect.parse("@dec1 class c {}");
Reflect.parse("@dec1 @dec2 @dec3 class c {}");
Reflect.parse("@dec1('a') @(() => {}) @dec3 class c {}");
Reflect.parse("@dec1('a') @(() => {}) @dec3 class c {@((a, b, c) => {}) @dec2('a', 'b') @dec3 method(a, b) {};}");
Reflect.parse("x = @dec class { #x }");
Reflect.parse("x = (class A { }, @dec class { })");
Reflect.parse("@dec1 class A extends @dec2 class B extends @dec3 class {} {} {}");
Reflect.parse("class c {@dec1.dec2.dec3 method() {};}");
Reflect.parse("class c {@dec1 @dec2.dec3.dec4 @dec5 method() {};}");
Reflect.parse("class c {@dec1('a') @dec2.dec3.dec4 @dec5 method() {};}");
Reflect.parse("class c {@dec1('a') @dec2.dec3.dec4('b', 'c') @dec5 method() {};}");
Reflect.parse("class c {@dec1('a') @dec2.dec3.dec4('b', 'c') @dec5.dec6 method() {};}");
Reflect.parse("@dec1.dec2 class c {}");
Reflect.parse("@dec1.dec2 @dec3.dec4 @dec5 class c {}");
Reflect.parse("@dec1.dec2('a') @(() => {}) @dec4 class c {}");
Reflect.parse("@dec1.dec2('a') @(() => {}) @dec4 class c {@((a, b, c) => {}) @dec5.dec6('a', 'b') @dec7 method(a, b) {};}");
Reflect.parse("class c {accessor field = false;}");
Reflect.parse("class c {static accessor field = false;}");
Reflect.parse("class c {@dec1 @dec2 accessor field = false;}");
Reflect.parse("class c {@dec1 @dec2 @dec3 static accessor field = false;}");
Reflect.parse("let accessor = false;");
Reflect.parse("class accessor {accessor = false;}");
Reflect.parse("class c {accessor = false;}");
Reflect.parse("class c {accessor\n= false;}");
Reflect.parse("class c {accessor\nfield = false;}");
Reflect.parse("class C {accessor\n foo() {}}");
Reflect.parse("class c {accessor; }");
Reflect.parse("class c {accessor\nset field(a) {}}");
Reflect.parse("class c {accessor\nasync field(a) {}}");
Reflect.parse("class c {accessor\n* field(a) {}}");
Reflect.parse("{accessor, field(a)}");
Reflect.parse("class c {static accessor x = 1;}");
Reflect.parse("class c {accessor #y = 2;}");
Reflect.parse("class c {static dec1(){}; static {@c.dec1 class d{@c.dec1 field}}}");
Reflect.parse("class c {static #dec1(){}; static {@c.#dec1 class d{@c.#dec1 field}}}");
Reflect.parse("class c {@as field = false;}");
Reflect.parse("class c {@accessor field = false;}");
Reflect.parse("class c {@assert field = false;}");
Reflect.parse("class c {@async field = false;}");
Reflect.parse("class c {@await field = false;}");
Reflect.parse("class c {@each field = false;}");
Reflect.parse("class c {@from field = false;}");
Reflect.parse("class c {@get field = false;}");
Reflect.parse("class c {@meta field = false;}");
Reflect.parse("class c {@of field = false;}");
Reflect.parse("class c {@set field = false;}");
Reflect.parse("class c {@target field = false;}");
assertThrowsInstanceOf(() => Reflect.parse("class c {@ method() {};}"), SyntaxError);
assertThrowsInstanceOf(() => Reflect.parse("class c {@((a, b, c => {}) method(a, b) {};}"), SyntaxError);
assertThrowsInstanceOf(() => Reflect.parse("class c {@dec1 static @dec2 method(a, b) {};}"), SyntaxError);
assertThrowsInstanceOf(() => Reflect.parse("@dec1 let x = 1"), SyntaxError);
assertThrowsInstanceOf(() => Reflect.parse("@dec1 f(a) {}"), SyntaxError);
assertThrowsInstanceOf(() => Reflect.parse("@dec1 () => {}"), SyntaxError);
assertThrowsInstanceOf(() => Reflect.parse("@class class { x; }"), SyntaxError);
assertThrowsInstanceOf(() => Reflect.parse("@for class { x; }"), SyntaxError);
assertThrowsInstanceOf(() => Reflect.parse("class c {@dec1. method() {};}"), SyntaxError);
assertThrowsInstanceOf(() => Reflect.parse("@dec1. class c {method() {};}"), SyntaxError);
assertThrowsInstanceOf(() => Reflect.parse("@dec1.(a) class c {method() {};}"), SyntaxError);
assertThrowsInstanceOf(() => Reflect.parse("class c {accessor method() {};}"), SyntaxError);
assertThrowsInstanceOf(() => Reflect.parse("class c {accessor @dec1 field = false;}"), SyntaxError);
assertThrowsInstanceOf(() => Reflect.parse("class c {accessor, }"), SyntaxError);
assertThrowsInstanceOf(() => Reflect.parse("({ accessor field: 10 })"), SyntaxError);
assertThrowsInstanceOf(() => Reflect.parse("({ accessor foo });"), SyntaxError);
assertThrowsInstanceOf(() => Reflect.parse("({ accessor foo = 10 } = {});"), SyntaxError);
assertThrowsInstanceOf(() => Reflect.parse("class c {async accessor foo() {}}"), SyntaxError);
assertThrowsInstanceOf(() => Reflect.parse("class c {accessor set field(a) {}}"), SyntaxError);
assertThrowsInstanceOf(() => Reflect.parse("class c {accessor *field(a) {}}"), SyntaxError);
assertThrowsInstanceOf(() => Reflect.parse("class c {accessor *field(a) {}}"), SyntaxError);
assertThrowsInstanceOf(() => Reflect.parse("class c {@dec[0] x}"), SyntaxError);
assertThrowsInstanceOf(() => Reflect.parse("class c {@new dec() x}"), SyntaxError);
assertThrowsInstanceOf(() => Reflect.parse("class c {@super x}"), SyntaxError);
assertThrowsInstanceOf(() => Reflect.parse("class c {@super() x}"), SyntaxError);
assertThrowsInstanceOf(() => Reflect.parse("class c {@super.dec() x}"), SyntaxError);
assertThrowsInstanceOf(() => Reflect.parse("class c {@import \"decorator\" x}"), SyntaxError);
assertThrowsInstanceOf(() => Reflect.parse("class c {@dec`template` x}"), SyntaxError);
assertThrowsInstanceOf(() => Reflect.parse("class c {@import.meta.url x}"), SyntaxError);
assertThrowsInstanceOf(() => Reflect.parse("class c {@new.target x}"), SyntaxError);
assertThrowsInstanceOf(() => Reflect.parse("class c {@obj.first?.second x}"), SyntaxError);
assertThrowsInstanceOf(() => Reflect.parse("class c {@class x}"), SyntaxError);
assertThrowsInstanceOf(() => Reflect.parse("class c {@function() x}"), SyntaxError);
assertThrowsInstanceOf(() => Reflect.parse("class c {@{} x}"), SyntaxError);
assertThrowsInstanceOf(() => Reflect.parse("class c {@[] x}"), SyntaxError);
assertThrowsInstanceOf(() => Reflect.parse("class c {@\"string\" x}"), SyntaxError);
assertThrowsInstanceOf(() => Reflect.parse("class c {@`string` x}"), SyntaxError);
assertThrowsInstanceOf(() => Reflect.parse("class c {@/[a-z]+/ x}"), SyntaxError);
assertThrowsInstanceOf(() => Reflect.parse("class c {@true x}"), SyntaxError);
assertThrowsInstanceOf(() => Reflect.parse("class c {@1n x}"), SyntaxError);
assertThrowsInstanceOf(() => Reflect.parse("class c {@this x}"), SyntaxError);
assertThrowsInstanceOf(() => Reflect.parse("class c {@null x}"), SyntaxError);
assertThrowsInstanceOf(() => Reflect.parse("class c {@... x}"), SyntaxError);
assertThrowsInstanceOf(() => Reflect.parse("class c {@let x}"), SyntaxError);
assertThrowsInstanceOf(() => Reflect.parse("class c {@static x}"), SyntaxError);
assertThrowsInstanceOf(() => Reflect.parse("class c {@yield x}"), SyntaxError);
assertThrowsInstanceOf(() => Reflect.parse("class c {@if x}"), SyntaxError);
assertThrowsInstanceOf(() => Reflect.parse("class c {@enum x}"), SyntaxError);
assertThrowsInstanceOf(() => Reflect.parse("class c {@implements x}"), SyntaxError);
assertThrowsInstanceOf(() => Reflect.parse("class c {@foo().bar.prop x}"), SyntaxError);
assertThrowsInstanceOf(() => Reflect.parse("class c {@foo.bar().prop x}"), SyntaxError);
assertThrowsInstanceOf(() => Reflect.parse("class c {@foo.bar().prop() x}"), SyntaxError);
// assert we have the right syntax tree
const syntax = Reflect.parse("class c {}");
assertEq(syntax.type, "Program");
assertEq(syntax.body.length, 1);
assertEq(syntax.body[0].type, "ClassStatement");
assertEq(syntax.body[0].id.type, "Identifier");
assertEq(syntax.body[0].id.name, "c");
assertEq(syntax.body[0].decorators, null);
// assert decrorators on a class
const syntax2 = Reflect.parse("@dec @dec2 class c {}");
assertEq(syntax2.type, "Program");
assertEq(syntax2.body.length, 1);
assertEq(syntax2.body[0].decorators.type, "SequenceExpression");
assertEq(syntax2.body[0].decorators.expressions.length, 2);
assertEq(syntax2.body[0].decorators.expressions[0].type, "Identifier");
assertEq(syntax2.body[0].decorators.expressions[0].name, "dec");
assertEq(syntax2.body[0].decorators.expressions[1].type, "Identifier");
assertEq(syntax2.body[0].decorators.expressions[1].name, "dec2");
// assert decorators on class fields
const syntax3 = Reflect.parse("class c {@dec1 @dec2 field = false;}");
assertEq(syntax3.type, "Program");
assertEq(syntax3.body.length, 1);
assertEq(syntax3.body[0].decorators, null);
assertEq(syntax3.body[0].body[0].type, "ClassField");
assertEq(syntax3.body[0].body[0].decorators.type, "SequenceExpression");
assertEq(syntax3.body[0].body[0].decorators.expressions.length, 2);
assertEq(syntax3.body[0].body[0].decorators.expressions[0].type, "Identifier");
assertEq(syntax3.body[0].body[0].decorators.expressions[0].name, "dec1");
assertEq(syntax3.body[0].body[0].decorators.expressions[1].type, "Identifier");
assertEq(syntax3.body[0].body[0].decorators.expressions[1].name, "dec2");
// assert decorators on accessors
const syntax4 = Reflect.parse("class c {@dec1 @dec2 accessor field = false;}");
assertEq(syntax3.type, "Program");
assertEq(syntax3.body.length, 1);
assertEq(syntax3.body[0].decorators, null);
assertEq(syntax3.body[0].body[0].type, "ClassField");
assertEq(syntax3.body[0].body[0].decorators.type, "SequenceExpression");
assertEq(syntax3.body[0].body[0].decorators.expressions.length, 2);
assertEq(syntax3.body[0].body[0].decorators.expressions[0].type, "Identifier");
assertEq(syntax3.body[0].body[0].decorators.expressions[0].name, "dec1");
assertEq(syntax3.body[0].body[0].decorators.expressions[1].type, "Identifier");
assertEq(syntax3.body[0].body[0].decorators.expressions[1].name, "dec2");
// assert decorators on methods
const syntax5 = Reflect.parse("class c {@dec1 @dec2 method() {};}");
assertEq(syntax5.type, "Program");
assertEq(syntax5.body.length, 1);
assertEq(syntax5.body[0].decorators, null);
assertEq(syntax5.body[0].body[0].type, "ClassMethod");
assertEq(syntax5.body[0].body[0].decorators.type, "SequenceExpression");
assertEq(syntax5.body[0].body[0].decorators.expressions.length, 2);
assertEq(syntax5.body[0].body[0].decorators.expressions[0].type, "Identifier");
assertEq(syntax5.body[0].body[0].decorators.expressions[0].name, "dec1");
assertEq(syntax5.body[0].body[0].decorators.expressions[1].type, "Identifier");
assertEq(syntax5.body[0].body[0].decorators.expressions[1].name, "dec2");
|