applyDecs2301.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import _typeof from "./typeof.js";
  2. import _bindInstanceProperty from "@babel/runtime-corejs3/core-js/instance/bind";
  3. import _Object$getOwnPropertyDescriptor from "@babel/runtime-corejs3/core-js/object/get-own-property-descriptor";
  4. import _Object$defineProperty from "@babel/runtime-corejs3/core-js/object/define-property";
  5. import _Map from "@babel/runtime-corejs3/core-js/map";
  6. import _Array$isArray from "@babel/runtime-corejs3/core-js/array/is-array";
  7. import checkInRHS from "./checkInRHS.js";
  8. function createAddInitializerMethod(initializers, decoratorFinishedRef) {
  9. return function (initializer) {
  10. assertNotFinished(decoratorFinishedRef, "addInitializer"), assertCallable(initializer, "An initializer"), initializers.push(initializer);
  11. };
  12. }
  13. function assertInstanceIfPrivate(has, target) {
  14. if (!has(target)) throw new TypeError("Attempted to access private element on non-instance");
  15. }
  16. function memberDec(dec, name, desc, initializers, kind, isStatic, isPrivate, value, hasPrivateBrand) {
  17. var kindStr;
  18. switch (kind) {
  19. case 1:
  20. kindStr = "accessor";
  21. break;
  22. case 2:
  23. kindStr = "method";
  24. break;
  25. case 3:
  26. kindStr = "getter";
  27. break;
  28. case 4:
  29. kindStr = "setter";
  30. break;
  31. default:
  32. kindStr = "field";
  33. }
  34. var get,
  35. set,
  36. ctx = {
  37. kind: kindStr,
  38. name: isPrivate ? "#" + name : name,
  39. "static": isStatic,
  40. "private": isPrivate
  41. },
  42. decoratorFinishedRef = {
  43. v: !1
  44. };
  45. if (0 !== kind && (ctx.addInitializer = createAddInitializerMethod(initializers, decoratorFinishedRef)), isPrivate || 0 !== kind && 2 !== kind) {
  46. if (2 === kind) get = function get(target) {
  47. return assertInstanceIfPrivate(hasPrivateBrand, target), desc.value;
  48. };else {
  49. var t = 0 === kind || 1 === kind;
  50. (t || 3 === kind) && (get = isPrivate ? function (target) {
  51. return assertInstanceIfPrivate(hasPrivateBrand, target), desc.get.call(target);
  52. } : function (target) {
  53. return desc.get.call(target);
  54. }), (t || 4 === kind) && (set = isPrivate ? function (target, value) {
  55. assertInstanceIfPrivate(hasPrivateBrand, target), desc.set.call(target, value);
  56. } : function (target, value) {
  57. desc.set.call(target, value);
  58. });
  59. }
  60. } else get = function get(target) {
  61. return target[name];
  62. }, 0 === kind && (set = function set(target, v) {
  63. target[name] = v;
  64. });
  65. var has = isPrivate ? _bindInstanceProperty(hasPrivateBrand).call(hasPrivateBrand) : function (target) {
  66. return name in target;
  67. };
  68. ctx.access = get && set ? {
  69. get: get,
  70. set: set,
  71. has: has
  72. } : get ? {
  73. get: get,
  74. has: has
  75. } : {
  76. set: set,
  77. has: has
  78. };
  79. try {
  80. return dec(value, ctx);
  81. } finally {
  82. decoratorFinishedRef.v = !0;
  83. }
  84. }
  85. function assertNotFinished(decoratorFinishedRef, fnName) {
  86. if (decoratorFinishedRef.v) throw new Error("attempted to call " + fnName + " after decoration was finished");
  87. }
  88. function assertCallable(fn, hint) {
  89. if ("function" != typeof fn) throw new TypeError(hint + " must be a function");
  90. }
  91. function assertValidReturnValue(kind, value) {
  92. var type = _typeof(value);
  93. if (1 === kind) {
  94. if ("object" !== type || null === value) throw new TypeError("accessor decorators must return an object with get, set, or init properties or void 0");
  95. 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");
  96. } else if ("function" !== type) {
  97. var hint;
  98. throw hint = 0 === kind ? "field" : 10 === kind ? "class" : "method", new TypeError(hint + " decorators must return a function or void 0");
  99. }
  100. }
  101. function curryThis1(fn) {
  102. return function () {
  103. return fn(this);
  104. };
  105. }
  106. function curryThis2(fn) {
  107. return function (value) {
  108. fn(this, value);
  109. };
  110. }
  111. function applyMemberDec(ret, base, decInfo, name, kind, isStatic, isPrivate, initializers, hasPrivateBrand) {
  112. var desc,
  113. init,
  114. value,
  115. newValue,
  116. get,
  117. set,
  118. decs = decInfo[0];
  119. if (isPrivate ? desc = 0 === kind || 1 === kind ? {
  120. get: curryThis1(decInfo[3]),
  121. set: curryThis2(decInfo[4])
  122. } : 3 === kind ? {
  123. get: decInfo[3]
  124. } : 4 === kind ? {
  125. set: decInfo[3]
  126. } : {
  127. value: decInfo[3]
  128. } : 0 !== kind && (desc = _Object$getOwnPropertyDescriptor(base, name)), 1 === kind ? value = {
  129. get: desc.get,
  130. set: desc.set
  131. } : 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, initializers, kind, isStatic, isPrivate, value, hasPrivateBrand)) && (assertValidReturnValue(kind, newValue), 0 === kind ? init = newValue : 1 === kind ? (init = newValue.init, get = newValue.get || value.get, set = newValue.set || value.set, value = {
  132. get: get,
  133. set: set
  134. }) : value = newValue);else for (var i = decs.length - 1; i >= 0; i--) {
  135. var newInit;
  136. if (void 0 !== (newValue = memberDec(decs[i], name, desc, initializers, kind, isStatic, isPrivate, value, hasPrivateBrand))) assertValidReturnValue(kind, newValue), 0 === kind ? newInit = newValue : 1 === kind ? (newInit = newValue.init, get = newValue.get || value.get, set = newValue.set || value.set, value = {
  137. get: get,
  138. set: set
  139. }) : value = newValue, void 0 !== newInit && (void 0 === init ? init = newInit : "function" == typeof init ? init = [init, newInit] : init.push(newInit));
  140. }
  141. if (0 === kind || 1 === kind) {
  142. if (void 0 === init) init = function init(instance, _init) {
  143. return _init;
  144. };else if ("function" != typeof init) {
  145. var ownInitializers = init;
  146. init = function init(instance, _init2) {
  147. for (var value = _init2, i = 0; i < ownInitializers.length; i++) value = ownInitializers[i].call(instance, value);
  148. return value;
  149. };
  150. } else {
  151. var originalInitializer = init;
  152. init = function init(instance, _init3) {
  153. return originalInitializer.call(instance, _init3);
  154. };
  155. }
  156. ret.push(init);
  157. }
  158. 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) {
  159. return value.get.call(instance, args);
  160. }), ret.push(function (instance, args) {
  161. return value.set.call(instance, args);
  162. })) : 2 === kind ? ret.push(value) : ret.push(function (instance, args) {
  163. return value.call(instance, args);
  164. }) : _Object$defineProperty(base, name, desc));
  165. }
  166. function applyMemberDecs(Class, decInfos, instanceBrand) {
  167. for (var protoInitializers, staticInitializers, staticBrand, ret = [], existingProtoNonFields = new _Map(), existingStaticNonFields = new _Map(), i = 0; i < decInfos.length; i++) {
  168. var decInfo = decInfos[i];
  169. if (_Array$isArray(decInfo)) {
  170. var base,
  171. initializers,
  172. kind = decInfo[1],
  173. name = decInfo[2],
  174. isPrivate = decInfo.length > 3,
  175. isStatic = kind >= 5,
  176. hasPrivateBrand = instanceBrand;
  177. if (isStatic ? (base = Class, 0 !== (kind -= 5) && (initializers = staticInitializers = staticInitializers || []), isPrivate && !staticBrand && (staticBrand = function staticBrand(_) {
  178. return checkInRHS(_) === Class;
  179. }), hasPrivateBrand = staticBrand) : (base = Class.prototype, 0 !== kind && (initializers = protoInitializers = protoInitializers || [])), 0 !== kind && !isPrivate) {
  180. var existingNonFields = isStatic ? existingStaticNonFields : existingProtoNonFields,
  181. existingKind = existingNonFields.get(name) || 0;
  182. 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);
  183. !existingKind && kind > 2 ? existingNonFields.set(name, kind) : existingNonFields.set(name, !0);
  184. }
  185. applyMemberDec(ret, base, decInfo, name, kind, isStatic, isPrivate, initializers, hasPrivateBrand);
  186. }
  187. }
  188. return pushInitializers(ret, protoInitializers), pushInitializers(ret, staticInitializers), ret;
  189. }
  190. function pushInitializers(ret, initializers) {
  191. initializers && ret.push(function (instance) {
  192. for (var i = 0; i < initializers.length; i++) initializers[i].call(instance);
  193. return instance;
  194. });
  195. }
  196. function applyClassDecs(targetClass, classDecs) {
  197. if (classDecs.length > 0) {
  198. for (var initializers = [], newClass = targetClass, name = targetClass.name, i = classDecs.length - 1; i >= 0; i--) {
  199. var decoratorFinishedRef = {
  200. v: !1
  201. };
  202. try {
  203. var nextNewClass = classDecs[i](newClass, {
  204. kind: "class",
  205. name: name,
  206. addInitializer: createAddInitializerMethod(initializers, decoratorFinishedRef)
  207. });
  208. } finally {
  209. decoratorFinishedRef.v = !0;
  210. }
  211. void 0 !== nextNewClass && (assertValidReturnValue(10, nextNewClass), newClass = nextNewClass);
  212. }
  213. return [newClass, function () {
  214. for (var i = 0; i < initializers.length; i++) initializers[i].call(newClass);
  215. }];
  216. }
  217. }
  218. export default function applyDecs2301(targetClass, memberDecs, classDecs, instanceBrand) {
  219. return {
  220. e: applyMemberDecs(targetClass, memberDecs, instanceBrand),
  221. get c() {
  222. return applyClassDecs(targetClass, classDecs);
  223. }
  224. };
  225. }