Skip to content

Commit 62c5322

Browse files
authored
Add support for __esDecorate and related helpers (#193)
1 parent 3bdc002 commit 62c5322

File tree

4 files changed

+138
-0
lines changed

4 files changed

+138
-0
lines changed

modules/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ const {
55
__rest,
66
__decorate,
77
__param,
8+
__esDecorate,
9+
__runInitializers,
10+
__propKey,
11+
__setFunctionName,
812
__metadata,
913
__awaiter,
1014
__generator,
@@ -32,6 +36,10 @@ export {
3236
__rest,
3337
__decorate,
3438
__param,
39+
__esDecorate,
40+
__runInitializers,
41+
__propKey,
42+
__setFunctionName,
3543
__metadata,
3644
__awaiter,
3745
__generator,

tslib.d.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,38 @@ export declare function __decorate(decorators: Function[], target: any, key?: st
5858
*/
5959
export declare function __param(paramIndex: number, decorator: Function): Function;
6060

61+
/**
62+
* Applies decorators to a class or class member, following the native ECMAScript decorator specification.
63+
* @param ctor For non-field class members, the class constructor. Otherwise, `null`.
64+
* @param descriptorIn The `PropertyDescriptor` to use when unable to look up the property from `ctor`.
65+
* @param decorators The decorators to apply
66+
* @param contextIn The `DecoratorContext` to clone for each decorator application.
67+
* @param initializers An array of field initializer mutation functions into which new initializers are written.
68+
* @param extraInitializers An array of extra initializer functions into which new initializers are written.
69+
*/
70+
export declare function __esDecorate(ctor: Function | null, descriptorIn: object | null, decorators: Function[], contextIn: object, initializers: Function[] | null, extraInitializers: Function[]): void;
71+
72+
/**
73+
* Runs field initializers or extra initializers generated by `__esDecorate`.
74+
* @param thisArg The `this` argument to use.
75+
* @param initializers The array of initializers to evaluate.
76+
* @param value The initial value to pass to the initializers.
77+
*/
78+
export declare function __runInitializers(thisArg: unknown, initializers: Function[], value?: any): any;
79+
80+
/**
81+
* Converts a computed property name into a `string` or `symbol` value.
82+
*/
83+
export declare function __propKey(x: any): string | symbol;
84+
85+
/**
86+
* Assigns the name of a function derived from the left-hand side of an assignment.
87+
* @param f The function to rename.
88+
* @param name The new name for the function.
89+
* @param prefix A prefix (such as `"get"` or `"set"`) to insert before the name.
90+
*/
91+
export declare function __setFunctionName(f: Function, name: string | symbol, prefix?: string): Function;
92+
6193
/**
6294
* Creates a decorator that sets metadata.
6395
*

tslib.es6.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,51 @@ export function __param(paramIndex, decorator) {
6363
return function (target, key) { decorator(target, key, paramIndex); }
6464
}
6565

66+
export function __esDecorate(ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
67+
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
68+
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
69+
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
70+
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
71+
var _, done = false;
72+
for (var i = decorators.length - 1; i >= 0; i--) {
73+
var context = {};
74+
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
75+
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
76+
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
77+
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
78+
if (kind === "accessor") {
79+
if (result === void 0) continue;
80+
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
81+
if (_ = accept(result.get)) descriptor.get = _;
82+
if (_ = accept(result.set)) descriptor.set = _;
83+
if (_ = accept(result.init)) initializers.push(_);
84+
}
85+
else if (_ = accept(result)) {
86+
if (kind === "field") initializers.push(_);
87+
else descriptor[key] = _;
88+
}
89+
}
90+
if (target) Object.defineProperty(target, contextIn.name, descriptor);
91+
done = true;
92+
};
93+
94+
export function __runInitializers(thisArg, initializers, value) {
95+
var useValue = arguments.length > 2;
96+
for (var i = 0; i < initializers.length; i++) {
97+
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
98+
}
99+
return useValue ? value : void 0;
100+
};
101+
102+
export function __propKey(x) {
103+
return typeof x === "symbol" ? x : "".concat(x);
104+
};
105+
106+
export function __setFunctionName(f, name, prefix) {
107+
if (typeof name === "symbol") name = name.description ? "[".concat(name.description, "]") : "";
108+
return Object.defineProperty(f, "name", { configurable: true, value: prefix ? "".concat(prefix, " ", name) : name });
109+
};
110+
66111
export function __metadata(metadataKey, metadataValue) {
67112
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(metadataKey, metadataValue);
68113
}

tslib.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ var __assign;
1818
var __rest;
1919
var __decorate;
2020
var __param;
21+
var __esDecorate;
22+
var __runInitializers;
23+
var __propKey;
24+
var __setFunctionName;
2125
var __metadata;
2226
var __awaiter;
2327
var __generator;
@@ -105,6 +109,51 @@ var __createBinding;
105109
return function (target, key) { decorator(target, key, paramIndex); }
106110
};
107111

112+
__esDecorate = function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
113+
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
114+
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
115+
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
116+
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
117+
var _, done = false;
118+
for (var i = decorators.length - 1; i >= 0; i--) {
119+
var context = {};
120+
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
121+
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
122+
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
123+
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
124+
if (kind === "accessor") {
125+
if (result === void 0) continue;
126+
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
127+
if (_ = accept(result.get)) descriptor.get = _;
128+
if (_ = accept(result.set)) descriptor.set = _;
129+
if (_ = accept(result.init)) initializers.push(_);
130+
}
131+
else if (_ = accept(result)) {
132+
if (kind === "field") initializers.push(_);
133+
else descriptor[key] = _;
134+
}
135+
}
136+
if (target) Object.defineProperty(target, contextIn.name, descriptor);
137+
done = true;
138+
};
139+
140+
__runInitializers = function (thisArg, initializers, value) {
141+
var useValue = arguments.length > 2;
142+
for (var i = 0; i < initializers.length; i++) {
143+
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
144+
}
145+
return useValue ? value : void 0;
146+
};
147+
148+
__propKey = function (x) {
149+
return typeof x === "symbol" ? x : "".concat(x);
150+
};
151+
152+
__setFunctionName = function (f, name, prefix) {
153+
if (typeof name === "symbol") name = name.description ? "[".concat(name.description, "]") : "";
154+
return Object.defineProperty(f, "name", { configurable: true, value: prefix ? "".concat(prefix, " ", name) : name });
155+
};
156+
108157
__metadata = function (metadataKey, metadataValue) {
109158
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(metadataKey, metadataValue);
110159
};
@@ -294,6 +343,10 @@ var __createBinding;
294343
exporter("__rest", __rest);
295344
exporter("__decorate", __decorate);
296345
exporter("__param", __param);
346+
exporter("__esDecorate", __esDecorate);
347+
exporter("__runInitializers", __runInitializers);
348+
exporter("__propKey", __propKey);
349+
exporter("__setFunctionName", __setFunctionName);
297350
exporter("__metadata", __metadata);
298351
exporter("__awaiter", __awaiter);
299352
exporter("__generator", __generator);

0 commit comments

Comments
 (0)