applyDecs.js 12 KB

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