decorate.js 13 KB

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