applyDecs2203.js 7.8 KB

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