applyDecs2203.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = applyDecs2203;
  6. function applyDecs2203Factory() {
  7. function createAddInitializerMethod(initializers, decoratorFinishedRef) {
  8. return function addInitializer(initializer) {
  9. assertNotFinished(decoratorFinishedRef, "addInitializer");
  10. assertCallable(initializer, "An initializer");
  11. initializers.push(initializer);
  12. };
  13. }
  14. function memberDec(dec, name, desc, initializers, kind, isStatic, isPrivate, value) {
  15. var kindStr;
  16. switch (kind) {
  17. case 1:
  18. kindStr = "accessor";
  19. break;
  20. case 2:
  21. kindStr = "method";
  22. break;
  23. case 3:
  24. kindStr = "getter";
  25. break;
  26. case 4:
  27. kindStr = "setter";
  28. break;
  29. default:
  30. kindStr = "field";
  31. }
  32. var ctx = {
  33. kind: kindStr,
  34. name: isPrivate ? "#" + name : name,
  35. static: isStatic,
  36. private: isPrivate
  37. };
  38. var decoratorFinishedRef = {
  39. v: false
  40. };
  41. if (kind !== 0) {
  42. ctx.addInitializer = createAddInitializerMethod(initializers, decoratorFinishedRef);
  43. }
  44. var get, set;
  45. if (kind === 0) {
  46. if (isPrivate) {
  47. get = desc.get;
  48. set = desc.set;
  49. } else {
  50. get = function () {
  51. return this[name];
  52. };
  53. set = function (v) {
  54. this[name] = v;
  55. };
  56. }
  57. } else if (kind === 2) {
  58. get = function () {
  59. return desc.value;
  60. };
  61. } else {
  62. if (kind === 1 || kind === 3) {
  63. get = function () {
  64. return desc.get.call(this);
  65. };
  66. }
  67. if (kind === 1 || kind === 4) {
  68. set = function (v) {
  69. desc.set.call(this, v);
  70. };
  71. }
  72. }
  73. ctx.access = get && set ? {
  74. get: get,
  75. set: set
  76. } : get ? {
  77. get: get
  78. } : {
  79. set: set
  80. };
  81. try {
  82. return dec(value, ctx);
  83. } finally {
  84. decoratorFinishedRef.v = true;
  85. }
  86. }
  87. function assertNotFinished(decoratorFinishedRef, fnName) {
  88. if (decoratorFinishedRef.v) {
  89. throw new Error("attempted to call " + fnName + " after decoration was finished");
  90. }
  91. }
  92. function assertCallable(fn, hint) {
  93. if (typeof fn !== "function") {
  94. throw new TypeError(hint + " must be a function");
  95. }
  96. }
  97. function assertValidReturnValue(kind, value) {
  98. var type = typeof value;
  99. if (kind === 1) {
  100. if (type !== "object" || value === null) {
  101. throw new TypeError("accessor decorators must return an object with get, set, or init properties or void 0");
  102. }
  103. if (value.get !== undefined) {
  104. assertCallable(value.get, "accessor.get");
  105. }
  106. if (value.set !== undefined) {
  107. assertCallable(value.set, "accessor.set");
  108. }
  109. if (value.init !== undefined) {
  110. assertCallable(value.init, "accessor.init");
  111. }
  112. } else if (type !== "function") {
  113. var hint;
  114. if (kind === 0) {
  115. hint = "field";
  116. } else if (kind === 10) {
  117. hint = "class";
  118. } else {
  119. hint = "method";
  120. }
  121. throw new TypeError(hint + " decorators must return a function or void 0");
  122. }
  123. }
  124. function applyMemberDec(ret, base, decInfo, name, kind, isStatic, isPrivate, initializers) {
  125. var decs = decInfo[0];
  126. var desc, init, value;
  127. if (isPrivate) {
  128. if (kind === 0 || kind === 1) {
  129. desc = {
  130. get: decInfo[3],
  131. set: decInfo[4]
  132. };
  133. } else if (kind === 3) {
  134. desc = {
  135. get: decInfo[3]
  136. };
  137. } else if (kind === 4) {
  138. desc = {
  139. set: decInfo[3]
  140. };
  141. } else {
  142. desc = {
  143. value: decInfo[3]
  144. };
  145. }
  146. } else if (kind !== 0) {
  147. desc = Object.getOwnPropertyDescriptor(base, name);
  148. }
  149. if (kind === 1) {
  150. value = {
  151. get: desc.get,
  152. set: desc.set
  153. };
  154. } else if (kind === 2) {
  155. value = desc.value;
  156. } else if (kind === 3) {
  157. value = desc.get;
  158. } else if (kind === 4) {
  159. value = desc.set;
  160. }
  161. var newValue, get, set;
  162. if (typeof decs === "function") {
  163. newValue = memberDec(decs, name, desc, initializers, kind, isStatic, isPrivate, value);
  164. if (newValue !== void 0) {
  165. assertValidReturnValue(kind, newValue);
  166. if (kind === 0) {
  167. init = newValue;
  168. } else if (kind === 1) {
  169. init = newValue.init;
  170. get = newValue.get || value.get;
  171. set = newValue.set || value.set;
  172. value = {
  173. get: get,
  174. set: set
  175. };
  176. } else {
  177. value = newValue;
  178. }
  179. }
  180. } else {
  181. for (var i = decs.length - 1; i >= 0; i--) {
  182. var dec = decs[i];
  183. newValue = memberDec(dec, name, desc, initializers, kind, isStatic, isPrivate, value);
  184. if (newValue !== void 0) {
  185. assertValidReturnValue(kind, newValue);
  186. var newInit;
  187. if (kind === 0) {
  188. newInit = newValue;
  189. } else if (kind === 1) {
  190. newInit = newValue.init;
  191. get = newValue.get || value.get;
  192. set = newValue.set || value.set;
  193. value = {
  194. get: get,
  195. set: set
  196. };
  197. } else {
  198. value = newValue;
  199. }
  200. if (newInit !== void 0) {
  201. if (init === void 0) {
  202. init = newInit;
  203. } else if (typeof init === "function") {
  204. init = [init, newInit];
  205. } else {
  206. init.push(newInit);
  207. }
  208. }
  209. }
  210. }
  211. }
  212. if (kind === 0 || kind === 1) {
  213. if (init === void 0) {
  214. init = function (instance, init) {
  215. return init;
  216. };
  217. } else if (typeof init !== "function") {
  218. var ownInitializers = init;
  219. init = function (instance, init) {
  220. var value = init;
  221. for (var i = 0; i < ownInitializers.length; i++) {
  222. value = ownInitializers[i].call(instance, value);
  223. }
  224. return value;
  225. };
  226. } else {
  227. var originalInitializer = init;
  228. init = function (instance, init) {
  229. return originalInitializer.call(instance, init);
  230. };
  231. }
  232. ret.push(init);
  233. }
  234. if (kind !== 0) {
  235. if (kind === 1) {
  236. desc.get = value.get;
  237. desc.set = value.set;
  238. } else if (kind === 2) {
  239. desc.value = value;
  240. } else if (kind === 3) {
  241. desc.get = value;
  242. } else if (kind === 4) {
  243. desc.set = value;
  244. }
  245. if (isPrivate) {
  246. if (kind === 1) {
  247. ret.push(function (instance, args) {
  248. return value.get.call(instance, args);
  249. });
  250. ret.push(function (instance, args) {
  251. return value.set.call(instance, args);
  252. });
  253. } else if (kind === 2) {
  254. ret.push(value);
  255. } else {
  256. ret.push(function (instance, args) {
  257. return value.call(instance, args);
  258. });
  259. }
  260. } else {
  261. Object.defineProperty(base, name, desc);
  262. }
  263. }
  264. }
  265. function applyMemberDecs(ret, Class, decInfos) {
  266. var protoInitializers;
  267. var staticInitializers;
  268. var existingProtoNonFields = new Map();
  269. var existingStaticNonFields = new Map();
  270. for (var i = 0; i < decInfos.length; i++) {
  271. var decInfo = decInfos[i];
  272. if (!Array.isArray(decInfo)) continue;
  273. var kind = decInfo[1];
  274. var name = decInfo[2];
  275. var isPrivate = decInfo.length > 3;
  276. var isStatic = kind >= 5;
  277. var base;
  278. var initializers;
  279. if (isStatic) {
  280. base = Class;
  281. kind = kind - 5;
  282. if (kind !== 0) {
  283. staticInitializers = staticInitializers || [];
  284. initializers = staticInitializers;
  285. }
  286. } else {
  287. base = Class.prototype;
  288. if (kind !== 0) {
  289. protoInitializers = protoInitializers || [];
  290. initializers = protoInitializers;
  291. }
  292. }
  293. if (kind !== 0 && !isPrivate) {
  294. var existingNonFields = isStatic ? existingStaticNonFields : existingProtoNonFields;
  295. var existingKind = existingNonFields.get(name) || 0;
  296. if (existingKind === true || existingKind === 3 && kind !== 4 || existingKind === 4 && kind !== 3) {
  297. 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);
  298. } else if (!existingKind && kind > 2) {
  299. existingNonFields.set(name, kind);
  300. } else {
  301. existingNonFields.set(name, true);
  302. }
  303. }
  304. applyMemberDec(ret, base, decInfo, name, kind, isStatic, isPrivate, initializers);
  305. }
  306. pushInitializers(ret, protoInitializers);
  307. pushInitializers(ret, staticInitializers);
  308. }
  309. function pushInitializers(ret, initializers) {
  310. if (initializers) {
  311. ret.push(function (instance) {
  312. for (var i = 0; i < initializers.length; i++) {
  313. initializers[i].call(instance);
  314. }
  315. return instance;
  316. });
  317. }
  318. }
  319. function applyClassDecs(ret, targetClass, classDecs) {
  320. if (classDecs.length > 0) {
  321. var initializers = [];
  322. var newClass = targetClass;
  323. var name = targetClass.name;
  324. for (var i = classDecs.length - 1; i >= 0; i--) {
  325. var decoratorFinishedRef = {
  326. v: false
  327. };
  328. try {
  329. var nextNewClass = classDecs[i](newClass, {
  330. kind: "class",
  331. name: name,
  332. addInitializer: createAddInitializerMethod(initializers, decoratorFinishedRef)
  333. });
  334. } finally {
  335. decoratorFinishedRef.v = true;
  336. }
  337. if (nextNewClass !== undefined) {
  338. assertValidReturnValue(10, nextNewClass);
  339. newClass = nextNewClass;
  340. }
  341. }
  342. ret.push(newClass, function () {
  343. for (var i = 0; i < initializers.length; i++) {
  344. initializers[i].call(newClass);
  345. }
  346. });
  347. }
  348. }
  349. return function applyDecs2203Impl(targetClass, memberDecs, classDecs) {
  350. var ret = [];
  351. applyMemberDecs(ret, targetClass, memberDecs);
  352. applyClassDecs(ret, targetClass, classDecs);
  353. return ret;
  354. };
  355. }
  356. var applyDecs2203Impl;
  357. function applyDecs2203(targetClass, memberDecs, classDecs) {
  358. applyDecs2203Impl = applyDecs2203Impl || applyDecs2203Factory();
  359. return applyDecs2203Impl(targetClass, memberDecs, classDecs);
  360. }
  361. //# sourceMappingURL=applyDecs2203.js.map