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
|
// |jit-test| skip-if: !getBuildConfiguration("decorators")
load(libdir + "asserts.js");
let dec1Called = false;
// This explicitly tests the case where undefined is returned.
function dec1(value, context) {
dec1Called = true;
// returns undefined
}
function dec2(value, context) {
return class extends value {
constructor(...args) {
super(...args);
}
x2 = true;
}
}
function checkDecoratorContext(name) {
return function (value, context) {
assertEq(typeof value, "function");
assertEq(context.kind, "class");
assertEq(context.name, name);
assertEq(typeof context.addInitializer, "undefined");
// return undefined
}
}
@dec1 class C1 {};
assertEq(dec1Called, true);
@dec2 class C2 {
x1 = true;
}
let c2 = new C2();
assertEq(c2.x1, true);
assertEq(c2.x2, true);
let c3 = new @dec2 class {
x1 = true;
}
assertEq(c3.x1, true);
assertEq(c3.x2, true);
@checkDecoratorContext("D") class D {}
let d2 = new @checkDecoratorContext(undefined) class {};
class E {
static #dec1(value, context) {
return class extends value {
constructor(...args) {
super(...args);
}
x2 = true;
}
}
static {
this.F = @E.#dec1 class {
x1 = true;
}
}
}
let f = new E.F();
assertEq(f.x1, true);
assertEq(f.x2, true);
assertThrowsInstanceOf(() => {
@(() => { return "hello!"; }) class G {}
}, TypeError), "Returning a value other than undefined or a callable throws.";
assertThrowsInstanceOf(() => {
class G {
static #dec1() {}
static {
@G.#dec1 class G {}
}
}
}, ReferenceError), "can't access lexical declaration 'G' before initialization";
const decoratorOrder = [];
function makeOrderedDecorator(order) {
return function (value, context) {
decoratorOrder.push(order);
return value;
}
}
@makeOrderedDecorator(1) @makeOrderedDecorator(2) @makeOrderedDecorator(3)
class H {}
assertEq(decoratorOrder.length, 3);
assertEq(decoratorOrder[0], 3);
assertEq(decoratorOrder[1], 2);
assertEq(decoratorOrder[2], 1);
|