applyDecs2305.js 8.4 KB

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