decorate.js 9.9 KB

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