applyDecs.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. import _typeof from "./typeof.js";
  2. function createMetadataMethodsForProperty(metadataMap, kind, property, decoratorFinishedRef) {
  3. return {
  4. getMetadata: function getMetadata(key) {
  5. assertNotFinished(decoratorFinishedRef, "getMetadata"), 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. assertNotFinished(decoratorFinishedRef, "setMetadata"), 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 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 createAddInitializerMethod(initializers, decoratorFinishedRef) {
  51. return function (initializer) {
  52. assertNotFinished(decoratorFinishedRef, "addInitializer"), assertCallable(initializer, "An initializer"), initializers.push(initializer);
  53. };
  54. }
  55. function 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 = 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, createMetadataMethodsForProperty(metadataMap, metadataKind, metadataName, decoratorFinishedRef)));
  97. } finally {
  98. decoratorFinishedRef.v = !0;
  99. }
  100. }
  101. function assertNotFinished(decoratorFinishedRef, fnName) {
  102. if (decoratorFinishedRef.v) throw new Error("attempted to call " + fnName + " after decoration was finished");
  103. }
  104. function assertMetadataKey(key) {
  105. if ("symbol" != _typeof(key)) throw new TypeError("Metadata keys must be symbols, received: " + key);
  106. }
  107. function assertCallable(fn, hint) {
  108. if ("function" != typeof fn) throw new TypeError(hint + " must be a function");
  109. }
  110. function 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 && 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");
  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 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 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 = 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 = {
  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 = 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 = {
  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++) {
  161. value = ownInitializers[i].call(instance, value);
  162. }
  163. return value;
  164. };
  165. } else {
  166. var originalInitializer = initializer;
  167. initializer = function initializer(instance, init) {
  168. return originalInitializer.call(instance, init);
  169. };
  170. }
  171. ret.push(initializer);
  172. }
  173. 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) {
  174. return value.get.call(instance, args);
  175. }), ret.push(function (instance, args) {
  176. return value.set.call(instance, args);
  177. })) : 2 === kind ? ret.push(value) : ret.push(function (instance, args) {
  178. return value.call(instance, args);
  179. }) : Object.defineProperty(base, name, desc));
  180. }
  181. function applyMemberDecs(ret, Class, protoMetadataMap, staticMetadataMap, decInfos) {
  182. for (var protoInitializers, staticInitializers, existingProtoNonFields = new Map(), existingStaticNonFields = new Map(), i = 0; i < decInfos.length; i++) {
  183. var decInfo = decInfos[i];
  184. if (Array.isArray(decInfo)) {
  185. var base,
  186. metadataMap,
  187. initializers,
  188. kind = decInfo[1],
  189. name = decInfo[2],
  190. isPrivate = decInfo.length > 3,
  191. isStatic = kind >= 5;
  192. 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) {
  193. var existingNonFields = isStatic ? existingStaticNonFields : existingProtoNonFields,
  194. existingKind = existingNonFields.get(name) || 0;
  195. 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);
  196. !existingKind && kind > 2 ? existingNonFields.set(name, kind) : existingNonFields.set(name, !0);
  197. }
  198. applyMemberDec(ret, base, decInfo, name, kind, isStatic, isPrivate, metadataMap, initializers);
  199. }
  200. }
  201. pushInitializers(ret, protoInitializers), pushInitializers(ret, staticInitializers);
  202. }
  203. function pushInitializers(ret, initializers) {
  204. initializers && ret.push(function (instance) {
  205. for (var i = 0; i < initializers.length; i++) {
  206. initializers[i].call(instance);
  207. }
  208. return instance;
  209. });
  210. }
  211. function applyClassDecs(ret, targetClass, metadataMap, classDecs) {
  212. if (classDecs.length > 0) {
  213. for (var initializers = [], newClass = targetClass, name = targetClass.name, i = classDecs.length - 1; i >= 0; i--) {
  214. var decoratorFinishedRef = {
  215. v: !1
  216. };
  217. try {
  218. var ctx = Object.assign({
  219. kind: "class",
  220. name: name,
  221. addInitializer: createAddInitializerMethod(initializers, decoratorFinishedRef)
  222. }, createMetadataMethodsForProperty(metadataMap, 0, name, decoratorFinishedRef)),
  223. nextNewClass = classDecs[i](newClass, ctx);
  224. } finally {
  225. decoratorFinishedRef.v = !0;
  226. }
  227. void 0 !== nextNewClass && (assertValidReturnValue(10, nextNewClass), newClass = nextNewClass);
  228. }
  229. ret.push(newClass, function () {
  230. for (var i = 0; i < initializers.length; i++) {
  231. initializers[i].call(newClass);
  232. }
  233. });
  234. }
  235. }
  236. export default function applyDecs(targetClass, memberDecs, classDecs) {
  237. var ret = [],
  238. staticMetadataMap = {},
  239. protoMetadataMap = {};
  240. return applyMemberDecs(ret, targetClass, protoMetadataMap, staticMetadataMap, memberDecs), convertMetadataMapToFinal(targetClass.prototype, protoMetadataMap), applyClassDecs(ret, targetClass, staticMetadataMap, classDecs), convertMetadataMapToFinal(targetClass, staticMetadataMap), ret;
  241. }