applyDecs.js 12 KB

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