applyDecs.js 13 KB

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