-
-
Notifications
You must be signed in to change notification settings - Fork 185
Expand file tree
/
Copy pathDecorateLegacy.ts
More file actions
54 lines (48 loc) · 1.84 KB
/
DecorateLegacy.ts
File metadata and controls
54 lines (48 loc) · 1.84 KB
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
/**
* Old-style decorators, activated by enabling the experimentalDecorators flag
*/
import { __TS__ObjectGetOwnPropertyDescriptor } from "./ObjectGetOwnPropertyDescriptor";
import { __TS__SetDescriptor } from "./SetDescriptor";
export type LegacyDecorator<TTarget extends AnyTable, TKey extends keyof TTarget> = (
target: TTarget,
key?: TKey,
descriptor?: PropertyDescriptor
) => TTarget;
export function __TS__DecorateLegacy<TTarget extends AnyTable, TKey extends keyof TTarget>(
this: void,
decorators: Array<LegacyDecorator<TTarget, TKey>>,
target: TTarget,
key?: TKey,
desc?: any
): TTarget {
let result = target;
for (let i = decorators.length; i >= 0; i--) {
const decorator = decorators[i];
if (decorator !== undefined) {
const oldResult = result;
if (key === undefined) {
result = decorator(result);
} else if (desc === true) {
const value = rawget(target, key);
const descriptor = __TS__ObjectGetOwnPropertyDescriptor(target, key) ?? {
configurable: true,
writable: true,
value,
};
const desc = decorator(target, key, descriptor) || descriptor;
const isSimpleValue = desc.configurable === true && desc.writable === true && !desc.get && !desc.set;
if (isSimpleValue) {
rawset(target, key, desc.value);
} else {
__TS__SetDescriptor(target, key, { ...descriptor, ...desc });
}
} else if (desc === false) {
result = decorator(target, key, desc);
} else {
result = decorator(target, key);
}
result = result || oldResult;
}
}
return result;
}