decorate.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. var toArray = require("./toArray.js");
  2. var toPropertyKey = require("./toPropertyKey.js");
  3. function _decorate(e, r, t, i) {
  4. var o = _getDecoratorsApi();
  5. if (i) for (var n = 0; n < i.length; n++) o = i[n](o);
  6. var s = r(function (e) {
  7. o.initializeInstanceElements(e, a.elements);
  8. }, t),
  9. a = o.decorateClass(_coalesceClassElements(s.d.map(_createElementDescriptor)), e);
  10. return o.initializeClassElements(s.F, a.elements), o.runClassFinishers(s.F, a.finishers);
  11. }
  12. function _getDecoratorsApi() {
  13. _getDecoratorsApi = function _getDecoratorsApi() {
  14. return e;
  15. };
  16. var e = {
  17. elementsDefinitionOrder: [["method"], ["field"]],
  18. initializeInstanceElements: function initializeInstanceElements(e, r) {
  19. ["method", "field"].forEach(function (t) {
  20. r.forEach(function (r) {
  21. r.kind === t && "own" === r.placement && this.defineClassElement(e, r);
  22. }, this);
  23. }, this);
  24. },
  25. initializeClassElements: function initializeClassElements(e, r) {
  26. var t = e.prototype;
  27. ["method", "field"].forEach(function (i) {
  28. r.forEach(function (r) {
  29. var o = r.placement;
  30. if (r.kind === i && ("static" === o || "prototype" === o)) {
  31. var n = "static" === o ? e : t;
  32. this.defineClassElement(n, r);
  33. }
  34. }, this);
  35. }, this);
  36. },
  37. defineClassElement: function defineClassElement(e, r) {
  38. var t = r.descriptor;
  39. if ("field" === r.kind) {
  40. var i = r.initializer;
  41. t = {
  42. enumerable: t.enumerable,
  43. writable: t.writable,
  44. configurable: t.configurable,
  45. value: void 0 === i ? void 0 : i.call(e)
  46. };
  47. }
  48. Object.defineProperty(e, r.key, t);
  49. },
  50. decorateClass: function decorateClass(e, r) {
  51. var t = [],
  52. i = [],
  53. o = {
  54. "static": [],
  55. prototype: [],
  56. own: []
  57. };
  58. if (e.forEach(function (e) {
  59. this.addElementPlacement(e, o);
  60. }, this), e.forEach(function (e) {
  61. if (!_hasDecorators(e)) return t.push(e);
  62. var r = this.decorateElement(e, o);
  63. t.push(r.element), t.push.apply(t, r.extras), i.push.apply(i, r.finishers);
  64. }, this), !r) return {
  65. elements: t,
  66. finishers: i
  67. };
  68. var n = this.decorateConstructor(t, r);
  69. return i.push.apply(i, n.finishers), n.finishers = i, n;
  70. },
  71. addElementPlacement: function addElementPlacement(e, r, t) {
  72. var i = r[e.placement];
  73. if (!t && -1 !== i.indexOf(e.key)) throw new TypeError("Duplicated element (" + e.key + ")");
  74. i.push(e.key);
  75. },
  76. decorateElement: function decorateElement(e, r) {
  77. for (var t = [], i = [], o = e.decorators, n = o.length - 1; n >= 0; n--) {
  78. var s = r[e.placement];
  79. s.splice(s.indexOf(e.key), 1);
  80. var a = this.fromElementDescriptor(e),
  81. l = this.toElementFinisherExtras((0, o[n])(a) || a);
  82. e = l.element, this.addElementPlacement(e, r), l.finisher && i.push(l.finisher);
  83. var c = l.extras;
  84. if (c) {
  85. for (var p = 0; p < c.length; p++) this.addElementPlacement(c[p], r);
  86. t.push.apply(t, c);
  87. }
  88. }
  89. return {
  90. element: e,
  91. finishers: i,
  92. extras: t
  93. };
  94. },
  95. decorateConstructor: function decorateConstructor(e, r) {
  96. for (var t = [], i = r.length - 1; i >= 0; i--) {
  97. var o = this.fromClassDescriptor(e),
  98. n = this.toClassDescriptor((0, r[i])(o) || o);
  99. if (void 0 !== n.finisher && t.push(n.finisher), void 0 !== n.elements) {
  100. e = n.elements;
  101. for (var s = 0; s < e.length - 1; s++) for (var a = s + 1; a < e.length; a++) if (e[s].key === e[a].key && e[s].placement === e[a].placement) throw new TypeError("Duplicated element (" + e[s].key + ")");
  102. }
  103. }
  104. return {
  105. elements: e,
  106. finishers: t
  107. };
  108. },
  109. fromElementDescriptor: function fromElementDescriptor(e) {
  110. var r = {
  111. kind: e.kind,
  112. key: e.key,
  113. placement: e.placement,
  114. descriptor: e.descriptor
  115. };
  116. return Object.defineProperty(r, Symbol.toStringTag, {
  117. value: "Descriptor",
  118. configurable: !0
  119. }), "field" === e.kind && (r.initializer = e.initializer), r;
  120. },
  121. toElementDescriptors: function toElementDescriptors(e) {
  122. if (void 0 !== e) return toArray(e).map(function (e) {
  123. var r = this.toElementDescriptor(e);
  124. return this.disallowProperty(e, "finisher", "An element descriptor"), this.disallowProperty(e, "extras", "An element descriptor"), r;
  125. }, this);
  126. },
  127. toElementDescriptor: function toElementDescriptor(e) {
  128. var r = e.kind + "";
  129. if ("method" !== r && "field" !== r) throw new TypeError('An element descriptor\'s .kind property must be either "method" or "field", but a decorator created an element descriptor with .kind "' + r + '"');
  130. var t = toPropertyKey(e.key),
  131. i = e.placement + "";
  132. if ("static" !== i && "prototype" !== i && "own" !== i) throw new TypeError('An element descriptor\'s .placement property must be one of "static", "prototype" or "own", but a decorator created an element descriptor with .placement "' + i + '"');
  133. var o = e.descriptor;
  134. this.disallowProperty(e, "elements", "An element descriptor");
  135. var n = {
  136. kind: r,
  137. key: t,
  138. placement: i,
  139. descriptor: Object.assign({}, o)
  140. };
  141. return "field" !== r ? this.disallowProperty(e, "initializer", "A method descriptor") : (this.disallowProperty(o, "get", "The property descriptor of a field descriptor"), this.disallowProperty(o, "set", "The property descriptor of a field descriptor"), this.disallowProperty(o, "value", "The property descriptor of a field descriptor"), n.initializer = e.initializer), n;
  142. },
  143. toElementFinisherExtras: function toElementFinisherExtras(e) {
  144. return {
  145. element: this.toElementDescriptor(e),
  146. finisher: _optionalCallableProperty(e, "finisher"),
  147. extras: this.toElementDescriptors(e.extras)
  148. };
  149. },
  150. fromClassDescriptor: function fromClassDescriptor(e) {
  151. var r = {
  152. kind: "class",
  153. elements: e.map(this.fromElementDescriptor, this)
  154. };
  155. return Object.defineProperty(r, Symbol.toStringTag, {
  156. value: "Descriptor",
  157. configurable: !0
  158. }), r;
  159. },
  160. toClassDescriptor: function toClassDescriptor(e) {
  161. var r = e.kind + "";
  162. if ("class" !== r) throw new TypeError('A class descriptor\'s .kind property must be "class", but a decorator created a class descriptor with .kind "' + r + '"');
  163. this.disallowProperty(e, "key", "A class descriptor"), this.disallowProperty(e, "placement", "A class descriptor"), this.disallowProperty(e, "descriptor", "A class descriptor"), this.disallowProperty(e, "initializer", "A class descriptor"), this.disallowProperty(e, "extras", "A class descriptor");
  164. var t = _optionalCallableProperty(e, "finisher");
  165. return {
  166. elements: this.toElementDescriptors(e.elements),
  167. finisher: t
  168. };
  169. },
  170. runClassFinishers: function runClassFinishers(e, r) {
  171. for (var t = 0; t < r.length; t++) {
  172. var i = (0, r[t])(e);
  173. if (void 0 !== i) {
  174. if ("function" != typeof i) throw new TypeError("Finishers must return a constructor.");
  175. e = i;
  176. }
  177. }
  178. return e;
  179. },
  180. disallowProperty: function disallowProperty(e, r, t) {
  181. if (void 0 !== e[r]) throw new TypeError(t + " can't have a ." + r + " property.");
  182. }
  183. };
  184. return e;
  185. }
  186. function _createElementDescriptor(e) {
  187. var r,
  188. t = toPropertyKey(e.key);
  189. "method" === e.kind ? r = {
  190. value: e.value,
  191. writable: !0,
  192. configurable: !0,
  193. enumerable: !1
  194. } : "get" === e.kind ? r = {
  195. get: e.value,
  196. configurable: !0,
  197. enumerable: !1
  198. } : "set" === e.kind ? r = {
  199. set: e.value,
  200. configurable: !0,
  201. enumerable: !1
  202. } : "field" === e.kind && (r = {
  203. configurable: !0,
  204. writable: !0,
  205. enumerable: !0
  206. });
  207. var i = {
  208. kind: "field" === e.kind ? "field" : "method",
  209. key: t,
  210. placement: e["static"] ? "static" : "field" === e.kind ? "own" : "prototype",
  211. descriptor: r
  212. };
  213. return e.decorators && (i.decorators = e.decorators), "field" === e.kind && (i.initializer = e.value), i;
  214. }
  215. function _coalesceGetterSetter(e, r) {
  216. void 0 !== e.descriptor.get ? r.descriptor.get = e.descriptor.get : r.descriptor.set = e.descriptor.set;
  217. }
  218. function _coalesceClassElements(e) {
  219. for (var r = [], isSameElement = function isSameElement(e) {
  220. return "method" === e.kind && e.key === o.key && e.placement === o.placement;
  221. }, t = 0; t < e.length; t++) {
  222. var i,
  223. o = e[t];
  224. if ("method" === o.kind && (i = r.find(isSameElement))) {
  225. if (_isDataDescriptor(o.descriptor) || _isDataDescriptor(i.descriptor)) {
  226. if (_hasDecorators(o) || _hasDecorators(i)) throw new ReferenceError("Duplicated methods (" + o.key + ") can't be decorated.");
  227. i.descriptor = o.descriptor;
  228. } else {
  229. if (_hasDecorators(o)) {
  230. if (_hasDecorators(i)) throw new ReferenceError("Decorators can't be placed on different accessors with for the same property (" + o.key + ").");
  231. i.decorators = o.decorators;
  232. }
  233. _coalesceGetterSetter(o, i);
  234. }
  235. } else r.push(o);
  236. }
  237. return r;
  238. }
  239. function _hasDecorators(e) {
  240. return e.decorators && e.decorators.length;
  241. }
  242. function _isDataDescriptor(e) {
  243. return void 0 !== e && !(void 0 === e.value && void 0 === e.writable);
  244. }
  245. function _optionalCallableProperty(e, r) {
  246. var t = e[r];
  247. if (void 0 !== t && "function" != typeof t) throw new TypeError("Expected '" + r + "' to be a function");
  248. return t;
  249. }
  250. module.exports = _decorate, module.exports.__esModule = true, module.exports["default"] = module.exports;