applyDecs2301.js 9.4 KB

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