decorate.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. var _mapInstanceProperty = require("@babel/runtime-corejs3/core-js/instance/map");
  2. var _forEachInstanceProperty = require("@babel/runtime-corejs3/core-js/instance/for-each");
  3. var _Object$defineProperty = require("@babel/runtime-corejs3/core-js/object/define-property");
  4. var _indexOfInstanceProperty = require("@babel/runtime-corejs3/core-js/instance/index-of");
  5. var _spliceInstanceProperty = require("@babel/runtime-corejs3/core-js/instance/splice");
  6. var _Symbol$toStringTag = require("@babel/runtime-corejs3/core-js/symbol/to-string-tag");
  7. var _Object$assign = require("@babel/runtime-corejs3/core-js/object/assign");
  8. var _findInstanceProperty = require("@babel/runtime-corejs3/core-js/instance/find");
  9. var toArray = require("./toArray.js");
  10. var toPropertyKey = require("./toPropertyKey.js");
  11. function _decorate(decorators, factory, superClass, mixins) {
  12. var _context;
  13. var api = _getDecoratorsApi();
  14. if (mixins) {
  15. for (var i = 0; i < mixins.length; i++) {
  16. api = mixins[i](api);
  17. }
  18. }
  19. var r = factory(function initialize(O) {
  20. api.initializeInstanceElements(O, decorated.elements);
  21. }, superClass);
  22. var decorated = api.decorateClass(_coalesceClassElements(_mapInstanceProperty(_context = r.d).call(_context, _createElementDescriptor)), decorators);
  23. api.initializeClassElements(r.F, decorated.elements);
  24. return api.runClassFinishers(r.F, decorated.finishers);
  25. }
  26. function _getDecoratorsApi() {
  27. _getDecoratorsApi = function _getDecoratorsApi() {
  28. return api;
  29. };
  30. var api = {
  31. elementsDefinitionOrder: [["method"], ["field"]],
  32. initializeInstanceElements: function initializeInstanceElements(O, elements) {
  33. var _context2;
  34. _forEachInstanceProperty(_context2 = ["method", "field"]).call(_context2, function (kind) {
  35. _forEachInstanceProperty(elements).call(elements, function (element) {
  36. if (element.kind === kind && element.placement === "own") {
  37. this.defineClassElement(O, element);
  38. }
  39. }, this);
  40. }, this);
  41. },
  42. initializeClassElements: function initializeClassElements(F, elements) {
  43. var _context3;
  44. var proto = F.prototype;
  45. _forEachInstanceProperty(_context3 = ["method", "field"]).call(_context3, function (kind) {
  46. _forEachInstanceProperty(elements).call(elements, function (element) {
  47. var placement = element.placement;
  48. if (element.kind === kind && (placement === "static" || placement === "prototype")) {
  49. var receiver = placement === "static" ? F : proto;
  50. this.defineClassElement(receiver, element);
  51. }
  52. }, this);
  53. }, this);
  54. },
  55. defineClassElement: function defineClassElement(receiver, element) {
  56. var descriptor = element.descriptor;
  57. if (element.kind === "field") {
  58. var initializer = element.initializer;
  59. descriptor = {
  60. enumerable: descriptor.enumerable,
  61. writable: descriptor.writable,
  62. configurable: descriptor.configurable,
  63. value: initializer === void 0 ? void 0 : initializer.call(receiver)
  64. };
  65. }
  66. _Object$defineProperty(receiver, element.key, descriptor);
  67. },
  68. decorateClass: function decorateClass(elements, decorators) {
  69. var newElements = [];
  70. var finishers = [];
  71. var placements = {
  72. "static": [],
  73. prototype: [],
  74. own: []
  75. };
  76. _forEachInstanceProperty(elements).call(elements, function (element) {
  77. this.addElementPlacement(element, placements);
  78. }, this);
  79. _forEachInstanceProperty(elements).call(elements, function (element) {
  80. if (!_hasDecorators(element)) return newElements.push(element);
  81. var elementFinishersExtras = this.decorateElement(element, placements);
  82. newElements.push(elementFinishersExtras.element);
  83. newElements.push.apply(newElements, elementFinishersExtras.extras);
  84. finishers.push.apply(finishers, elementFinishersExtras.finishers);
  85. }, this);
  86. if (!decorators) {
  87. return {
  88. elements: newElements,
  89. finishers: finishers
  90. };
  91. }
  92. var result = this.decorateConstructor(newElements, decorators);
  93. finishers.push.apply(finishers, result.finishers);
  94. result.finishers = finishers;
  95. return result;
  96. },
  97. addElementPlacement: function addElementPlacement(element, placements, silent) {
  98. var keys = placements[element.placement];
  99. if (!silent && _indexOfInstanceProperty(keys).call(keys, element.key) !== -1) {
  100. throw new TypeError("Duplicated element (" + element.key + ")");
  101. }
  102. keys.push(element.key);
  103. },
  104. decorateElement: function decorateElement(element, placements) {
  105. var extras = [];
  106. var finishers = [];
  107. for (var decorators = element.decorators, i = decorators.length - 1; i >= 0; i--) {
  108. var keys = placements[element.placement];
  109. _spliceInstanceProperty(keys).call(keys, _indexOfInstanceProperty(keys).call(keys, element.key), 1);
  110. var elementObject = this.fromElementDescriptor(element);
  111. var elementFinisherExtras = this.toElementFinisherExtras((0, decorators[i])(elementObject) || elementObject);
  112. element = elementFinisherExtras.element;
  113. this.addElementPlacement(element, placements);
  114. if (elementFinisherExtras.finisher) {
  115. finishers.push(elementFinisherExtras.finisher);
  116. }
  117. var newExtras = elementFinisherExtras.extras;
  118. if (newExtras) {
  119. for (var j = 0; j < newExtras.length; j++) {
  120. this.addElementPlacement(newExtras[j], placements);
  121. }
  122. extras.push.apply(extras, newExtras);
  123. }
  124. }
  125. return {
  126. element: element,
  127. finishers: finishers,
  128. extras: extras
  129. };
  130. },
  131. decorateConstructor: function decorateConstructor(elements, decorators) {
  132. var finishers = [];
  133. for (var i = decorators.length - 1; i >= 0; i--) {
  134. var obj = this.fromClassDescriptor(elements);
  135. var elementsAndFinisher = this.toClassDescriptor((0, decorators[i])(obj) || obj);
  136. if (elementsAndFinisher.finisher !== undefined) {
  137. finishers.push(elementsAndFinisher.finisher);
  138. }
  139. if (elementsAndFinisher.elements !== undefined) {
  140. elements = elementsAndFinisher.elements;
  141. for (var j = 0; j < elements.length - 1; j++) {
  142. for (var k = j + 1; k < elements.length; k++) {
  143. if (elements[j].key === elements[k].key && elements[j].placement === elements[k].placement) {
  144. throw new TypeError("Duplicated element (" + elements[j].key + ")");
  145. }
  146. }
  147. }
  148. }
  149. }
  150. return {
  151. elements: elements,
  152. finishers: finishers
  153. };
  154. },
  155. fromElementDescriptor: function fromElementDescriptor(element) {
  156. var obj = {
  157. kind: element.kind,
  158. key: element.key,
  159. placement: element.placement,
  160. descriptor: element.descriptor
  161. };
  162. var desc = {
  163. value: "Descriptor",
  164. configurable: true
  165. };
  166. _Object$defineProperty(obj, _Symbol$toStringTag, desc);
  167. if (element.kind === "field") obj.initializer = element.initializer;
  168. return obj;
  169. },
  170. toElementDescriptors: function toElementDescriptors(elementObjects) {
  171. var _context4;
  172. if (elementObjects === undefined) return;
  173. return _mapInstanceProperty(_context4 = toArray(elementObjects)).call(_context4, function (elementObject) {
  174. var element = this.toElementDescriptor(elementObject);
  175. this.disallowProperty(elementObject, "finisher", "An element descriptor");
  176. this.disallowProperty(elementObject, "extras", "An element descriptor");
  177. return element;
  178. }, this);
  179. },
  180. toElementDescriptor: function toElementDescriptor(elementObject) {
  181. var kind = String(elementObject.kind);
  182. if (kind !== "method" && kind !== "field") {
  183. throw new TypeError('An element descriptor\'s .kind property must be either "method" or' + ' "field", but a decorator created an element descriptor with' + ' .kind "' + kind + '"');
  184. }
  185. var key = toPropertyKey(elementObject.key);
  186. var placement = String(elementObject.placement);
  187. if (placement !== "static" && placement !== "prototype" && placement !== "own") {
  188. 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 "' + placement + '"');
  189. }
  190. var descriptor = elementObject.descriptor;
  191. this.disallowProperty(elementObject, "elements", "An element descriptor");
  192. var element = {
  193. kind: kind,
  194. key: key,
  195. placement: placement,
  196. descriptor: _Object$assign({}, descriptor)
  197. };
  198. if (kind !== "field") {
  199. this.disallowProperty(elementObject, "initializer", "A method descriptor");
  200. } else {
  201. this.disallowProperty(descriptor, "get", "The property descriptor of a field descriptor");
  202. this.disallowProperty(descriptor, "set", "The property descriptor of a field descriptor");
  203. this.disallowProperty(descriptor, "value", "The property descriptor of a field descriptor");
  204. element.initializer = elementObject.initializer;
  205. }
  206. return element;
  207. },
  208. toElementFinisherExtras: function toElementFinisherExtras(elementObject) {
  209. var element = this.toElementDescriptor(elementObject);
  210. var finisher = _optionalCallableProperty(elementObject, "finisher");
  211. var extras = this.toElementDescriptors(elementObject.extras);
  212. return {
  213. element: element,
  214. finisher: finisher,
  215. extras: extras
  216. };
  217. },
  218. fromClassDescriptor: function fromClassDescriptor(elements) {
  219. var obj = {
  220. kind: "class",
  221. elements: _mapInstanceProperty(elements).call(elements, this.fromElementDescriptor, this)
  222. };
  223. var desc = {
  224. value: "Descriptor",
  225. configurable: true
  226. };
  227. _Object$defineProperty(obj, _Symbol$toStringTag, desc);
  228. return obj;
  229. },
  230. toClassDescriptor: function toClassDescriptor(obj) {
  231. var kind = String(obj.kind);
  232. if (kind !== "class") {
  233. throw new TypeError('A class descriptor\'s .kind property must be "class", but a decorator' + ' created a class descriptor with .kind "' + kind + '"');
  234. }
  235. this.disallowProperty(obj, "key", "A class descriptor");
  236. this.disallowProperty(obj, "placement", "A class descriptor");
  237. this.disallowProperty(obj, "descriptor", "A class descriptor");
  238. this.disallowProperty(obj, "initializer", "A class descriptor");
  239. this.disallowProperty(obj, "extras", "A class descriptor");
  240. var finisher = _optionalCallableProperty(obj, "finisher");
  241. var elements = this.toElementDescriptors(obj.elements);
  242. return {
  243. elements: elements,
  244. finisher: finisher
  245. };
  246. },
  247. runClassFinishers: function runClassFinishers(constructor, finishers) {
  248. for (var i = 0; i < finishers.length; i++) {
  249. var newConstructor = (0, finishers[i])(constructor);
  250. if (newConstructor !== undefined) {
  251. if (typeof newConstructor !== "function") {
  252. throw new TypeError("Finishers must return a constructor.");
  253. }
  254. constructor = newConstructor;
  255. }
  256. }
  257. return constructor;
  258. },
  259. disallowProperty: function disallowProperty(obj, name, objectType) {
  260. if (obj[name] !== undefined) {
  261. throw new TypeError(objectType + " can't have a ." + name + " property.");
  262. }
  263. }
  264. };
  265. return api;
  266. }
  267. function _createElementDescriptor(def) {
  268. var key = toPropertyKey(def.key);
  269. var descriptor;
  270. if (def.kind === "method") {
  271. descriptor = {
  272. value: def.value,
  273. writable: true,
  274. configurable: true,
  275. enumerable: false
  276. };
  277. } else if (def.kind === "get") {
  278. descriptor = {
  279. get: def.value,
  280. configurable: true,
  281. enumerable: false
  282. };
  283. } else if (def.kind === "set") {
  284. descriptor = {
  285. set: def.value,
  286. configurable: true,
  287. enumerable: false
  288. };
  289. } else if (def.kind === "field") {
  290. descriptor = {
  291. configurable: true,
  292. writable: true,
  293. enumerable: true
  294. };
  295. }
  296. var element = {
  297. kind: def.kind === "field" ? "field" : "method",
  298. key: key,
  299. placement: def["static"] ? "static" : def.kind === "field" ? "own" : "prototype",
  300. descriptor: descriptor
  301. };
  302. if (def.decorators) element.decorators = def.decorators;
  303. if (def.kind === "field") element.initializer = def.value;
  304. return element;
  305. }
  306. function _coalesceGetterSetter(element, other) {
  307. if (element.descriptor.get !== undefined) {
  308. other.descriptor.get = element.descriptor.get;
  309. } else {
  310. other.descriptor.set = element.descriptor.set;
  311. }
  312. }
  313. function _coalesceClassElements(elements) {
  314. var newElements = [];
  315. var isSameElement = function isSameElement(other) {
  316. return other.kind === "method" && other.key === element.key && other.placement === element.placement;
  317. };
  318. for (var i = 0; i < elements.length; i++) {
  319. var element = elements[i];
  320. var other;
  321. if (element.kind === "method" && (other = _findInstanceProperty(newElements).call(newElements, isSameElement))) {
  322. if (_isDataDescriptor(element.descriptor) || _isDataDescriptor(other.descriptor)) {
  323. if (_hasDecorators(element) || _hasDecorators(other)) {
  324. throw new ReferenceError("Duplicated methods (" + element.key + ") can't be decorated.");
  325. }
  326. other.descriptor = element.descriptor;
  327. } else {
  328. if (_hasDecorators(element)) {
  329. if (_hasDecorators(other)) {
  330. throw new ReferenceError("Decorators can't be placed on different accessors with for " + "the same property (" + element.key + ").");
  331. }
  332. other.decorators = element.decorators;
  333. }
  334. _coalesceGetterSetter(element, other);
  335. }
  336. } else {
  337. newElements.push(element);
  338. }
  339. }
  340. return newElements;
  341. }
  342. function _hasDecorators(element) {
  343. return element.decorators && element.decorators.length;
  344. }
  345. function _isDataDescriptor(desc) {
  346. return desc !== undefined && !(desc.value === undefined && desc.writable === undefined);
  347. }
  348. function _optionalCallableProperty(obj, name) {
  349. var value = obj[name];
  350. if (value !== undefined && typeof value !== "function") {
  351. throw new TypeError("Expected '" + name + "' to be a function");
  352. }
  353. return value;
  354. }
  355. module.exports = _decorate;
  356. module.exports["default"] = module.exports, module.exports.__esModule = true;