applyDecs.js 12 KB

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