applyDecs.js (11847B)
1 var _typeof = require("./typeof.js")["default"]; 2 3 function createMetadataMethodsForProperty(metadataMap, kind, property, decoratorFinishedRef) { 4 return { 5 getMetadata: function getMetadata(key) { 6 assertNotFinished(decoratorFinishedRef, "getMetadata"), assertMetadataKey(key); 7 var metadataForKey = metadataMap[key]; 8 if (void 0 !== metadataForKey) if (1 === kind) { 9 var pub = metadataForKey["public"]; 10 if (void 0 !== pub) return pub[property]; 11 } else if (2 === kind) { 12 var priv = metadataForKey["private"]; 13 if (void 0 !== priv) return priv.get(property); 14 } else if (Object.hasOwnProperty.call(metadataForKey, "constructor")) return metadataForKey.constructor; 15 }, 16 setMetadata: function setMetadata(key, value) { 17 assertNotFinished(decoratorFinishedRef, "setMetadata"), assertMetadataKey(key); 18 var metadataForKey = metadataMap[key]; 19 20 if (void 0 === metadataForKey && (metadataForKey = metadataMap[key] = {}), 1 === kind) { 21 var pub = metadataForKey["public"]; 22 void 0 === pub && (pub = metadataForKey["public"] = {}), pub[property] = value; 23 } else if (2 === kind) { 24 var priv = metadataForKey.priv; 25 void 0 === priv && (priv = metadataForKey["private"] = new Map()), priv.set(property, value); 26 } else metadataForKey.constructor = value; 27 } 28 }; 29 } 30 31 function convertMetadataMapToFinal(obj, metadataMap) { 32 var parentMetadataMap = obj[Symbol.metadata || Symbol["for"]("Symbol.metadata")], 33 metadataKeys = Object.getOwnPropertySymbols(metadataMap); 34 35 if (0 !== metadataKeys.length) { 36 for (var i = 0; i < metadataKeys.length; i++) { 37 var key = metadataKeys[i], 38 metaForKey = metadataMap[key], 39 parentMetaForKey = parentMetadataMap ? parentMetadataMap[key] : null, 40 pub = metaForKey["public"], 41 parentPub = parentMetaForKey ? parentMetaForKey["public"] : null; 42 pub && parentPub && Object.setPrototypeOf(pub, parentPub); 43 var priv = metaForKey["private"]; 44 45 if (priv) { 46 var privArr = Array.from(priv.values()), 47 parentPriv = parentMetaForKey ? parentMetaForKey["private"] : null; 48 parentPriv && (privArr = privArr.concat(parentPriv)), metaForKey["private"] = privArr; 49 } 50 51 parentMetaForKey && Object.setPrototypeOf(metaForKey, parentMetaForKey); 52 } 53 54 parentMetadataMap && Object.setPrototypeOf(metadataMap, parentMetadataMap), obj[Symbol.metadata || Symbol["for"]("Symbol.metadata")] = metadataMap; 55 } 56 } 57 58 function createAddInitializerMethod(initializers, decoratorFinishedRef) { 59 return function (initializer) { 60 assertNotFinished(decoratorFinishedRef, "addInitializer"), assertCallable(initializer, "An initializer"), initializers.push(initializer); 61 }; 62 } 63 64 function memberDec(dec, name, desc, metadataMap, initializers, kind, isStatic, isPrivate, value) { 65 var kindStr; 66 67 switch (kind) { 68 case 1: 69 kindStr = "accessor"; 70 break; 71 72 case 2: 73 kindStr = "method"; 74 break; 75 76 case 3: 77 kindStr = "getter"; 78 break; 79 80 case 4: 81 kindStr = "setter"; 82 break; 83 84 default: 85 kindStr = "field"; 86 } 87 88 var metadataKind, 89 metadataName, 90 ctx = { 91 kind: kindStr, 92 name: isPrivate ? "#" + name : name, 93 isStatic: isStatic, 94 isPrivate: isPrivate 95 }, 96 decoratorFinishedRef = { 97 v: !1 98 }; 99 100 if (0 !== kind && (ctx.addInitializer = createAddInitializerMethod(initializers, decoratorFinishedRef)), isPrivate) { 101 metadataKind = 2, metadataName = Symbol(name); 102 var access = {}; 103 0 === kind ? (access.get = desc.get, access.set = desc.set) : 2 === kind ? access.get = function () { 104 return desc.value; 105 } : (1 !== kind && 3 !== kind || (access.get = function () { 106 return desc.get.call(this); 107 }), 1 !== kind && 4 !== kind || (access.set = function (v) { 108 desc.set.call(this, v); 109 })), ctx.access = access; 110 } else metadataKind = 1, metadataName = name; 111 112 try { 113 return dec(value, Object.assign(ctx, createMetadataMethodsForProperty(metadataMap, metadataKind, metadataName, decoratorFinishedRef))); 114 } finally { 115 decoratorFinishedRef.v = !0; 116 } 117 } 118 119 function assertNotFinished(decoratorFinishedRef, fnName) { 120 if (decoratorFinishedRef.v) throw new Error("attempted to call " + fnName + " after decoration was finished"); 121 } 122 123 function assertMetadataKey(key) { 124 if ("symbol" != _typeof(key)) throw new TypeError("Metadata keys must be symbols, received: " + key); 125 } 126 127 function assertCallable(fn, hint) { 128 if ("function" != typeof fn) throw new TypeError(hint + " must be a function"); 129 } 130 131 function assertValidReturnValue(kind, value) { 132 var type = _typeof(value); 133 134 if (1 === kind) { 135 if ("object" !== type || null === value) throw new TypeError("accessor decorators must return an object with get, set, or init properties or void 0"); 136 void 0 !== value.get && assertCallable(value.get, "accessor.get"), void 0 !== value.set && assertCallable(value.set, "accessor.set"), void 0 !== value.init && assertCallable(value.init, "accessor.init"), void 0 !== value.initializer && assertCallable(value.initializer, "accessor.initializer"); 137 } else if ("function" !== type) { 138 var hint; 139 throw hint = 0 === kind ? "field" : 10 === kind ? "class" : "method", new TypeError(hint + " decorators must return a function or void 0"); 140 } 141 } 142 143 function getInit(desc) { 144 var initializer; 145 return null == (initializer = desc.init) && (initializer = desc.initializer) && "undefined" != typeof console && console.warn(".initializer has been renamed to .init as of March 2022"), initializer; 146 } 147 148 function applyMemberDec(ret, base, decInfo, name, kind, isStatic, isPrivate, metadataMap, initializers) { 149 var desc, 150 initializer, 151 value, 152 newValue, 153 get, 154 set, 155 decs = decInfo[0]; 156 if (isPrivate ? desc = 0 === kind || 1 === kind ? { 157 get: decInfo[3], 158 set: decInfo[4] 159 } : 3 === kind ? { 160 get: decInfo[3] 161 } : 4 === kind ? { 162 set: decInfo[3] 163 } : { 164 value: decInfo[3] 165 } : 0 !== kind && (desc = Object.getOwnPropertyDescriptor(base, name)), 1 === kind ? value = { 166 get: desc.get, 167 set: desc.set 168 } : 2 === kind ? value = desc.value : 3 === kind ? value = desc.get : 4 === kind && (value = desc.set), "function" == typeof decs) void 0 !== (newValue = memberDec(decs, name, desc, metadataMap, initializers, kind, isStatic, isPrivate, value)) && (assertValidReturnValue(kind, newValue), 0 === kind ? initializer = newValue : 1 === kind ? (initializer = getInit(newValue), get = newValue.get || value.get, set = newValue.set || value.set, value = { 169 get: get, 170 set: set 171 }) : value = newValue);else for (var i = decs.length - 1; i >= 0; i--) { 172 var newInit; 173 if (void 0 !== (newValue = memberDec(decs[i], name, desc, metadataMap, initializers, kind, isStatic, isPrivate, value))) assertValidReturnValue(kind, newValue), 0 === kind ? newInit = newValue : 1 === kind ? (newInit = getInit(newValue), get = newValue.get || value.get, set = newValue.set || value.set, value = { 174 get: get, 175 set: set 176 }) : value = newValue, void 0 !== newInit && (void 0 === initializer ? initializer = newInit : "function" == typeof initializer ? initializer = [initializer, newInit] : initializer.push(newInit)); 177 } 178 179 if (0 === kind || 1 === kind) { 180 if (void 0 === initializer) initializer = function initializer(instance, init) { 181 return init; 182 };else if ("function" != typeof initializer) { 183 var ownInitializers = initializer; 184 185 initializer = function initializer(instance, init) { 186 for (var value = init, i = 0; i < ownInitializers.length; i++) { 187 value = ownInitializers[i].call(instance, value); 188 } 189 190 return value; 191 }; 192 } else { 193 var originalInitializer = initializer; 194 195 initializer = function initializer(instance, init) { 196 return originalInitializer.call(instance, init); 197 }; 198 } 199 ret.push(initializer); 200 } 201 202 0 !== kind && (1 === kind ? (desc.get = value.get, desc.set = value.set) : 2 === kind ? desc.value = value : 3 === kind ? desc.get = value : 4 === kind && (desc.set = value), isPrivate ? 1 === kind ? (ret.push(function (instance, args) { 203 return value.get.call(instance, args); 204 }), ret.push(function (instance, args) { 205 return value.set.call(instance, args); 206 })) : 2 === kind ? ret.push(value) : ret.push(function (instance, args) { 207 return value.call(instance, args); 208 }) : Object.defineProperty(base, name, desc)); 209 } 210 211 function applyMemberDecs(ret, Class, protoMetadataMap, staticMetadataMap, decInfos) { 212 for (var protoInitializers, staticInitializers, existingProtoNonFields = new Map(), existingStaticNonFields = new Map(), i = 0; i < decInfos.length; i++) { 213 var decInfo = decInfos[i]; 214 215 if (Array.isArray(decInfo)) { 216 var base, 217 metadataMap, 218 initializers, 219 kind = decInfo[1], 220 name = decInfo[2], 221 isPrivate = decInfo.length > 3, 222 isStatic = kind >= 5; 223 224 if (isStatic ? (base = Class, metadataMap = staticMetadataMap, 0 !== (kind -= 5) && (initializers = staticInitializers = staticInitializers || [])) : (base = Class.prototype, metadataMap = protoMetadataMap, 0 !== kind && (initializers = protoInitializers = protoInitializers || [])), 0 !== kind && !isPrivate) { 225 var existingNonFields = isStatic ? existingStaticNonFields : existingProtoNonFields, 226 existingKind = existingNonFields.get(name) || 0; 227 if (!0 === existingKind || 3 === existingKind && 4 !== kind || 4 === existingKind && 3 !== kind) 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); 228 !existingKind && kind > 2 ? existingNonFields.set(name, kind) : existingNonFields.set(name, !0); 229 } 230 231 applyMemberDec(ret, base, decInfo, name, kind, isStatic, isPrivate, metadataMap, initializers); 232 } 233 } 234 235 pushInitializers(ret, protoInitializers), pushInitializers(ret, staticInitializers); 236 } 237 238 function pushInitializers(ret, initializers) { 239 initializers && ret.push(function (instance) { 240 for (var i = 0; i < initializers.length; i++) { 241 initializers[i].call(instance); 242 } 243 244 return instance; 245 }); 246 } 247 248 function applyClassDecs(ret, targetClass, metadataMap, classDecs) { 249 if (classDecs.length > 0) { 250 for (var initializers = [], newClass = targetClass, name = targetClass.name, i = classDecs.length - 1; i >= 0; i--) { 251 var decoratorFinishedRef = { 252 v: !1 253 }; 254 255 try { 256 var ctx = Object.assign({ 257 kind: "class", 258 name: name, 259 addInitializer: createAddInitializerMethod(initializers, decoratorFinishedRef) 260 }, createMetadataMethodsForProperty(metadataMap, 0, name, decoratorFinishedRef)), 261 nextNewClass = classDecs[i](newClass, ctx); 262 } finally { 263 decoratorFinishedRef.v = !0; 264 } 265 266 void 0 !== nextNewClass && (assertValidReturnValue(10, nextNewClass), newClass = nextNewClass); 267 } 268 269 ret.push(newClass, function () { 270 for (var i = 0; i < initializers.length; i++) { 271 initializers[i].call(newClass); 272 } 273 }); 274 } 275 } 276 277 function applyDecs(targetClass, memberDecs, classDecs) { 278 var ret = [], 279 staticMetadataMap = {}, 280 protoMetadataMap = {}; 281 return applyMemberDecs(ret, targetClass, protoMetadataMap, staticMetadataMap, memberDecs), convertMetadataMapToFinal(targetClass.prototype, protoMetadataMap), applyClassDecs(ret, targetClass, staticMetadataMap, classDecs), convertMetadataMapToFinal(targetClass, staticMetadataMap), ret; 282 } 283 284 module.exports = applyDecs, module.exports.__esModule = true, module.exports["default"] = module.exports;