applyDecs.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. import _typeof from "./typeof.js";
  2. import _Map from "@babel/runtime-corejs3/core-js/map";
  3. import _Symbol from "@babel/runtime-corejs3/core-js/symbol";
  4. import _Symbol$for from "@babel/runtime-corejs3/core-js/symbol/for";
  5. import _Object$getOwnPropertySymbols from "@babel/runtime-corejs3/core-js/object/get-own-property-symbols";
  6. import _Object$setPrototypeOf from "@babel/runtime-corejs3/core-js/object/set-prototype-of";
  7. import _Array$from from "@babel/runtime-corejs3/core-js/array/from";
  8. import _valuesInstanceProperty from "@babel/runtime-corejs3/core-js/instance/values";
  9. import _concatInstanceProperty from "@babel/runtime-corejs3/core-js/instance/concat";
  10. import _Object$assign from "@babel/runtime-corejs3/core-js/object/assign";
  11. import _Object$getOwnPropertyDescriptor from "@babel/runtime-corejs3/core-js/object/get-own-property-descriptor";
  12. import _Object$defineProperty from "@babel/runtime-corejs3/core-js/object/define-property";
  13. import _Array$isArray from "@babel/runtime-corejs3/core-js/array/is-array";
  14. function createMetadataMethodsForProperty(metadataMap, kind, property, decoratorFinishedRef) {
  15. return {
  16. getMetadata: function getMetadata(key) {
  17. assertNotFinished(decoratorFinishedRef, "getMetadata"), 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. assertNotFinished(decoratorFinishedRef, "setMetadata"), 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 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 createAddInitializerMethod(initializers, decoratorFinishedRef) {
  63. return function (initializer) {
  64. assertNotFinished(decoratorFinishedRef, "addInitializer"), assertCallable(initializer, "An initializer"), initializers.push(initializer);
  65. };
  66. }
  67. function 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 = 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, createMetadataMethodsForProperty(metadataMap, metadataKind, metadataName, decoratorFinishedRef)));
  109. } finally {
  110. decoratorFinishedRef.v = !0;
  111. }
  112. }
  113. function assertNotFinished(decoratorFinishedRef, fnName) {
  114. if (decoratorFinishedRef.v) throw new Error("attempted to call " + fnName + " after decoration was finished");
  115. }
  116. function assertMetadataKey(key) {
  117. if ("symbol" != _typeof(key)) throw new TypeError("Metadata keys must be symbols, received: " + key);
  118. }
  119. function assertCallable(fn, hint) {
  120. if ("function" != typeof fn) throw new TypeError(hint + " must be a function");
  121. }
  122. function 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 && 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");
  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 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 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 = 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 = {
  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 = 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 = {
  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++) {
  173. value = ownInitializers[i].call(instance, value);
  174. }
  175. return value;
  176. };
  177. } else {
  178. var originalInitializer = initializer;
  179. initializer = function initializer(instance, init) {
  180. return originalInitializer.call(instance, init);
  181. };
  182. }
  183. ret.push(initializer);
  184. }
  185. 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) {
  186. return value.get.call(instance, args);
  187. }), ret.push(function (instance, args) {
  188. return value.set.call(instance, args);
  189. })) : 2 === kind ? ret.push(value) : ret.push(function (instance, args) {
  190. return value.call(instance, args);
  191. }) : _Object$defineProperty(base, name, desc));
  192. }
  193. function applyMemberDecs(ret, Class, protoMetadataMap, staticMetadataMap, decInfos) {
  194. for (var protoInitializers, staticInitializers, existingProtoNonFields = new _Map(), existingStaticNonFields = new _Map(), i = 0; i < decInfos.length; i++) {
  195. var decInfo = decInfos[i];
  196. if (_Array$isArray(decInfo)) {
  197. var base,
  198. metadataMap,
  199. initializers,
  200. kind = decInfo[1],
  201. name = decInfo[2],
  202. isPrivate = decInfo.length > 3,
  203. isStatic = kind >= 5;
  204. 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) {
  205. var existingNonFields = isStatic ? existingStaticNonFields : existingProtoNonFields,
  206. existingKind = existingNonFields.get(name) || 0;
  207. 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);
  208. !existingKind && kind > 2 ? existingNonFields.set(name, kind) : existingNonFields.set(name, !0);
  209. }
  210. applyMemberDec(ret, base, decInfo, name, kind, isStatic, isPrivate, metadataMap, initializers);
  211. }
  212. }
  213. pushInitializers(ret, protoInitializers), pushInitializers(ret, staticInitializers);
  214. }
  215. function pushInitializers(ret, initializers) {
  216. initializers && ret.push(function (instance) {
  217. for (var i = 0; i < initializers.length; i++) {
  218. initializers[i].call(instance);
  219. }
  220. return instance;
  221. });
  222. }
  223. function applyClassDecs(ret, targetClass, metadataMap, classDecs) {
  224. if (classDecs.length > 0) {
  225. for (var initializers = [], newClass = targetClass, name = targetClass.name, i = classDecs.length - 1; i >= 0; i--) {
  226. var decoratorFinishedRef = {
  227. v: !1
  228. };
  229. try {
  230. var ctx = _Object$assign({
  231. kind: "class",
  232. name: name,
  233. addInitializer: createAddInitializerMethod(initializers, decoratorFinishedRef)
  234. }, createMetadataMethodsForProperty(metadataMap, 0, name, decoratorFinishedRef)),
  235. nextNewClass = classDecs[i](newClass, ctx);
  236. } finally {
  237. decoratorFinishedRef.v = !0;
  238. }
  239. void 0 !== nextNewClass && (assertValidReturnValue(10, nextNewClass), newClass = nextNewClass);
  240. }
  241. ret.push(newClass, function () {
  242. for (var i = 0; i < initializers.length; i++) {
  243. initializers[i].call(newClass);
  244. }
  245. });
  246. }
  247. }
  248. export default function applyDecs(targetClass, memberDecs, classDecs) {
  249. var ret = [],
  250. staticMetadataMap = {},
  251. protoMetadataMap = {};
  252. return applyMemberDecs(ret, targetClass, protoMetadataMap, staticMetadataMap, memberDecs), convertMetadataMapToFinal(targetClass.prototype, protoMetadataMap), applyClassDecs(ret, targetClass, staticMetadataMap, classDecs), convertMetadataMapToFinal(targetClass, staticMetadataMap), ret;
  253. }