decorate.js 11 KB

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