applyDecs.js 13 KB

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