@prostojs/mate 0.3.3 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +32 -30
- package/dist/index.cjs +242 -0
- package/dist/index.d.ts +102 -0
- package/dist/index.mjs +236 -0
- package/package.json +68 -79
- package/dist/mate.cjs.prod.js +0 -263
- package/dist/mate.d.ts +0 -84
- package/dist/mate.esm-bundler.js +0 -257
- package/dist/src/index.d.ts +0 -3
- package/dist/src/index.d.ts.map +0 -1
- package/dist/src/mate.d.ts +0 -62
- package/dist/src/mate.d.ts.map +0 -1
- package/dist/src/reflect.d.ts +0 -8
- package/dist/src/reflect.d.ts.map +0 -1
- package/dist/src/tests/mate.artifacts.d.ts +0 -70
- package/dist/src/tests/mate.artifacts.d.ts.map +0 -1
- package/dist/src/tests/mate.spec.d.ts +0 -2
- package/dist/src/tests/mate.spec.d.ts.map +0 -1
- package/dist/src/tests/reflect.artifacts.d.ts +0 -5
- package/dist/src/tests/reflect.artifacts.d.ts.map +0 -1
- package/dist/src/tests/reflect.spec.d.ts +0 -2
- package/dist/src/tests/reflect.spec.d.ts.map +0 -1
- package/dist/src/types.d.ts +0 -5
- package/dist/src/types.d.ts.map +0 -1
- package/dist/src/utils/helpers.d.ts +0 -4
- package/dist/src/utils/helpers.d.ts.map +0 -1
- package/index.js +0 -2
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
//#region src/module-identity.ts
|
|
2
|
+
const MATE_MODULE_IDENTITY_KEY = Symbol.for("prostojs:mate:module-identity");
|
|
3
|
+
function getModulePath() {
|
|
4
|
+
try {
|
|
5
|
+
return import.meta.url;
|
|
6
|
+
} catch {
|
|
7
|
+
return;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
function formatIdentity(identity) {
|
|
11
|
+
const version = identity.version ? `v${identity.version}` : "unknown version";
|
|
12
|
+
return identity.path ? `${version} at ${identity.path}` : version;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Stamps `globalThis` with this copy's identity and warns if another
|
|
16
|
+
* copy of @prostojs/mate was already loaded.
|
|
17
|
+
*
|
|
18
|
+
* Invoked once at module scope from index.ts, so importing the package
|
|
19
|
+
* performs the check.
|
|
20
|
+
*/
|
|
21
|
+
function warnOnDuplicateMate() {
|
|
22
|
+
const g = globalThis;
|
|
23
|
+
const identity = {};
|
|
24
|
+
identity.version = "0.4.0";
|
|
25
|
+
const path = getModulePath();
|
|
26
|
+
if (path) identity.path = path;
|
|
27
|
+
const first = g[MATE_MODULE_IDENTITY_KEY];
|
|
28
|
+
if (first) console.warn(`[mate] A second copy of @prostojs/mate was loaded (${formatIdentity(first)}, now ${formatIdentity(identity)}). Decorator metadata is stored per-copy and will NOT be visible across copies. Check your bundler/dedupe settings.`);
|
|
29
|
+
else g[MATE_MODULE_IDENTITY_KEY] = identity;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
//#endregion
|
|
33
|
+
//#region src/utils/helpers.ts
|
|
34
|
+
function getConstructor(instance) {
|
|
35
|
+
return isConstructor(instance) ? instance : instance.constructor ? instance.constructor : Object.getPrototypeOf(instance).constructor;
|
|
36
|
+
}
|
|
37
|
+
function isConstructor(v) {
|
|
38
|
+
return typeof v === "function" && Object.getOwnPropertyDescriptor(v, "prototype") !== void 0 && Object.getOwnPropertyDescriptor(v, "caller") === void 0 && !!v.name;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
//#endregion
|
|
42
|
+
//#region src/reflect.ts
|
|
43
|
+
let classMetadata = /* @__PURE__ */ new WeakMap();
|
|
44
|
+
let paramMetadata = /* @__PURE__ */ new WeakMap();
|
|
45
|
+
const root = typeof global === "object" ? global : typeof self === "object" ? self : {};
|
|
46
|
+
function getMetaObject(target, prop) {
|
|
47
|
+
const isParam = typeof prop !== "undefined";
|
|
48
|
+
const metadata = isParam ? paramMetadata : classMetadata;
|
|
49
|
+
const targetKey = getConstructor(target);
|
|
50
|
+
let meta = metadata.get(targetKey);
|
|
51
|
+
if (!meta) {
|
|
52
|
+
meta = {};
|
|
53
|
+
metadata.set(targetKey, meta);
|
|
54
|
+
}
|
|
55
|
+
if (isParam) meta = meta[prop] = meta[prop] || {};
|
|
56
|
+
return meta;
|
|
57
|
+
}
|
|
58
|
+
const _reflect = {
|
|
59
|
+
getOwnMetadata(key, target, prop) {
|
|
60
|
+
return getMetaObject(target, prop)[key];
|
|
61
|
+
},
|
|
62
|
+
defineMetadata(key, data, target, prop) {
|
|
63
|
+
const meta = getMetaObject(target, prop);
|
|
64
|
+
meta[key] = data;
|
|
65
|
+
},
|
|
66
|
+
metadata(key, data) {
|
|
67
|
+
return ((target, propKey) => {
|
|
68
|
+
Reflect$1.defineMetadata(key, data, target, propKey);
|
|
69
|
+
});
|
|
70
|
+
},
|
|
71
|
+
_cleanup: (() => {
|
|
72
|
+
classMetadata = /* @__PURE__ */ new WeakMap();
|
|
73
|
+
paramMetadata = /* @__PURE__ */ new WeakMap();
|
|
74
|
+
})
|
|
75
|
+
};
|
|
76
|
+
if (!root.Reflect) root.Reflect = _reflect;
|
|
77
|
+
else {
|
|
78
|
+
const funcs = [
|
|
79
|
+
"getOwnMetadata",
|
|
80
|
+
"defineMetadata",
|
|
81
|
+
"metadata"
|
|
82
|
+
];
|
|
83
|
+
const target = root.Reflect;
|
|
84
|
+
for (const func of funcs) if (typeof target[func] !== "function") Object.defineProperty(target, func, {
|
|
85
|
+
configurable: true,
|
|
86
|
+
writable: true,
|
|
87
|
+
value: _reflect[func]
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
const Reflect$1 = _reflect;
|
|
91
|
+
|
|
92
|
+
//#endregion
|
|
93
|
+
//#region src/mate.ts
|
|
94
|
+
const Reflect = global?.Reflect || self?.Reflect || Reflect$1;
|
|
95
|
+
var Mate = class {
|
|
96
|
+
constructor(workspace, options = {}) {
|
|
97
|
+
this.workspace = workspace;
|
|
98
|
+
this.options = options;
|
|
99
|
+
this.logger = options.logger || console;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Cleanup function to reset metadata
|
|
103
|
+
*
|
|
104
|
+
* It is usefull in dev mode when server restarts
|
|
105
|
+
*/
|
|
106
|
+
_cleanup() {
|
|
107
|
+
Reflect._cleanup?.call(Reflect);
|
|
108
|
+
this._readCache = void 0;
|
|
109
|
+
}
|
|
110
|
+
set(args, key, value, isArray) {
|
|
111
|
+
this._readCache = void 0;
|
|
112
|
+
let level = "CLASS";
|
|
113
|
+
const newArgs = args.level === "CLASS" ? { target: getConstructor(args.target) } : args.level === "PROP" ? {
|
|
114
|
+
target: args.target,
|
|
115
|
+
propKey: args.propKey
|
|
116
|
+
} : args;
|
|
117
|
+
let meta = Reflect.getOwnMetadata(this.workspace, newArgs.target, newArgs.propKey) || {};
|
|
118
|
+
if (newArgs.propKey && this.options.readReturnType && !meta.returnType && args.descriptor) meta.returnType = Reflect.getOwnMetadata("design:returntype", newArgs.target, newArgs.propKey);
|
|
119
|
+
if (newArgs.propKey && this.options.readType && !meta.type) meta.type = Reflect.getOwnMetadata("design:type", newArgs.target, newArgs.propKey);
|
|
120
|
+
const { index } = newArgs;
|
|
121
|
+
const cb = typeof key === "function" ? key : void 0;
|
|
122
|
+
let data = meta;
|
|
123
|
+
if (!data.params) {
|
|
124
|
+
const paramTypes = Reflect.getOwnMetadata("design:paramtypes", newArgs.target, newArgs.propKey);
|
|
125
|
+
if (paramTypes) {
|
|
126
|
+
data.params = new Array(paramTypes.length);
|
|
127
|
+
for (let i = 0; i < paramTypes.length; i++) data.params[i] = { type: paramTypes[i] };
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
if (typeof index === "number") {
|
|
131
|
+
level = "PARAM";
|
|
132
|
+
data.params = data.params || [];
|
|
133
|
+
data.params[index] = data.params[index] || { type: void 0 };
|
|
134
|
+
if (cb) data.params[index] = cb(data.params[index], level, args.propKey, typeof args.index === "number" ? args.index : void 0);
|
|
135
|
+
else data = data.params[index];
|
|
136
|
+
} else if (!index && !args.descriptor && args.propKey && this.options.collectPropKeys && args.level !== "CLASS") this.set({
|
|
137
|
+
...args,
|
|
138
|
+
level: "CLASS"
|
|
139
|
+
}, (meta) => {
|
|
140
|
+
if (!meta.properties) meta.properties = [args.propKey];
|
|
141
|
+
else if (!meta.properties.includes(args.propKey)) meta.properties.push(args.propKey);
|
|
142
|
+
return meta;
|
|
143
|
+
});
|
|
144
|
+
level = typeof index === "number" ? "PARAM" : newArgs.propKey && newArgs.descriptor ? "METHOD" : newArgs.propKey ? "PROP" : "CLASS";
|
|
145
|
+
if (typeof key !== "function") if (isArray) {
|
|
146
|
+
const newArray = data[key] || [];
|
|
147
|
+
if (!Array.isArray(newArray))
|
|
148
|
+
/* istanbul ignore next line */
|
|
149
|
+
this.logger.error("Mate.add (isArray=true) called for non-array metadata");
|
|
150
|
+
newArray.unshift(value);
|
|
151
|
+
data[key] = newArray;
|
|
152
|
+
} else data[key] = value;
|
|
153
|
+
else if (cb && typeof index !== "number") meta = cb(data, level, args.propKey, typeof args.index === "number" ? args.index : void 0);
|
|
154
|
+
Reflect.defineMetadata(this.workspace, meta, newArgs.target, newArgs.propKey);
|
|
155
|
+
}
|
|
156
|
+
read(target, propKey) {
|
|
157
|
+
const constructor = isConstructor(target) ? target : getConstructor(target);
|
|
158
|
+
const proto = constructor.prototype;
|
|
159
|
+
const propKeyStr = propKey;
|
|
160
|
+
const ctorCache = this._readCache?.get(constructor);
|
|
161
|
+
if (ctorCache?.has(propKeyStr)) return ctorCache.get(propKeyStr);
|
|
162
|
+
let ownMeta = Reflect.getOwnMetadata(this.workspace, typeof propKey === "string" ? proto : constructor, propKey);
|
|
163
|
+
if (ownMeta && propKey === void 0 && ownMeta.params === void 0) {
|
|
164
|
+
const parent = Object.getPrototypeOf(constructor);
|
|
165
|
+
if (typeof parent === "function" && parent !== fnProto && parent !== constructor) ownMeta.params = this.read(parent)?.params;
|
|
166
|
+
}
|
|
167
|
+
if (this.options.inherit) {
|
|
168
|
+
const inheritFn = typeof this.options.inherit === "function" ? this.options.inherit : void 0;
|
|
169
|
+
let shouldInherit = this.options.inherit;
|
|
170
|
+
if (inheritFn) if (typeof propKey === "string") shouldInherit = inheritFn(Reflect.getOwnMetadata(this.workspace, constructor), ownMeta, "PROP", propKey);
|
|
171
|
+
else shouldInherit = inheritFn(ownMeta, ownMeta, "CLASS");
|
|
172
|
+
if (shouldInherit) {
|
|
173
|
+
const parent = Object.getPrototypeOf(constructor);
|
|
174
|
+
if (typeof parent === "function" && parent !== fnProto && parent !== constructor) {
|
|
175
|
+
const inheritedMeta = this.read(parent, propKey) || {};
|
|
176
|
+
const ownParams = ownMeta?.params;
|
|
177
|
+
ownMeta = {
|
|
178
|
+
...inheritedMeta,
|
|
179
|
+
...ownMeta
|
|
180
|
+
};
|
|
181
|
+
if (typeof propKey === "string" && ownParams && inheritedMeta?.params) {
|
|
182
|
+
for (let i = 0; i < ownParams.length; i++) if (typeof inheritedMeta?.params[i] !== "undefined") {
|
|
183
|
+
const ownParam = ownParams[i];
|
|
184
|
+
if (ownMeta.params && inheritFn && inheritFn(ownMeta, ownParam, "PARAM", typeof propKey === "string" ? propKey : void 0)) ownMeta.params[i] = {
|
|
185
|
+
...inheritedMeta?.params[i],
|
|
186
|
+
...ownParams[i]
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
const cache = this._readCache ??= /* @__PURE__ */ new WeakMap();
|
|
194
|
+
let tc = cache.get(constructor);
|
|
195
|
+
if (!tc) {
|
|
196
|
+
tc = /* @__PURE__ */ new Map();
|
|
197
|
+
cache.set(constructor, tc);
|
|
198
|
+
}
|
|
199
|
+
tc.set(propKeyStr, ownMeta);
|
|
200
|
+
return ownMeta;
|
|
201
|
+
}
|
|
202
|
+
apply(...decorators) {
|
|
203
|
+
return ((target, propKey, descriptor) => {
|
|
204
|
+
for (const d of decorators) d(target, propKey, descriptor);
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
decorate(key, value, isArray, level) {
|
|
208
|
+
return ((target, propKey, descriptor) => {
|
|
209
|
+
const args = {
|
|
210
|
+
target,
|
|
211
|
+
propKey,
|
|
212
|
+
descriptor: typeof descriptor === "number" ? void 0 : descriptor,
|
|
213
|
+
index: typeof descriptor === "number" ? descriptor : void 0,
|
|
214
|
+
level
|
|
215
|
+
};
|
|
216
|
+
this.set(args, key, value, isArray);
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
decorateConditional(ccb) {
|
|
220
|
+
return ((target, propKey, descriptor) => {
|
|
221
|
+
const d = ccb(typeof descriptor === "number" ? "PARAM" : propKey && descriptor ? "METHOD" : propKey ? "PROP" : "CLASS");
|
|
222
|
+
if (d) d(target, propKey, descriptor);
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
decorateClass(key, value, isArray) {
|
|
226
|
+
return this.decorate(key, value, isArray, "CLASS");
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
const fnProto = Object.getPrototypeOf(Function);
|
|
230
|
+
|
|
231
|
+
//#endregion
|
|
232
|
+
//#region src/index.ts
|
|
233
|
+
warnOnDuplicateMate();
|
|
234
|
+
|
|
235
|
+
//#endregion
|
|
236
|
+
export { MATE_MODULE_IDENTITY_KEY, Mate, getConstructor, isConstructor, warnOnDuplicateMate };
|
package/package.json
CHANGED
|
@@ -1,81 +1,70 @@
|
|
|
1
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
|
-
"rollup": "^2.58.0",
|
|
71
|
-
"rollup-plugin-terser": "^7.0.2",
|
|
72
|
-
"rollup-plugin-typescript2": "^0.30.0",
|
|
73
|
-
"semver": "^7.3.5",
|
|
74
|
-
"ts-jest": "^27.0.5",
|
|
75
|
-
"ts-node": "^10.9.1",
|
|
76
|
-
"tslib": "^2.3.1",
|
|
77
|
-
"typescript": "^4.8.2",
|
|
78
|
-
"yorkie": "^2.0.0",
|
|
79
|
-
"zlib": "^1.0.5"
|
|
80
|
-
}
|
|
2
|
+
"name": "@prostojs/mate",
|
|
3
|
+
"version": "0.5.0",
|
|
4
|
+
"description": "MATE is TS Metadata Organizer",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"decorators",
|
|
7
|
+
"mate",
|
|
8
|
+
"metadata",
|
|
9
|
+
"prostojs"
|
|
10
|
+
],
|
|
11
|
+
"homepage": "https://github.com/prostojs/mate#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/prostojs/mate/issues"
|
|
14
|
+
},
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"author": "Artem Maltsev",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/prostojs/mate.git"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
24
|
+
"main": "dist/index.cjs",
|
|
25
|
+
"module": "dist/index.mjs",
|
|
26
|
+
"types": "dist/index.d.ts",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
30
|
+
"import": "./dist/index.mjs",
|
|
31
|
+
"require": "./dist/index.cjs"
|
|
32
|
+
},
|
|
33
|
+
"./package.json": "./package.json"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "tsc -p tsconfig.build.json && rolldown -c rolldown.config.mjs && rm -rf dts-build",
|
|
37
|
+
"release": "node ./scripts/release",
|
|
38
|
+
"test": "vitest run",
|
|
39
|
+
"test:cov": "vitest run --coverage",
|
|
40
|
+
"lint": "oxlint",
|
|
41
|
+
"lint:fix": "oxlint --fix",
|
|
42
|
+
"format": "oxfmt",
|
|
43
|
+
"format:check": "oxfmt --check",
|
|
44
|
+
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s",
|
|
45
|
+
"version": "conventional-changelog -p angular -i CHANGELOG.md -s && git add CHANGELOG.md"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@prostojs/dye": "^0.4.9",
|
|
49
|
+
"@swc/core": "^1.15.13",
|
|
50
|
+
"@types/node": "^22.19.11",
|
|
51
|
+
"conventional-changelog": "^3.1.25",
|
|
52
|
+
"conventional-changelog-cli": "^2.2.2",
|
|
53
|
+
"enquirer": "^2.4.1",
|
|
54
|
+
"execa": "^5.1.1",
|
|
55
|
+
"minimist": "^1.2.8",
|
|
56
|
+
"oxfmt": "^0.35.0",
|
|
57
|
+
"oxlint": "^1.50.0",
|
|
58
|
+
"reflect-metadata": "^0.1.14",
|
|
59
|
+
"rolldown": "1.0.0-rc.5",
|
|
60
|
+
"rolldown-plugin-dts": "^0.22.1",
|
|
61
|
+
"semver": "^7.7.4",
|
|
62
|
+
"typescript": "^5.9.3",
|
|
63
|
+
"unplugin-swc": "^1.5.9",
|
|
64
|
+
"vitest": "^3.2.4",
|
|
65
|
+
"yorkie": "^2.0.0"
|
|
66
|
+
},
|
|
67
|
+
"gitHooks": {
|
|
68
|
+
"commit-msg": "node scripts/verifyCommit.js"
|
|
69
|
+
}
|
|
81
70
|
}
|
package/dist/mate.cjs.prod.js
DELETED
|
@@ -1,263 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
function getConstructor(instance) {
|
|
6
|
-
return isConstructor(instance) ?
|
|
7
|
-
instance : instance.constructor ?
|
|
8
|
-
instance.constructor : Object.getPrototypeOf(instance).constructor;
|
|
9
|
-
}
|
|
10
|
-
function isConstructor(v) {
|
|
11
|
-
return typeof v === 'function' && Object.getOwnPropertyNames(v).includes('prototype') && !Object.getOwnPropertyNames(v).includes('caller') && !!v.name;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
let classMetadata = new WeakMap();
|
|
15
|
-
let paramMetadata = new WeakMap();
|
|
16
|
-
const root = typeof global === 'object' ? global : typeof self === 'object' ? self : {};
|
|
17
|
-
function getMetaObject(target, prop) {
|
|
18
|
-
const isParam = typeof prop !== 'undefined';
|
|
19
|
-
const metadata = isParam ? paramMetadata : classMetadata;
|
|
20
|
-
const targetKey = getConstructor(target);
|
|
21
|
-
let meta = metadata.get(targetKey);
|
|
22
|
-
if (!meta) {
|
|
23
|
-
meta = {};
|
|
24
|
-
metadata.set(targetKey, meta);
|
|
25
|
-
}
|
|
26
|
-
if (isParam) {
|
|
27
|
-
meta = (meta[prop] =
|
|
28
|
-
meta[prop] || {});
|
|
29
|
-
}
|
|
30
|
-
return meta;
|
|
31
|
-
}
|
|
32
|
-
const _reflect = {
|
|
33
|
-
getOwnMetadata(key, target, prop) {
|
|
34
|
-
return getMetaObject(target, prop)[key];
|
|
35
|
-
},
|
|
36
|
-
defineMetadata(key, data, target, prop) {
|
|
37
|
-
const meta = getMetaObject(target, prop);
|
|
38
|
-
meta[key] = data;
|
|
39
|
-
},
|
|
40
|
-
metadata(key, data) {
|
|
41
|
-
return ((target, propKey) => {
|
|
42
|
-
Reflect$1.defineMetadata(key, data, target, propKey);
|
|
43
|
-
});
|
|
44
|
-
},
|
|
45
|
-
_cleanup: (() => {
|
|
46
|
-
classMetadata = new WeakMap();
|
|
47
|
-
paramMetadata = new WeakMap();
|
|
48
|
-
}),
|
|
49
|
-
};
|
|
50
|
-
if (!root.Reflect) {
|
|
51
|
-
root.Reflect = _reflect;
|
|
52
|
-
}
|
|
53
|
-
else {
|
|
54
|
-
const funcs = [
|
|
55
|
-
'getOwnMetadata',
|
|
56
|
-
'defineMetadata',
|
|
57
|
-
'metadata',
|
|
58
|
-
];
|
|
59
|
-
const target = root.Reflect;
|
|
60
|
-
for (const func of funcs) {
|
|
61
|
-
if (typeof target[func] !== 'function') {
|
|
62
|
-
Object.defineProperty(target, func, { configurable: true, writable: true, value: _reflect[func] });
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
const Reflect$1 = _reflect;
|
|
67
|
-
|
|
68
|
-
const Reflect = ((global === null || global === void 0 ? void 0 : global.Reflect) ||
|
|
69
|
-
(self === null || self === void 0 ? void 0 : self.Reflect) ||
|
|
70
|
-
Reflect$1);
|
|
71
|
-
class Mate {
|
|
72
|
-
constructor(workspace, options = {}) {
|
|
73
|
-
this.workspace = workspace;
|
|
74
|
-
this.options = options;
|
|
75
|
-
this.logger = options.logger || console;
|
|
76
|
-
}
|
|
77
|
-
_cleanup() {
|
|
78
|
-
var _a;
|
|
79
|
-
(_a = Reflect._cleanup) === null || _a === void 0 ? void 0 : _a.call(Reflect);
|
|
80
|
-
}
|
|
81
|
-
set(args, key, value, isArray) {
|
|
82
|
-
var _a;
|
|
83
|
-
let level = 'CLASS';
|
|
84
|
-
const newArgs = args.level === 'CLASS'
|
|
85
|
-
? { target: args.target }
|
|
86
|
-
: args.level === 'PROP'
|
|
87
|
-
? { target: args.target, propKey: args.propKey }
|
|
88
|
-
: args;
|
|
89
|
-
let meta = (Reflect.getOwnMetadata(this.workspace, newArgs.target, newArgs.propKey) || {});
|
|
90
|
-
if (newArgs.propKey &&
|
|
91
|
-
this.options.readReturnType &&
|
|
92
|
-
!meta.returnType &&
|
|
93
|
-
args.descriptor) {
|
|
94
|
-
meta.returnType = Reflect.getOwnMetadata('design:returntype', newArgs.target, newArgs.propKey);
|
|
95
|
-
}
|
|
96
|
-
if (newArgs.propKey && this.options.readType && !meta.type) {
|
|
97
|
-
meta.type = Reflect.getOwnMetadata('design:type', newArgs.target, newArgs.propKey);
|
|
98
|
-
}
|
|
99
|
-
const { index } = newArgs;
|
|
100
|
-
const cb = typeof key === 'function' ? key : undefined;
|
|
101
|
-
let data = meta;
|
|
102
|
-
if (!data.params) {
|
|
103
|
-
data.params = (_a = Reflect.getOwnMetadata('design:paramtypes', newArgs.target, newArgs.propKey)) === null || _a === void 0 ? void 0 : _a.map((f) => ({ type: f }));
|
|
104
|
-
}
|
|
105
|
-
if (typeof index === 'number') {
|
|
106
|
-
level = 'PARAM';
|
|
107
|
-
data.params = data.params || [];
|
|
108
|
-
data.params[index] = data.params[index] || {
|
|
109
|
-
type: undefined,
|
|
110
|
-
};
|
|
111
|
-
if (cb) {
|
|
112
|
-
data.params[index] = cb(data.params[index], level, args.propKey, typeof args.index === 'number' ? args.index : undefined);
|
|
113
|
-
}
|
|
114
|
-
else {
|
|
115
|
-
data = data.params[index];
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
else if (!index &&
|
|
119
|
-
!args.descriptor &&
|
|
120
|
-
args.propKey &&
|
|
121
|
-
this.options.collectPropKeys &&
|
|
122
|
-
args.level !== 'CLASS') {
|
|
123
|
-
this.set({ ...args, level: 'CLASS' }, (meta) => {
|
|
124
|
-
if (!meta.properties) {
|
|
125
|
-
meta.properties = [args.propKey];
|
|
126
|
-
}
|
|
127
|
-
else if (!meta.properties.includes(args.propKey)) {
|
|
128
|
-
meta.properties.push(args.propKey);
|
|
129
|
-
}
|
|
130
|
-
return meta;
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
level =
|
|
134
|
-
typeof index === 'number'
|
|
135
|
-
? 'PARAM'
|
|
136
|
-
: newArgs.propKey && newArgs.descriptor
|
|
137
|
-
? 'METHOD'
|
|
138
|
-
: newArgs.propKey
|
|
139
|
-
? 'PROP'
|
|
140
|
-
: 'CLASS';
|
|
141
|
-
if (typeof key !== 'function') {
|
|
142
|
-
if (isArray) {
|
|
143
|
-
const newArray = (data[key] || []);
|
|
144
|
-
if (!Array.isArray(newArray)) {
|
|
145
|
-
this.logger.error('Mate.add (isArray=true) called for non-array metadata');
|
|
146
|
-
}
|
|
147
|
-
newArray.unshift(value);
|
|
148
|
-
data[key] = newArray;
|
|
149
|
-
}
|
|
150
|
-
else {
|
|
151
|
-
data[key] = value;
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
else if (cb && typeof index !== 'number') {
|
|
155
|
-
meta = cb(data, level, args.propKey, typeof args.index === 'number' ? args.index : undefined);
|
|
156
|
-
}
|
|
157
|
-
Reflect.defineMetadata(this.workspace, meta, newArgs.target, newArgs.propKey);
|
|
158
|
-
}
|
|
159
|
-
read(target, propKey) {
|
|
160
|
-
var _a;
|
|
161
|
-
const isConstr = isConstructor(target);
|
|
162
|
-
const constructor = isConstr ? target : getConstructor(target);
|
|
163
|
-
const proto = constructor.prototype;
|
|
164
|
-
let ownMeta = Reflect.getOwnMetadata(this.workspace, typeof propKey === 'string' ? proto : constructor, propKey);
|
|
165
|
-
if (ownMeta && propKey === undefined && ownMeta.params === undefined) {
|
|
166
|
-
const parent = Object.getPrototypeOf(constructor);
|
|
167
|
-
if (typeof parent === 'function' &&
|
|
168
|
-
parent !== fnProto &&
|
|
169
|
-
parent !== constructor) {
|
|
170
|
-
ownMeta.params = (_a = this.read(parent)) === null || _a === void 0 ? void 0 : _a.params;
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
if (this.options.inherit) {
|
|
174
|
-
const inheritFn = typeof this.options.inherit === 'function'
|
|
175
|
-
? this.options.inherit
|
|
176
|
-
: undefined;
|
|
177
|
-
let shouldInherit = this.options.inherit;
|
|
178
|
-
if (inheritFn) {
|
|
179
|
-
if (typeof propKey === 'string') {
|
|
180
|
-
const classMeta = Reflect.getOwnMetadata(this.workspace, constructor);
|
|
181
|
-
shouldInherit = inheritFn(classMeta, ownMeta, 'PROP', propKey);
|
|
182
|
-
}
|
|
183
|
-
else {
|
|
184
|
-
shouldInherit = inheritFn(ownMeta, ownMeta, 'CLASS');
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
if (shouldInherit) {
|
|
188
|
-
const parent = Object.getPrototypeOf(constructor);
|
|
189
|
-
if (typeof parent === 'function' &&
|
|
190
|
-
parent !== fnProto &&
|
|
191
|
-
parent !== constructor) {
|
|
192
|
-
const inheritedMeta = (this.read(parent, propKey) ||
|
|
193
|
-
{});
|
|
194
|
-
const ownParams = ownMeta === null || ownMeta === void 0 ? void 0 : ownMeta.params;
|
|
195
|
-
ownMeta = { ...inheritedMeta, ...ownMeta };
|
|
196
|
-
if (typeof propKey === 'string' &&
|
|
197
|
-
ownParams &&
|
|
198
|
-
(inheritedMeta === null || inheritedMeta === void 0 ? void 0 : inheritedMeta.params)) {
|
|
199
|
-
for (let i = 0; i < ownParams.length; i++) {
|
|
200
|
-
if (typeof (inheritedMeta === null || inheritedMeta === void 0 ? void 0 : inheritedMeta.params[i]) !== 'undefined') {
|
|
201
|
-
const ownParam = ownParams[i];
|
|
202
|
-
if (ownMeta.params &&
|
|
203
|
-
inheritFn &&
|
|
204
|
-
inheritFn(ownMeta, ownParam, 'PARAM', typeof propKey === 'string'
|
|
205
|
-
? propKey
|
|
206
|
-
: undefined)) {
|
|
207
|
-
ownMeta.params[i] = {
|
|
208
|
-
...inheritedMeta === null || inheritedMeta === void 0 ? void 0 : inheritedMeta.params[i],
|
|
209
|
-
...ownParams[i],
|
|
210
|
-
};
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
return ownMeta;
|
|
219
|
-
}
|
|
220
|
-
apply(...decorators) {
|
|
221
|
-
return ((target, propKey, descriptor) => {
|
|
222
|
-
for (const d of decorators) {
|
|
223
|
-
d(target, propKey, descriptor);
|
|
224
|
-
}
|
|
225
|
-
});
|
|
226
|
-
}
|
|
227
|
-
decorate(key, value, isArray, level) {
|
|
228
|
-
return ((target, propKey, descriptor) => {
|
|
229
|
-
const args = {
|
|
230
|
-
target,
|
|
231
|
-
propKey,
|
|
232
|
-
descriptor: typeof descriptor === 'number' ? undefined : descriptor,
|
|
233
|
-
index: typeof descriptor === 'number' ? descriptor : undefined,
|
|
234
|
-
level,
|
|
235
|
-
};
|
|
236
|
-
this.set(args, key, value, isArray);
|
|
237
|
-
});
|
|
238
|
-
}
|
|
239
|
-
decorateConditional(ccb) {
|
|
240
|
-
return ((target, propKey, descriptor) => {
|
|
241
|
-
const hasIndex = typeof descriptor === 'number';
|
|
242
|
-
const decoratorLevel = hasIndex
|
|
243
|
-
? 'PARAM'
|
|
244
|
-
: propKey && descriptor
|
|
245
|
-
? 'METHOD'
|
|
246
|
-
: propKey
|
|
247
|
-
? 'PROP'
|
|
248
|
-
: 'CLASS';
|
|
249
|
-
const d = ccb(decoratorLevel);
|
|
250
|
-
if (d) {
|
|
251
|
-
d(target, propKey, descriptor);
|
|
252
|
-
}
|
|
253
|
-
});
|
|
254
|
-
}
|
|
255
|
-
decorateClass(key, value, isArray) {
|
|
256
|
-
return this.decorate(key, value, isArray, 'CLASS');
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
const fnProto = Object.getPrototypeOf(Function);
|
|
260
|
-
|
|
261
|
-
exports.Mate = Mate;
|
|
262
|
-
exports.getConstructor = getConstructor;
|
|
263
|
-
exports.isConstructor = isConstructor;
|