applyDecs2301.js 9.1 KB

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