[d565449] | 1 | "use strict";
|
---|
| 2 |
|
---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
---|
| 4 | value: true
|
---|
| 5 | });
|
---|
| 6 | exports.default = applyDecs;
|
---|
| 7 | var _setFunctionName = require("setFunctionName");
|
---|
| 8 | var _toPropertyKey = require("toPropertyKey");
|
---|
| 9 | function old_createMetadataMethodsForProperty(metadataMap, kind, property, decoratorFinishedRef) {
|
---|
| 10 | return {
|
---|
| 11 | getMetadata: function (key) {
|
---|
| 12 | old_assertNotFinished(decoratorFinishedRef, "getMetadata");
|
---|
| 13 | old_assertMetadataKey(key);
|
---|
| 14 | var metadataForKey = metadataMap[key];
|
---|
| 15 | if (metadataForKey === void 0) return void 0;
|
---|
| 16 | if (kind === 1) {
|
---|
| 17 | var pub = metadataForKey.public;
|
---|
| 18 | if (pub !== void 0) {
|
---|
| 19 | return pub[property];
|
---|
| 20 | }
|
---|
| 21 | } else if (kind === 2) {
|
---|
| 22 | var priv = metadataForKey.private;
|
---|
| 23 | if (priv !== void 0) {
|
---|
| 24 | return priv.get(property);
|
---|
| 25 | }
|
---|
| 26 | } else if (Object.hasOwnProperty.call(metadataForKey, "constructor")) {
|
---|
| 27 | return metadataForKey.constructor;
|
---|
| 28 | }
|
---|
| 29 | },
|
---|
| 30 | setMetadata: function (key, value) {
|
---|
| 31 | old_assertNotFinished(decoratorFinishedRef, "setMetadata");
|
---|
| 32 | old_assertMetadataKey(key);
|
---|
| 33 | var metadataForKey = metadataMap[key];
|
---|
| 34 | if (metadataForKey === void 0) {
|
---|
| 35 | metadataForKey = metadataMap[key] = {};
|
---|
| 36 | }
|
---|
| 37 | if (kind === 1) {
|
---|
| 38 | var pub = metadataForKey.public;
|
---|
| 39 | if (pub === void 0) {
|
---|
| 40 | pub = metadataForKey.public = {};
|
---|
| 41 | }
|
---|
| 42 | pub[property] = value;
|
---|
| 43 | } else if (kind === 2) {
|
---|
| 44 | var priv = metadataForKey.priv;
|
---|
| 45 | if (priv === void 0) {
|
---|
| 46 | priv = metadataForKey.private = new Map();
|
---|
| 47 | }
|
---|
| 48 | priv.set(property, value);
|
---|
| 49 | } else {
|
---|
| 50 | metadataForKey.constructor = value;
|
---|
| 51 | }
|
---|
| 52 | }
|
---|
| 53 | };
|
---|
| 54 | }
|
---|
| 55 | function old_convertMetadataMapToFinal(obj, metadataMap) {
|
---|
| 56 | var parentMetadataMap = obj[Symbol.metadata || Symbol.for("Symbol.metadata")];
|
---|
| 57 | var metadataKeys = Object.getOwnPropertySymbols(metadataMap);
|
---|
| 58 | if (metadataKeys.length === 0) return;
|
---|
| 59 | for (var i = 0; i < metadataKeys.length; i++) {
|
---|
| 60 | var key = metadataKeys[i];
|
---|
| 61 | var metaForKey = metadataMap[key];
|
---|
| 62 | var parentMetaForKey = parentMetadataMap ? parentMetadataMap[key] : null;
|
---|
| 63 | var pub = metaForKey.public;
|
---|
| 64 | var parentPub = parentMetaForKey ? parentMetaForKey.public : null;
|
---|
| 65 | if (pub && parentPub) {
|
---|
| 66 | Object.setPrototypeOf(pub, parentPub);
|
---|
| 67 | }
|
---|
| 68 | var priv = metaForKey.private;
|
---|
| 69 | if (priv) {
|
---|
| 70 | var privArr = Array.from(priv.values());
|
---|
| 71 | var parentPriv = parentMetaForKey ? parentMetaForKey.private : null;
|
---|
| 72 | if (parentPriv) {
|
---|
| 73 | privArr = privArr.concat(parentPriv);
|
---|
| 74 | }
|
---|
| 75 | metaForKey.private = privArr;
|
---|
| 76 | }
|
---|
| 77 | if (parentMetaForKey) {
|
---|
| 78 | Object.setPrototypeOf(metaForKey, parentMetaForKey);
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 | if (parentMetadataMap) {
|
---|
| 82 | Object.setPrototypeOf(metadataMap, parentMetadataMap);
|
---|
| 83 | }
|
---|
| 84 | obj[Symbol.metadata || Symbol.for("Symbol.metadata")] = metadataMap;
|
---|
| 85 | }
|
---|
| 86 | function old_createAddInitializerMethod(initializers, decoratorFinishedRef) {
|
---|
| 87 | return function addInitializer(initializer) {
|
---|
| 88 | old_assertNotFinished(decoratorFinishedRef, "addInitializer");
|
---|
| 89 | old_assertCallable(initializer, "An initializer");
|
---|
| 90 | initializers.push(initializer);
|
---|
| 91 | };
|
---|
| 92 | }
|
---|
| 93 | function old_memberDec(dec, name, desc, metadataMap, initializers, kind, isStatic, isPrivate, value) {
|
---|
| 94 | var kindStr;
|
---|
| 95 | switch (kind) {
|
---|
| 96 | case 1:
|
---|
| 97 | kindStr = "accessor";
|
---|
| 98 | break;
|
---|
| 99 | case 2:
|
---|
| 100 | kindStr = "method";
|
---|
| 101 | break;
|
---|
| 102 | case 3:
|
---|
| 103 | kindStr = "getter";
|
---|
| 104 | break;
|
---|
| 105 | case 4:
|
---|
| 106 | kindStr = "setter";
|
---|
| 107 | break;
|
---|
| 108 | default:
|
---|
| 109 | kindStr = "field";
|
---|
| 110 | }
|
---|
| 111 | var ctx = {
|
---|
| 112 | kind: kindStr,
|
---|
| 113 | name: isPrivate ? "#" + name : _toPropertyKey(name),
|
---|
| 114 | isStatic: isStatic,
|
---|
| 115 | isPrivate: isPrivate
|
---|
| 116 | };
|
---|
| 117 | var decoratorFinishedRef = {
|
---|
| 118 | v: false
|
---|
| 119 | };
|
---|
| 120 | if (kind !== 0) {
|
---|
| 121 | ctx.addInitializer = old_createAddInitializerMethod(initializers, decoratorFinishedRef);
|
---|
| 122 | }
|
---|
| 123 | var metadataKind, metadataName;
|
---|
| 124 | if (isPrivate) {
|
---|
| 125 | metadataKind = 2;
|
---|
| 126 | metadataName = Symbol(name);
|
---|
| 127 | var access = {};
|
---|
| 128 | if (kind === 0) {
|
---|
| 129 | access.get = desc.get;
|
---|
| 130 | access.set = desc.set;
|
---|
| 131 | } else if (kind === 2) {
|
---|
| 132 | access.get = function () {
|
---|
| 133 | return desc.value;
|
---|
| 134 | };
|
---|
| 135 | } else {
|
---|
| 136 | if (kind === 1 || kind === 3) {
|
---|
| 137 | access.get = function () {
|
---|
| 138 | return desc.get.call(this);
|
---|
| 139 | };
|
---|
| 140 | }
|
---|
| 141 | if (kind === 1 || kind === 4) {
|
---|
| 142 | access.set = function (v) {
|
---|
| 143 | desc.set.call(this, v);
|
---|
| 144 | };
|
---|
| 145 | }
|
---|
| 146 | }
|
---|
| 147 | ctx.access = access;
|
---|
| 148 | } else {
|
---|
| 149 | metadataKind = 1;
|
---|
| 150 | metadataName = name;
|
---|
| 151 | }
|
---|
| 152 | try {
|
---|
| 153 | return dec(value, Object.assign(ctx, old_createMetadataMethodsForProperty(metadataMap, metadataKind, metadataName, decoratorFinishedRef)));
|
---|
| 154 | } finally {
|
---|
| 155 | decoratorFinishedRef.v = true;
|
---|
| 156 | }
|
---|
| 157 | }
|
---|
| 158 | function old_assertNotFinished(decoratorFinishedRef, fnName) {
|
---|
| 159 | if (decoratorFinishedRef.v) {
|
---|
| 160 | throw new Error("attempted to call " + fnName + " after decoration was finished");
|
---|
| 161 | }
|
---|
| 162 | }
|
---|
| 163 | function old_assertMetadataKey(key) {
|
---|
| 164 | if (typeof key !== "symbol") {
|
---|
| 165 | throw new TypeError("Metadata keys must be symbols, received: " + key);
|
---|
| 166 | }
|
---|
| 167 | }
|
---|
| 168 | function old_assertCallable(fn, hint) {
|
---|
| 169 | if (typeof fn !== "function") {
|
---|
| 170 | throw new TypeError(hint + " must be a function");
|
---|
| 171 | }
|
---|
| 172 | }
|
---|
| 173 | function old_assertValidReturnValue(kind, value) {
|
---|
| 174 | var type = typeof value;
|
---|
| 175 | if (kind === 1) {
|
---|
| 176 | if (type !== "object" || value === null) {
|
---|
| 177 | throw new TypeError("accessor decorators must return an object with get, set, or init properties or void 0");
|
---|
| 178 | }
|
---|
| 179 | if (value.get !== undefined) {
|
---|
| 180 | old_assertCallable(value.get, "accessor.get");
|
---|
| 181 | }
|
---|
| 182 | if (value.set !== undefined) {
|
---|
| 183 | old_assertCallable(value.set, "accessor.set");
|
---|
| 184 | }
|
---|
| 185 | if (value.init !== undefined) {
|
---|
| 186 | old_assertCallable(value.init, "accessor.init");
|
---|
| 187 | }
|
---|
| 188 | if (value.initializer !== undefined) {
|
---|
| 189 | old_assertCallable(value.initializer, "accessor.initializer");
|
---|
| 190 | }
|
---|
| 191 | } else if (type !== "function") {
|
---|
| 192 | var hint;
|
---|
| 193 | if (kind === 0) {
|
---|
| 194 | hint = "field";
|
---|
| 195 | } else if (kind === 10) {
|
---|
| 196 | hint = "class";
|
---|
| 197 | } else {
|
---|
| 198 | hint = "method";
|
---|
| 199 | }
|
---|
| 200 | throw new TypeError(hint + " decorators must return a function or void 0");
|
---|
| 201 | }
|
---|
| 202 | }
|
---|
| 203 | function old_getInit(desc) {
|
---|
| 204 | var initializer;
|
---|
| 205 | if ((initializer = desc.init) == null && (initializer = desc.initializer) && typeof console !== "undefined") {
|
---|
| 206 | console.warn(".initializer has been renamed to .init as of March 2022");
|
---|
| 207 | }
|
---|
| 208 | return initializer;
|
---|
| 209 | }
|
---|
| 210 | function old_applyMemberDec(ret, base, decInfo, name, kind, isStatic, isPrivate, metadataMap, initializers) {
|
---|
| 211 | var decs = decInfo[0];
|
---|
| 212 | var desc, initializer, prefix, value;
|
---|
| 213 | if (isPrivate) {
|
---|
| 214 | if (kind === 0 || kind === 1) {
|
---|
| 215 | desc = {
|
---|
| 216 | get: decInfo[3],
|
---|
| 217 | set: decInfo[4]
|
---|
| 218 | };
|
---|
| 219 | prefix = "get";
|
---|
| 220 | } else if (kind === 3) {
|
---|
| 221 | desc = {
|
---|
| 222 | get: decInfo[3]
|
---|
| 223 | };
|
---|
| 224 | prefix = "get";
|
---|
| 225 | } else if (kind === 4) {
|
---|
| 226 | desc = {
|
---|
| 227 | set: decInfo[3]
|
---|
| 228 | };
|
---|
| 229 | prefix = "set";
|
---|
| 230 | } else {
|
---|
| 231 | desc = {
|
---|
| 232 | value: decInfo[3]
|
---|
| 233 | };
|
---|
| 234 | }
|
---|
| 235 | if (kind !== 0) {
|
---|
| 236 | if (kind === 1) {
|
---|
| 237 | _setFunctionName(decInfo[4], "#" + name, "set");
|
---|
| 238 | }
|
---|
| 239 | _setFunctionName(decInfo[3], "#" + name, prefix);
|
---|
| 240 | }
|
---|
| 241 | } else if (kind !== 0) {
|
---|
| 242 | desc = Object.getOwnPropertyDescriptor(base, name);
|
---|
| 243 | }
|
---|
| 244 | if (kind === 1) {
|
---|
| 245 | value = {
|
---|
| 246 | get: desc.get,
|
---|
| 247 | set: desc.set
|
---|
| 248 | };
|
---|
| 249 | } else if (kind === 2) {
|
---|
| 250 | value = desc.value;
|
---|
| 251 | } else if (kind === 3) {
|
---|
| 252 | value = desc.get;
|
---|
| 253 | } else if (kind === 4) {
|
---|
| 254 | value = desc.set;
|
---|
| 255 | }
|
---|
| 256 | var newValue, get, set;
|
---|
| 257 | if (typeof decs === "function") {
|
---|
| 258 | newValue = old_memberDec(decs, name, desc, metadataMap, initializers, kind, isStatic, isPrivate, value);
|
---|
| 259 | if (newValue !== void 0) {
|
---|
| 260 | old_assertValidReturnValue(kind, newValue);
|
---|
| 261 | if (kind === 0) {
|
---|
| 262 | initializer = newValue;
|
---|
| 263 | } else if (kind === 1) {
|
---|
| 264 | initializer = old_getInit(newValue);
|
---|
| 265 | get = newValue.get || value.get;
|
---|
| 266 | set = newValue.set || value.set;
|
---|
| 267 | value = {
|
---|
| 268 | get: get,
|
---|
| 269 | set: set
|
---|
| 270 | };
|
---|
| 271 | } else {
|
---|
| 272 | value = newValue;
|
---|
| 273 | }
|
---|
| 274 | }
|
---|
| 275 | } else {
|
---|
| 276 | for (var i = decs.length - 1; i >= 0; i--) {
|
---|
| 277 | var dec = decs[i];
|
---|
| 278 | newValue = old_memberDec(dec, name, desc, metadataMap, initializers, kind, isStatic, isPrivate, value);
|
---|
| 279 | if (newValue !== void 0) {
|
---|
| 280 | old_assertValidReturnValue(kind, newValue);
|
---|
| 281 | var newInit;
|
---|
| 282 | if (kind === 0) {
|
---|
| 283 | newInit = newValue;
|
---|
| 284 | } else if (kind === 1) {
|
---|
| 285 | newInit = old_getInit(newValue);
|
---|
| 286 | get = newValue.get || value.get;
|
---|
| 287 | set = newValue.set || value.set;
|
---|
| 288 | value = {
|
---|
| 289 | get: get,
|
---|
| 290 | set: set
|
---|
| 291 | };
|
---|
| 292 | } else {
|
---|
| 293 | value = newValue;
|
---|
| 294 | }
|
---|
| 295 | if (newInit !== void 0) {
|
---|
| 296 | if (initializer === void 0) {
|
---|
| 297 | initializer = newInit;
|
---|
| 298 | } else if (typeof initializer === "function") {
|
---|
| 299 | initializer = [initializer, newInit];
|
---|
| 300 | } else {
|
---|
| 301 | initializer.push(newInit);
|
---|
| 302 | }
|
---|
| 303 | }
|
---|
| 304 | }
|
---|
| 305 | }
|
---|
| 306 | }
|
---|
| 307 | if (kind === 0 || kind === 1) {
|
---|
| 308 | if (initializer === void 0) {
|
---|
| 309 | initializer = function (instance, init) {
|
---|
| 310 | return init;
|
---|
| 311 | };
|
---|
| 312 | } else if (typeof initializer !== "function") {
|
---|
| 313 | var ownInitializers = initializer;
|
---|
| 314 | initializer = function (instance, init) {
|
---|
| 315 | var value = init;
|
---|
| 316 | for (var i = 0; i < ownInitializers.length; i++) {
|
---|
| 317 | value = ownInitializers[i].call(instance, value);
|
---|
| 318 | }
|
---|
| 319 | return value;
|
---|
| 320 | };
|
---|
| 321 | } else {
|
---|
| 322 | var originalInitializer = initializer;
|
---|
| 323 | initializer = function (instance, init) {
|
---|
| 324 | return originalInitializer.call(instance, init);
|
---|
| 325 | };
|
---|
| 326 | }
|
---|
| 327 | ret.push(initializer);
|
---|
| 328 | }
|
---|
| 329 | if (kind !== 0) {
|
---|
| 330 | if (kind === 1) {
|
---|
| 331 | desc.get = value.get;
|
---|
| 332 | desc.set = value.set;
|
---|
| 333 | } else if (kind === 2) {
|
---|
| 334 | desc.value = value;
|
---|
| 335 | } else if (kind === 3) {
|
---|
| 336 | desc.get = value;
|
---|
| 337 | } else if (kind === 4) {
|
---|
| 338 | desc.set = value;
|
---|
| 339 | }
|
---|
| 340 | if (isPrivate) {
|
---|
| 341 | if (kind === 1) {
|
---|
| 342 | ret.push(function (instance, args) {
|
---|
| 343 | return value.get.call(instance, args);
|
---|
| 344 | });
|
---|
| 345 | ret.push(function (instance, args) {
|
---|
| 346 | return value.set.call(instance, args);
|
---|
| 347 | });
|
---|
| 348 | } else if (kind === 2) {
|
---|
| 349 | ret.push(value);
|
---|
| 350 | } else {
|
---|
| 351 | ret.push(function (instance, args) {
|
---|
| 352 | return value.call(instance, args);
|
---|
| 353 | });
|
---|
| 354 | }
|
---|
| 355 | } else {
|
---|
| 356 | Object.defineProperty(base, name, desc);
|
---|
| 357 | }
|
---|
| 358 | }
|
---|
| 359 | }
|
---|
| 360 | function old_applyMemberDecs(ret, Class, protoMetadataMap, staticMetadataMap, decInfos) {
|
---|
| 361 | var protoInitializers;
|
---|
| 362 | var staticInitializers;
|
---|
| 363 | var existingProtoNonFields = new Map();
|
---|
| 364 | var existingStaticNonFields = new Map();
|
---|
| 365 | for (var i = 0; i < decInfos.length; i++) {
|
---|
| 366 | var decInfo = decInfos[i];
|
---|
| 367 | if (!Array.isArray(decInfo)) continue;
|
---|
| 368 | var kind = decInfo[1];
|
---|
| 369 | var name = decInfo[2];
|
---|
| 370 | var isPrivate = decInfo.length > 3;
|
---|
| 371 | var isStatic = kind >= 5;
|
---|
| 372 | var base;
|
---|
| 373 | var metadataMap;
|
---|
| 374 | var initializers;
|
---|
| 375 | if (isStatic) {
|
---|
| 376 | base = Class;
|
---|
| 377 | metadataMap = staticMetadataMap;
|
---|
| 378 | kind = kind - 5;
|
---|
| 379 | if (kind !== 0) {
|
---|
| 380 | staticInitializers = staticInitializers || [];
|
---|
| 381 | initializers = staticInitializers;
|
---|
| 382 | }
|
---|
| 383 | } else {
|
---|
| 384 | base = Class.prototype;
|
---|
| 385 | metadataMap = protoMetadataMap;
|
---|
| 386 | if (kind !== 0) {
|
---|
| 387 | protoInitializers = protoInitializers || [];
|
---|
| 388 | initializers = protoInitializers;
|
---|
| 389 | }
|
---|
| 390 | }
|
---|
| 391 | if (kind !== 0 && !isPrivate) {
|
---|
| 392 | var existingNonFields = isStatic ? existingStaticNonFields : existingProtoNonFields;
|
---|
| 393 | var existingKind = existingNonFields.get(name) || 0;
|
---|
| 394 | if (existingKind === true || existingKind === 3 && kind !== 4 || existingKind === 4 && kind !== 3) {
|
---|
| 395 | throw new Error("Attempted to decorate a public method/accessor that has the same name as a previously decorated public method/accessor. This is not currently supported by the decorators plugin. Property name was: " + name);
|
---|
| 396 | } else if (!existingKind && kind > 2) {
|
---|
| 397 | existingNonFields.set(name, kind);
|
---|
| 398 | } else {
|
---|
| 399 | existingNonFields.set(name, true);
|
---|
| 400 | }
|
---|
| 401 | }
|
---|
| 402 | old_applyMemberDec(ret, base, decInfo, name, kind, isStatic, isPrivate, metadataMap, initializers);
|
---|
| 403 | }
|
---|
| 404 | old_pushInitializers(ret, protoInitializers);
|
---|
| 405 | old_pushInitializers(ret, staticInitializers);
|
---|
| 406 | }
|
---|
| 407 | function old_pushInitializers(ret, initializers) {
|
---|
| 408 | if (initializers) {
|
---|
| 409 | ret.push(function (instance) {
|
---|
| 410 | for (var i = 0; i < initializers.length; i++) {
|
---|
| 411 | initializers[i].call(instance);
|
---|
| 412 | }
|
---|
| 413 | return instance;
|
---|
| 414 | });
|
---|
| 415 | }
|
---|
| 416 | }
|
---|
| 417 | function old_applyClassDecs(ret, targetClass, metadataMap, classDecs) {
|
---|
| 418 | if (classDecs.length > 0) {
|
---|
| 419 | var initializers = [];
|
---|
| 420 | var newClass = targetClass;
|
---|
| 421 | var name = targetClass.name;
|
---|
| 422 | for (var i = classDecs.length - 1; i >= 0; i--) {
|
---|
| 423 | var decoratorFinishedRef = {
|
---|
| 424 | v: false
|
---|
| 425 | };
|
---|
| 426 | try {
|
---|
| 427 | var ctx = Object.assign({
|
---|
| 428 | kind: "class",
|
---|
| 429 | name: name,
|
---|
| 430 | addInitializer: old_createAddInitializerMethod(initializers, decoratorFinishedRef)
|
---|
| 431 | }, old_createMetadataMethodsForProperty(metadataMap, 0, name, decoratorFinishedRef));
|
---|
| 432 | var nextNewClass = classDecs[i](newClass, ctx);
|
---|
| 433 | } finally {
|
---|
| 434 | decoratorFinishedRef.v = true;
|
---|
| 435 | }
|
---|
| 436 | if (nextNewClass !== undefined) {
|
---|
| 437 | old_assertValidReturnValue(10, nextNewClass);
|
---|
| 438 | newClass = nextNewClass;
|
---|
| 439 | }
|
---|
| 440 | }
|
---|
| 441 | ret.push(newClass, function () {
|
---|
| 442 | for (var i = 0; i < initializers.length; i++) {
|
---|
| 443 | initializers[i].call(newClass);
|
---|
| 444 | }
|
---|
| 445 | });
|
---|
| 446 | }
|
---|
| 447 | }
|
---|
| 448 | function applyDecs(targetClass, memberDecs, classDecs) {
|
---|
| 449 | var ret = [];
|
---|
| 450 | var staticMetadataMap = {};
|
---|
| 451 | var protoMetadataMap = {};
|
---|
| 452 | old_applyMemberDecs(ret, targetClass, protoMetadataMap, staticMetadataMap, memberDecs);
|
---|
| 453 | old_convertMetadataMapToFinal(targetClass.prototype, protoMetadataMap);
|
---|
| 454 | old_applyClassDecs(ret, targetClass, staticMetadataMap, classDecs);
|
---|
| 455 | old_convertMetadataMapToFinal(targetClass, staticMetadataMap);
|
---|
| 456 | return ret;
|
---|
| 457 | }
|
---|
| 458 |
|
---|
| 459 | //# sourceMappingURL=applyDecs.js.map
|
---|