_decorate.js 13 KB

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