applyDecs.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = applyDecs;
  6. var _setFunctionName = require("setFunctionName");
  7. var _toPropertyKey = require("toPropertyKey");
  8. function old_createMetadataMethodsForProperty(metadataMap, kind, property, decoratorFinishedRef) {
  9. return {
  10. getMetadata: function (key) {
  11. old_assertNotFinished(decoratorFinishedRef, "getMetadata");
  12. old_assertMetadataKey(key);
  13. var metadataForKey = metadataMap[key];
  14. if (metadataForKey === void 0) return void 0;
  15. if (kind === 1) {
  16. var pub = metadataForKey.public;
  17. if (pub !== void 0) {
  18. return pub[property];
  19. }
  20. } else if (kind === 2) {
  21. var priv = metadataForKey.private;
  22. if (priv !== void 0) {
  23. return priv.get(property);
  24. }
  25. } else if (Object.hasOwnProperty.call(metadataForKey, "constructor")) {
  26. return metadataForKey.constructor;
  27. }
  28. },
  29. setMetadata: function (key, value) {
  30. old_assertNotFinished(decoratorFinishedRef, "setMetadata");
  31. old_assertMetadataKey(key);
  32. var metadataForKey = metadataMap[key];
  33. if (metadataForKey === void 0) {
  34. metadataForKey = metadataMap[key] = {};
  35. }
  36. if (kind === 1) {
  37. var pub = metadataForKey.public;
  38. if (pub === void 0) {
  39. pub = metadataForKey.public = {};
  40. }
  41. pub[property] = value;
  42. } else if (kind === 2) {
  43. var priv = metadataForKey.priv;
  44. if (priv === void 0) {
  45. priv = metadataForKey.private = new Map();
  46. }
  47. priv.set(property, value);
  48. } else {
  49. metadataForKey.constructor = value;
  50. }
  51. }
  52. };
  53. }
  54. function old_convertMetadataMapToFinal(obj, metadataMap) {
  55. var parentMetadataMap = obj[Symbol.metadata || Symbol.for("Symbol.metadata")];
  56. var metadataKeys = Object.getOwnPropertySymbols(metadataMap);
  57. if (metadataKeys.length === 0) return;
  58. for (var i = 0; i < metadataKeys.length; i++) {
  59. var key = metadataKeys[i];
  60. var metaForKey = metadataMap[key];
  61. var parentMetaForKey = parentMetadataMap ? parentMetadataMap[key] : null;
  62. var pub = metaForKey.public;
  63. var parentPub = parentMetaForKey ? parentMetaForKey.public : null;
  64. if (pub && parentPub) {
  65. Object.setPrototypeOf(pub, parentPub);
  66. }
  67. var priv = metaForKey.private;
  68. if (priv) {
  69. var privArr = Array.from(priv.values());
  70. var parentPriv = parentMetaForKey ? parentMetaForKey.private : null;
  71. if (parentPriv) {
  72. privArr = privArr.concat(parentPriv);
  73. }
  74. metaForKey.private = privArr;
  75. }
  76. if (parentMetaForKey) {
  77. Object.setPrototypeOf(metaForKey, parentMetaForKey);
  78. }
  79. }
  80. if (parentMetadataMap) {
  81. Object.setPrototypeOf(metadataMap, parentMetadataMap);
  82. }
  83. obj[Symbol.metadata || Symbol.for("Symbol.metadata")] = metadataMap;
  84. }
  85. function old_createAddInitializerMethod(initializers, decoratorFinishedRef) {
  86. return function addInitializer(initializer) {
  87. old_assertNotFinished(decoratorFinishedRef, "addInitializer");
  88. old_assertCallable(initializer, "An initializer");
  89. initializers.push(initializer);
  90. };
  91. }
  92. function old_memberDec(dec, name, desc, metadataMap, initializers, kind, isStatic, isPrivate, value) {
  93. var kindStr;
  94. switch (kind) {
  95. case 1:
  96. kindStr = "accessor";
  97. break;
  98. case 2:
  99. kindStr = "method";
  100. break;
  101. case 3:
  102. kindStr = "getter";
  103. break;
  104. case 4:
  105. kindStr = "setter";
  106. break;
  107. default:
  108. kindStr = "field";
  109. }
  110. var ctx = {
  111. kind: kindStr,
  112. name: isPrivate ? "#" + name : _toPropertyKey(name),
  113. isStatic: isStatic,
  114. isPrivate: isPrivate
  115. };
  116. var decoratorFinishedRef = {
  117. v: false
  118. };
  119. if (kind !== 0) {
  120. ctx.addInitializer = old_createAddInitializerMethod(initializers, decoratorFinishedRef);
  121. }
  122. var metadataKind, metadataName;
  123. if (isPrivate) {
  124. metadataKind = 2;
  125. metadataName = Symbol(name);
  126. var access = {};
  127. if (kind === 0) {
  128. access.get = desc.get;
  129. access.set = desc.set;
  130. } else if (kind === 2) {
  131. access.get = function () {
  132. return desc.value;
  133. };
  134. } else {
  135. if (kind === 1 || kind === 3) {
  136. access.get = function () {
  137. return desc.get.call(this);
  138. };
  139. }
  140. if (kind === 1 || kind === 4) {
  141. access.set = function (v) {
  142. desc.set.call(this, v);
  143. };
  144. }
  145. }
  146. ctx.access = access;
  147. } else {
  148. metadataKind = 1;
  149. metadataName = name;
  150. }
  151. try {
  152. return dec(value, Object.assign(ctx, old_createMetadataMethodsForProperty(metadataMap, metadataKind, metadataName, decoratorFinishedRef)));
  153. } finally {
  154. decoratorFinishedRef.v = true;
  155. }
  156. }
  157. function old_assertNotFinished(decoratorFinishedRef, fnName) {
  158. if (decoratorFinishedRef.v) {
  159. throw new Error("attempted to call " + fnName + " after decoration was finished");
  160. }
  161. }
  162. function old_assertMetadataKey(key) {
  163. if (typeof key !== "symbol") {
  164. throw new TypeError("Metadata keys must be symbols, received: " + key);
  165. }
  166. }
  167. function old_assertCallable(fn, hint) {
  168. if (typeof fn !== "function") {
  169. throw new TypeError(hint + " must be a function");
  170. }
  171. }
  172. function old_assertValidReturnValue(kind, value) {
  173. var type = typeof value;
  174. if (kind === 1) {
  175. if (type !== "object" || value === null) {
  176. throw new TypeError("accessor decorators must return an object with get, set, or init properties or void 0");
  177. }
  178. if (value.get !== undefined) {
  179. old_assertCallable(value.get, "accessor.get");
  180. }
  181. if (value.set !== undefined) {
  182. old_assertCallable(value.set, "accessor.set");
  183. }
  184. if (value.init !== undefined) {
  185. old_assertCallable(value.init, "accessor.init");
  186. }
  187. if (value.initializer !== undefined) {
  188. old_assertCallable(value.initializer, "accessor.initializer");
  189. }
  190. } else if (type !== "function") {
  191. var hint;
  192. if (kind === 0) {
  193. hint = "field";
  194. } else if (kind === 10) {
  195. hint = "class";
  196. } else {
  197. hint = "method";
  198. }
  199. throw new TypeError(hint + " decorators must return a function or void 0");
  200. }
  201. }
  202. function old_getInit(desc) {
  203. var initializer;
  204. if ((initializer = desc.init) == null && (initializer = desc.initializer) && typeof console !== "undefined") {
  205. console.warn(".initializer has been renamed to .init as of March 2022");
  206. }
  207. return initializer;
  208. }
  209. function old_applyMemberDec(ret, base, decInfo, name, kind, isStatic, isPrivate, metadataMap, initializers) {
  210. var decs = decInfo[0];
  211. var desc, initializer, prefix, value;
  212. if (isPrivate) {
  213. if (kind === 0 || kind === 1) {
  214. desc = {
  215. get: decInfo[3],
  216. set: decInfo[4]
  217. };
  218. prefix = "get";
  219. } else if (kind === 3) {
  220. desc = {
  221. get: decInfo[3]
  222. };
  223. prefix = "get";
  224. } else if (kind === 4) {
  225. desc = {
  226. set: decInfo[3]
  227. };
  228. prefix = "set";
  229. } else {
  230. desc = {
  231. value: decInfo[3]
  232. };
  233. }
  234. if (kind !== 0) {
  235. if (kind === 1) {
  236. _setFunctionName(decInfo[4], "#" + name, "set");
  237. }
  238. _setFunctionName(decInfo[3], "#" + name, prefix);
  239. }
  240. } else if (kind !== 0) {
  241. desc = Object.getOwnPropertyDescriptor(base, name);
  242. }
  243. if (kind === 1) {
  244. value = {
  245. get: desc.get,
  246. set: desc.set
  247. };
  248. } else if (kind === 2) {
  249. value = desc.value;
  250. } else if (kind === 3) {
  251. value = desc.get;
  252. } else if (kind === 4) {
  253. value = desc.set;
  254. }
  255. var newValue, get, set;
  256. if (typeof decs === "function") {
  257. newValue = old_memberDec(decs, name, desc, metadataMap, initializers, kind, isStatic, isPrivate, value);
  258. if (newValue !== void 0) {
  259. old_assertValidReturnValue(kind, newValue);
  260. if (kind === 0) {
  261. initializer = newValue;
  262. } else if (kind === 1) {
  263. initializer = old_getInit(newValue);
  264. get = newValue.get || value.get;
  265. set = newValue.set || value.set;
  266. value = {
  267. get: get,
  268. set: set
  269. };
  270. } else {
  271. value = newValue;
  272. }
  273. }
  274. } else {
  275. for (var i = decs.length - 1; i >= 0; i--) {
  276. var dec = decs[i];
  277. newValue = old_memberDec(dec, name, desc, metadataMap, initializers, kind, isStatic, isPrivate, value);
  278. if (newValue !== void 0) {
  279. old_assertValidReturnValue(kind, newValue);
  280. var newInit;
  281. if (kind === 0) {
  282. newInit = newValue;
  283. } else if (kind === 1) {
  284. newInit = old_getInit(newValue);
  285. get = newValue.get || value.get;
  286. set = newValue.set || value.set;
  287. value = {
  288. get: get,
  289. set: set
  290. };
  291. } else {
  292. value = newValue;
  293. }
  294. if (newInit !== void 0) {
  295. if (initializer === void 0) {
  296. initializer = newInit;
  297. } else if (typeof initializer === "function") {
  298. initializer = [initializer, newInit];
  299. } else {
  300. initializer.push(newInit);
  301. }
  302. }
  303. }
  304. }
  305. }
  306. if (kind === 0 || kind === 1) {
  307. if (initializer === void 0) {
  308. initializer = function (instance, init) {
  309. return init;
  310. };
  311. } else if (typeof initializer !== "function") {
  312. var ownInitializers = initializer;
  313. initializer = function (instance, init) {
  314. var value = init;
  315. for (var i = 0; i < ownInitializers.length; i++) {
  316. value = ownInitializers[i].call(instance, value);
  317. }
  318. return value;
  319. };
  320. } else {
  321. var originalInitializer = initializer;
  322. initializer = function (instance, init) {
  323. return originalInitializer.call(instance, init);
  324. };
  325. }
  326. ret.push(initializer);
  327. }
  328. if (kind !== 0) {
  329. if (kind === 1) {
  330. desc.get = value.get;
  331. desc.set = value.set;
  332. } else if (kind === 2) {
  333. desc.value = value;
  334. } else if (kind === 3) {
  335. desc.get = value;
  336. } else if (kind === 4) {
  337. desc.set = value;
  338. }
  339. if (isPrivate) {
  340. if (kind === 1) {
  341. ret.push(function (instance, args) {
  342. return value.get.call(instance, args);
  343. });
  344. ret.push(function (instance, args) {
  345. return value.set.call(instance, args);
  346. });
  347. } else if (kind === 2) {
  348. ret.push(value);
  349. } else {
  350. ret.push(function (instance, args) {
  351. return value.call(instance, args);
  352. });
  353. }
  354. } else {
  355. Object.defineProperty(base, name, desc);
  356. }
  357. }
  358. }
  359. function old_applyMemberDecs(ret, Class, protoMetadataMap, staticMetadataMap, decInfos) {
  360. var protoInitializers;
  361. var staticInitializers;
  362. var existingProtoNonFields = new Map();
  363. var existingStaticNonFields = new Map();
  364. for (var i = 0; i < decInfos.length; i++) {
  365. var decInfo = decInfos[i];
  366. if (!Array.isArray(decInfo)) continue;
  367. var kind = decInfo[1];
  368. var name = decInfo[2];
  369. var isPrivate = decInfo.length > 3;
  370. var isStatic = kind >= 5;
  371. var base;
  372. var metadataMap;
  373. var initializers;
  374. if (isStatic) {
  375. base = Class;
  376. metadataMap = staticMetadataMap;
  377. kind = kind - 5;
  378. if (kind !== 0) {
  379. staticInitializers = staticInitializers || [];
  380. initializers = staticInitializers;
  381. }
  382. } else {
  383. base = Class.prototype;
  384. metadataMap = protoMetadataMap;
  385. if (kind !== 0) {
  386. protoInitializers = protoInitializers || [];
  387. initializers = protoInitializers;
  388. }
  389. }
  390. if (kind !== 0 && !isPrivate) {
  391. var existingNonFields = isStatic ? existingStaticNonFields : existingProtoNonFields;
  392. var existingKind = existingNonFields.get(name) || 0;
  393. if (existingKind === true || existingKind === 3 && kind !== 4 || existingKind === 4 && kind !== 3) {
  394. throw new Error("Attempted to decorate a public method/accessor that has the same name as a previously decorated public method/accessor. This is not currently supported by the decorators plugin. Property name was: " + name);
  395. } else if (!existingKind && kind > 2) {
  396. existingNonFields.set(name, kind);
  397. } else {
  398. existingNonFields.set(name, true);
  399. }
  400. }
  401. old_applyMemberDec(ret, base, decInfo, name, kind, isStatic, isPrivate, metadataMap, initializers);
  402. }
  403. old_pushInitializers(ret, protoInitializers);
  404. old_pushInitializers(ret, staticInitializers);
  405. }
  406. function old_pushInitializers(ret, initializers) {
  407. if (initializers) {
  408. ret.push(function (instance) {
  409. for (var i = 0; i < initializers.length; i++) {
  410. initializers[i].call(instance);
  411. }
  412. return instance;
  413. });
  414. }
  415. }
  416. function old_applyClassDecs(ret, targetClass, metadataMap, classDecs) {
  417. if (classDecs.length > 0) {
  418. var initializers = [];
  419. var newClass = targetClass;
  420. var name = targetClass.name;
  421. for (var i = classDecs.length - 1; i >= 0; i--) {
  422. var decoratorFinishedRef = {
  423. v: false
  424. };
  425. try {
  426. var ctx = Object.assign({
  427. kind: "class",
  428. name: name,
  429. addInitializer: old_createAddInitializerMethod(initializers, decoratorFinishedRef)
  430. }, old_createMetadataMethodsForProperty(metadataMap, 0, name, decoratorFinishedRef));
  431. var nextNewClass = classDecs[i](newClass, ctx);
  432. } finally {
  433. decoratorFinishedRef.v = true;
  434. }
  435. if (nextNewClass !== undefined) {
  436. old_assertValidReturnValue(10, nextNewClass);
  437. newClass = nextNewClass;
  438. }
  439. }
  440. ret.push(newClass, function () {
  441. for (var i = 0; i < initializers.length; i++) {
  442. initializers[i].call(newClass);
  443. }
  444. });
  445. }
  446. }
  447. function applyDecs(targetClass, memberDecs, classDecs) {
  448. var ret = [];
  449. var staticMetadataMap = {};
  450. var protoMetadataMap = {};
  451. old_applyMemberDecs(ret, targetClass, protoMetadataMap, staticMetadataMap, memberDecs);
  452. old_convertMetadataMapToFinal(targetClass.prototype, protoMetadataMap);
  453. old_applyClassDecs(ret, targetClass, staticMetadataMap, classDecs);
  454. old_convertMetadataMapToFinal(targetClass, staticMetadataMap);
  455. return ret;
  456. }
  457. //# sourceMappingURL=applyDecs.js.map