734d9f975572d262c2ac06ebaf67c4fdb2c13c54b3b48df26196fe05239e533142f51849f66e8612e360ff50ec09c54fdd9db86936425aa75f6c322e21bcb5 11 KB

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