decorators.js 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = _default;
  6. exports.hasDecorators = hasDecorators;
  7. exports.hasOwnDecorators = hasOwnDecorators;
  8. var _core = require("@babel/core");
  9. var _helperReplaceSupers = require("@babel/helper-replace-supers");
  10. var _helperSkipTransparentExpressionWrappers = require("@babel/helper-skip-transparent-expression-wrappers");
  11. var _fields = require("./fields.js");
  12. var _misc = require("./misc.js");
  13. function hasOwnDecorators(node) {
  14. var _node$decorators;
  15. return !!((_node$decorators = node.decorators) != null && _node$decorators.length);
  16. }
  17. function hasDecorators(node) {
  18. return hasOwnDecorators(node) || node.body.body.some(hasOwnDecorators);
  19. }
  20. function incrementId(id, idx = id.length - 1) {
  21. if (idx === -1) {
  22. id.unshift(65);
  23. return;
  24. }
  25. const current = id[idx];
  26. if (current === 90) {
  27. id[idx] = 97;
  28. } else if (current === 122) {
  29. id[idx] = 65;
  30. incrementId(id, idx - 1);
  31. } else {
  32. id[idx] = current + 1;
  33. }
  34. }
  35. function createPrivateUidGeneratorForClass(classPath) {
  36. const currentPrivateId = [];
  37. const privateNames = new Set();
  38. classPath.traverse({
  39. PrivateName(path) {
  40. privateNames.add(path.node.id.name);
  41. }
  42. });
  43. return () => {
  44. let reifiedId;
  45. do {
  46. incrementId(currentPrivateId);
  47. reifiedId = String.fromCharCode(...currentPrivateId);
  48. } while (privateNames.has(reifiedId));
  49. return _core.types.privateName(_core.types.identifier(reifiedId));
  50. };
  51. }
  52. function createLazyPrivateUidGeneratorForClass(classPath) {
  53. let generator;
  54. return () => {
  55. if (!generator) {
  56. generator = createPrivateUidGeneratorForClass(classPath);
  57. }
  58. return generator();
  59. };
  60. }
  61. function replaceClassWithVar(path, className) {
  62. const id = path.node.id;
  63. const scope = path.scope;
  64. if (path.type === "ClassDeclaration") {
  65. const className = id.name;
  66. const varId = scope.generateUidIdentifierBasedOnNode(id);
  67. const classId = _core.types.identifier(className);
  68. scope.rename(className, varId.name);
  69. path.get("id").replaceWith(classId);
  70. return {
  71. id: _core.types.cloneNode(varId),
  72. path
  73. };
  74. } else {
  75. let varId;
  76. if (id) {
  77. className = id.name;
  78. varId = generateLetUidIdentifier(scope.parent, className);
  79. scope.rename(className, varId.name);
  80. } else {
  81. varId = generateLetUidIdentifier(scope.parent, typeof className === "string" ? className : "decorated_class");
  82. }
  83. const newClassExpr = _core.types.classExpression(typeof className === "string" ? _core.types.identifier(className) : null, path.node.superClass, path.node.body);
  84. const [newPath] = path.replaceWith(_core.types.sequenceExpression([newClassExpr, varId]));
  85. return {
  86. id: _core.types.cloneNode(varId),
  87. path: newPath.get("expressions.0")
  88. };
  89. }
  90. }
  91. function generateClassProperty(key, value, isStatic) {
  92. if (key.type === "PrivateName") {
  93. return _core.types.classPrivateProperty(key, value, undefined, isStatic);
  94. } else {
  95. return _core.types.classProperty(key, value, undefined, undefined, isStatic);
  96. }
  97. }
  98. function assignIdForAnonymousClass(path, className) {
  99. if (!path.node.id) {
  100. path.node.id = typeof className === "string" ? _core.types.identifier(className) : path.scope.generateUidIdentifier("Class");
  101. }
  102. }
  103. function addProxyAccessorsFor(className, element, getterKey, setterKey, targetKey, isComputed, isStatic, version) {
  104. const thisArg = (version === "2023-11" || version === "2023-05") && isStatic ? className : _core.types.thisExpression();
  105. const getterBody = _core.types.blockStatement([_core.types.returnStatement(_core.types.memberExpression(_core.types.cloneNode(thisArg), _core.types.cloneNode(targetKey)))]);
  106. const setterBody = _core.types.blockStatement([_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.memberExpression(_core.types.cloneNode(thisArg), _core.types.cloneNode(targetKey)), _core.types.identifier("v")))]);
  107. let getter, setter;
  108. if (getterKey.type === "PrivateName") {
  109. getter = _core.types.classPrivateMethod("get", getterKey, [], getterBody, isStatic);
  110. setter = _core.types.classPrivateMethod("set", setterKey, [_core.types.identifier("v")], setterBody, isStatic);
  111. } else {
  112. getter = _core.types.classMethod("get", getterKey, [], getterBody, isComputed, isStatic);
  113. setter = _core.types.classMethod("set", setterKey, [_core.types.identifier("v")], setterBody, isComputed, isStatic);
  114. }
  115. element.insertAfter(setter);
  116. element.insertAfter(getter);
  117. }
  118. function extractProxyAccessorsFor(targetKey, version) {
  119. if (version !== "2023-11" && version !== "2023-05" && version !== "2023-01") {
  120. return [_core.template.expression.ast`
  121. function () {
  122. return this.${_core.types.cloneNode(targetKey)};
  123. }
  124. `, _core.template.expression.ast`
  125. function (value) {
  126. this.${_core.types.cloneNode(targetKey)} = value;
  127. }
  128. `];
  129. }
  130. return [_core.template.expression.ast`
  131. o => o.${_core.types.cloneNode(targetKey)}
  132. `, _core.template.expression.ast`
  133. (o, v) => o.${_core.types.cloneNode(targetKey)} = v
  134. `];
  135. }
  136. function getComputedKeyLastElement(path) {
  137. path = (0, _helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers)(path);
  138. if (path.isSequenceExpression()) {
  139. const expressions = path.get("expressions");
  140. return getComputedKeyLastElement(expressions[expressions.length - 1]);
  141. }
  142. return path;
  143. }
  144. function getComputedKeyMemoiser(path) {
  145. const element = getComputedKeyLastElement(path);
  146. if (element.isConstantExpression()) {
  147. return _core.types.cloneNode(path.node);
  148. } else if (element.isIdentifier() && path.scope.hasUid(element.node.name)) {
  149. return _core.types.cloneNode(path.node);
  150. } else if (element.isAssignmentExpression() && element.get("left").isIdentifier()) {
  151. return _core.types.cloneNode(element.node.left);
  152. } else {
  153. throw new Error(`Internal Error: the computed key ${path.toString()} has not yet been memoised.`);
  154. }
  155. }
  156. function prependExpressionsToComputedKey(expressions, fieldPath) {
  157. const key = fieldPath.get("key");
  158. if (key.isSequenceExpression()) {
  159. expressions.push(...key.node.expressions);
  160. } else {
  161. expressions.push(key.node);
  162. }
  163. key.replaceWith(maybeSequenceExpression(expressions));
  164. }
  165. function appendExpressionsToComputedKey(expressions, fieldPath) {
  166. const key = fieldPath.get("key");
  167. const completion = getComputedKeyLastElement(key);
  168. if (completion.isConstantExpression()) {
  169. prependExpressionsToComputedKey(expressions, fieldPath);
  170. } else {
  171. const scopeParent = key.scope.parent;
  172. const maybeAssignment = (0, _misc.memoiseComputedKey)(completion.node, scopeParent, scopeParent.generateUid("computedKey"));
  173. if (!maybeAssignment) {
  174. prependExpressionsToComputedKey(expressions, fieldPath);
  175. } else {
  176. const expressionSequence = [...expressions, _core.types.cloneNode(maybeAssignment.left)];
  177. const completionParent = completion.parentPath;
  178. if (completionParent.isSequenceExpression()) {
  179. completionParent.pushContainer("expressions", expressionSequence);
  180. } else {
  181. completion.replaceWith(maybeSequenceExpression([_core.types.cloneNode(maybeAssignment), ...expressionSequence]));
  182. }
  183. }
  184. }
  185. }
  186. function prependExpressionsToFieldInitializer(expressions, fieldPath) {
  187. const initializer = fieldPath.get("value");
  188. if (initializer.node) {
  189. expressions.push(initializer.node);
  190. } else if (expressions.length > 0) {
  191. expressions[expressions.length - 1] = _core.types.unaryExpression("void", expressions[expressions.length - 1]);
  192. }
  193. initializer.replaceWith(maybeSequenceExpression(expressions));
  194. }
  195. function prependExpressionsToStaticBlock(expressions, blockPath) {
  196. blockPath.unshiftContainer("body", _core.types.expressionStatement(maybeSequenceExpression(expressions)));
  197. }
  198. function prependExpressionsToConstructor(expressions, constructorPath) {
  199. constructorPath.node.body.body.unshift(_core.types.expressionStatement(maybeSequenceExpression(expressions)));
  200. }
  201. function isProtoInitCallExpression(expression, protoInitCall) {
  202. return _core.types.isCallExpression(expression) && _core.types.isIdentifier(expression.callee, {
  203. name: protoInitCall.name
  204. });
  205. }
  206. function optimizeSuperCallAndExpressions(expressions, protoInitLocal) {
  207. if (protoInitLocal) {
  208. if (expressions.length >= 2 && isProtoInitCallExpression(expressions[1], protoInitLocal)) {
  209. const mergedSuperCall = _core.types.callExpression(_core.types.cloneNode(protoInitLocal), [expressions[0]]);
  210. expressions.splice(0, 2, mergedSuperCall);
  211. }
  212. if (expressions.length >= 2 && _core.types.isThisExpression(expressions[expressions.length - 1]) && isProtoInitCallExpression(expressions[expressions.length - 2], protoInitLocal)) {
  213. expressions.splice(expressions.length - 1, 1);
  214. }
  215. }
  216. return maybeSequenceExpression(expressions);
  217. }
  218. function insertExpressionsAfterSuperCallAndOptimize(expressions, constructorPath, protoInitLocal) {
  219. constructorPath.traverse({
  220. CallExpression: {
  221. exit(path) {
  222. if (!path.get("callee").isSuper()) return;
  223. const newNodes = [path.node, ...expressions.map(expr => _core.types.cloneNode(expr))];
  224. if (path.isCompletionRecord()) {
  225. newNodes.push(_core.types.thisExpression());
  226. }
  227. path.replaceWith(optimizeSuperCallAndExpressions(newNodes, protoInitLocal));
  228. path.skip();
  229. }
  230. },
  231. ClassMethod(path) {
  232. if (path.node.kind === "constructor") {
  233. path.skip();
  234. }
  235. }
  236. });
  237. }
  238. function createConstructorFromExpressions(expressions, isDerivedClass) {
  239. const body = [_core.types.expressionStatement(maybeSequenceExpression(expressions))];
  240. if (isDerivedClass) {
  241. body.unshift(_core.types.expressionStatement(_core.types.callExpression(_core.types.super(), [_core.types.spreadElement(_core.types.identifier("args"))])));
  242. }
  243. return _core.types.classMethod("constructor", _core.types.identifier("constructor"), isDerivedClass ? [_core.types.restElement(_core.types.identifier("args"))] : [], _core.types.blockStatement(body));
  244. }
  245. function createStaticBlockFromExpressions(expressions) {
  246. return _core.types.staticBlock([_core.types.expressionStatement(maybeSequenceExpression(expressions))]);
  247. }
  248. const FIELD = 0;
  249. const ACCESSOR = 1;
  250. const METHOD = 2;
  251. const GETTER = 3;
  252. const SETTER = 4;
  253. const STATIC_OLD_VERSION = 5;
  254. const STATIC = 8;
  255. const DECORATORS_HAVE_THIS = 16;
  256. function getElementKind(element) {
  257. switch (element.node.type) {
  258. case "ClassProperty":
  259. case "ClassPrivateProperty":
  260. return FIELD;
  261. case "ClassAccessorProperty":
  262. return ACCESSOR;
  263. case "ClassMethod":
  264. case "ClassPrivateMethod":
  265. if (element.node.kind === "get") {
  266. return GETTER;
  267. } else if (element.node.kind === "set") {
  268. return SETTER;
  269. } else {
  270. return METHOD;
  271. }
  272. }
  273. }
  274. function toSortedDecoratorInfo(info) {
  275. return [...info.filter(el => el.isStatic && el.kind >= ACCESSOR && el.kind <= SETTER), ...info.filter(el => !el.isStatic && el.kind >= ACCESSOR && el.kind <= SETTER), ...info.filter(el => el.isStatic && el.kind === FIELD), ...info.filter(el => !el.isStatic && el.kind === FIELD)];
  276. }
  277. function generateDecorationList(decorators, decoratorsThis, version) {
  278. const decsCount = decorators.length;
  279. const haveOneThis = decoratorsThis.some(Boolean);
  280. const decs = [];
  281. for (let i = 0; i < decsCount; i++) {
  282. if ((version === "2023-11" || version === "2023-05") && haveOneThis) {
  283. decs.push(decoratorsThis[i] || _core.types.unaryExpression("void", _core.types.numericLiteral(0)));
  284. }
  285. decs.push(decorators[i].expression);
  286. }
  287. return {
  288. haveThis: haveOneThis,
  289. decs
  290. };
  291. }
  292. function generateDecorationExprs(decorationInfo, version) {
  293. return _core.types.arrayExpression(decorationInfo.map(el => {
  294. let flag = el.kind;
  295. if (el.isStatic) {
  296. flag += version === "2023-11" || version === "2023-05" ? STATIC : STATIC_OLD_VERSION;
  297. }
  298. if (el.decoratorsHaveThis) flag += DECORATORS_HAVE_THIS;
  299. return _core.types.arrayExpression([el.decoratorsArray, _core.types.numericLiteral(flag), el.name, ...(el.privateMethods || [])]);
  300. }));
  301. }
  302. function extractElementLocalAssignments(decorationInfo) {
  303. const localIds = [];
  304. for (const el of decorationInfo) {
  305. const {
  306. locals
  307. } = el;
  308. if (Array.isArray(locals)) {
  309. localIds.push(...locals);
  310. } else if (locals !== undefined) {
  311. localIds.push(locals);
  312. }
  313. }
  314. return localIds;
  315. }
  316. function addCallAccessorsFor(version, element, key, getId, setId, isStatic) {
  317. element.insertAfter(_core.types.classPrivateMethod("get", _core.types.cloneNode(key), [], _core.types.blockStatement([_core.types.returnStatement(_core.types.callExpression(_core.types.cloneNode(getId), version === "2023-11" && isStatic ? [] : [_core.types.thisExpression()]))]), isStatic));
  318. element.insertAfter(_core.types.classPrivateMethod("set", _core.types.cloneNode(key), [_core.types.identifier("v")], _core.types.blockStatement([_core.types.expressionStatement(_core.types.callExpression(_core.types.cloneNode(setId), version === "2023-11" && isStatic ? [_core.types.identifier("v")] : [_core.types.thisExpression(), _core.types.identifier("v")]))]), isStatic));
  319. }
  320. function movePrivateAccessor(element, key, methodLocalVar, isStatic) {
  321. let params;
  322. let block;
  323. if (element.node.kind === "set") {
  324. params = [_core.types.identifier("v")];
  325. block = [_core.types.expressionStatement(_core.types.callExpression(methodLocalVar, [_core.types.thisExpression(), _core.types.identifier("v")]))];
  326. } else {
  327. params = [];
  328. block = [_core.types.returnStatement(_core.types.callExpression(methodLocalVar, [_core.types.thisExpression()]))];
  329. }
  330. element.replaceWith(_core.types.classPrivateMethod(element.node.kind, _core.types.cloneNode(key), params, _core.types.blockStatement(block), isStatic));
  331. }
  332. function isClassDecoratableElementPath(path) {
  333. const {
  334. type
  335. } = path;
  336. return type !== "TSDeclareMethod" && type !== "TSIndexSignature" && type !== "StaticBlock";
  337. }
  338. function staticBlockToIIFE(block) {
  339. return _core.types.callExpression(_core.types.arrowFunctionExpression([], _core.types.blockStatement(block.body)), []);
  340. }
  341. function staticBlockToFunctionClosure(block) {
  342. return _core.types.functionExpression(null, [], _core.types.blockStatement(block.body));
  343. }
  344. function fieldInitializerToClosure(value) {
  345. return _core.types.functionExpression(null, [], _core.types.blockStatement([_core.types.returnStatement(value)]));
  346. }
  347. function maybeSequenceExpression(exprs) {
  348. if (exprs.length === 0) return _core.types.unaryExpression("void", _core.types.numericLiteral(0));
  349. if (exprs.length === 1) return exprs[0];
  350. return _core.types.sequenceExpression(exprs);
  351. }
  352. function createFunctionExpressionFromPrivateMethod(node) {
  353. const {
  354. params,
  355. body,
  356. generator: isGenerator,
  357. async: isAsync
  358. } = node;
  359. return _core.types.functionExpression(undefined, params, body, isGenerator, isAsync);
  360. }
  361. function createSetFunctionNameCall(state, className) {
  362. return _core.types.callExpression(state.addHelper("setFunctionName"), [_core.types.thisExpression(), className]);
  363. }
  364. function createToPropertyKeyCall(state, propertyKey) {
  365. return _core.types.callExpression(state.addHelper("toPropertyKey"), [propertyKey]);
  366. }
  367. function createPrivateBrandCheckClosure(brandName) {
  368. return _core.types.arrowFunctionExpression([_core.types.identifier("_")], _core.types.binaryExpression("in", _core.types.cloneNode(brandName), _core.types.identifier("_")));
  369. }
  370. function usesPrivateField(expression) {
  371. {
  372. try {
  373. _core.types.traverseFast(expression, node => {
  374. if (_core.types.isPrivateName(node)) {
  375. throw null;
  376. }
  377. });
  378. return false;
  379. } catch (_unused) {
  380. return true;
  381. }
  382. }
  383. }
  384. function convertToComputedKey(path) {
  385. const {
  386. node
  387. } = path;
  388. node.computed = true;
  389. if (_core.types.isIdentifier(node.key)) {
  390. node.key = _core.types.stringLiteral(node.key.name);
  391. }
  392. }
  393. function hasInstancePrivateAccess(path, privateNames) {
  394. let containsInstancePrivateAccess = false;
  395. if (privateNames.length > 0) {
  396. const privateNameVisitor = (0, _fields.privateNameVisitorFactory)({
  397. PrivateName(path, state) {
  398. if (state.privateNamesMap.has(path.node.id.name)) {
  399. containsInstancePrivateAccess = true;
  400. path.stop();
  401. }
  402. }
  403. });
  404. const privateNamesMap = new Map();
  405. for (const name of privateNames) {
  406. privateNamesMap.set(name, null);
  407. }
  408. path.traverse(privateNameVisitor, {
  409. privateNamesMap: privateNamesMap
  410. });
  411. }
  412. return containsInstancePrivateAccess;
  413. }
  414. function checkPrivateMethodUpdateError(path, decoratedPrivateMethods) {
  415. const privateNameVisitor = (0, _fields.privateNameVisitorFactory)({
  416. PrivateName(path, state) {
  417. if (!state.privateNamesMap.has(path.node.id.name)) return;
  418. const parentPath = path.parentPath;
  419. const parentParentPath = parentPath.parentPath;
  420. if (parentParentPath.node.type === "AssignmentExpression" && parentParentPath.node.left === parentPath.node || parentParentPath.node.type === "UpdateExpression" || parentParentPath.node.type === "RestElement" || parentParentPath.node.type === "ArrayPattern" || parentParentPath.node.type === "ObjectProperty" && parentParentPath.node.value === parentPath.node && parentParentPath.parentPath.type === "ObjectPattern" || parentParentPath.node.type === "ForOfStatement" && parentParentPath.node.left === parentPath.node) {
  421. throw path.buildCodeFrameError(`Decorated private methods are read-only, but "#${path.node.id.name}" is updated via this expression.`);
  422. }
  423. }
  424. });
  425. const privateNamesMap = new Map();
  426. for (const name of decoratedPrivateMethods) {
  427. privateNamesMap.set(name, null);
  428. }
  429. path.traverse(privateNameVisitor, {
  430. privateNamesMap: privateNamesMap
  431. });
  432. }
  433. function transformClass(path, state, constantSuper, ignoreFunctionLength, className, propertyVisitor, version) {
  434. var _path$node$id;
  435. const body = path.get("body.body");
  436. const classDecorators = path.node.decorators;
  437. let hasElementDecorators = false;
  438. let hasComputedKeysSideEffects = false;
  439. let elemDecsUseFnContext = false;
  440. const generateClassPrivateUid = createLazyPrivateUidGeneratorForClass(path);
  441. const classAssignments = [];
  442. const scopeParent = path.scope.parent;
  443. const memoiseExpression = (expression, hint, assignments) => {
  444. const localEvaluatedId = generateLetUidIdentifier(scopeParent, hint);
  445. assignments.push(_core.types.assignmentExpression("=", localEvaluatedId, expression));
  446. return _core.types.cloneNode(localEvaluatedId);
  447. };
  448. let protoInitLocal;
  449. let staticInitLocal;
  450. const classIdName = (_path$node$id = path.node.id) == null ? void 0 : _path$node$id.name;
  451. const setClassName = typeof className === "object" ? className : undefined;
  452. const usesFunctionContextOrYieldAwait = decorator => {
  453. {
  454. try {
  455. _core.types.traverseFast(decorator, node => {
  456. if (_core.types.isThisExpression(node) || _core.types.isSuper(node) || _core.types.isYieldExpression(node) || _core.types.isAwaitExpression(node) || _core.types.isIdentifier(node, {
  457. name: "arguments"
  458. }) || classIdName && _core.types.isIdentifier(node, {
  459. name: classIdName
  460. }) || _core.types.isMetaProperty(node) && node.meta.name !== "import") {
  461. throw null;
  462. }
  463. });
  464. return false;
  465. } catch (_unused2) {
  466. return true;
  467. }
  468. }
  469. };
  470. const instancePrivateNames = [];
  471. for (const element of body) {
  472. if (!isClassDecoratableElementPath(element)) {
  473. continue;
  474. }
  475. const elementNode = element.node;
  476. if (!elementNode.static && _core.types.isPrivateName(elementNode.key)) {
  477. instancePrivateNames.push(elementNode.key.id.name);
  478. }
  479. if (isDecorated(elementNode)) {
  480. switch (elementNode.type) {
  481. case "ClassProperty":
  482. propertyVisitor.ClassProperty(element, state);
  483. break;
  484. case "ClassPrivateProperty":
  485. propertyVisitor.ClassPrivateProperty(element, state);
  486. break;
  487. case "ClassAccessorProperty":
  488. propertyVisitor.ClassAccessorProperty(element, state);
  489. if (version === "2023-11") {
  490. break;
  491. }
  492. default:
  493. if (elementNode.static) {
  494. staticInitLocal != null ? staticInitLocal : staticInitLocal = generateLetUidIdentifier(scopeParent, "initStatic");
  495. } else {
  496. protoInitLocal != null ? protoInitLocal : protoInitLocal = generateLetUidIdentifier(scopeParent, "initProto");
  497. }
  498. break;
  499. }
  500. hasElementDecorators = true;
  501. elemDecsUseFnContext || (elemDecsUseFnContext = elementNode.decorators.some(usesFunctionContextOrYieldAwait));
  502. } else if (elementNode.type === "ClassAccessorProperty") {
  503. propertyVisitor.ClassAccessorProperty(element, state);
  504. const {
  505. key,
  506. value,
  507. static: isStatic,
  508. computed
  509. } = elementNode;
  510. const newId = generateClassPrivateUid();
  511. const newField = generateClassProperty(newId, value, isStatic);
  512. const keyPath = element.get("key");
  513. const [newPath] = element.replaceWith(newField);
  514. let getterKey, setterKey;
  515. if (computed && !keyPath.isConstantExpression()) {
  516. getterKey = (0, _misc.memoiseComputedKey)(createToPropertyKeyCall(state, key), scopeParent, scopeParent.generateUid("computedKey"));
  517. setterKey = _core.types.cloneNode(getterKey.left);
  518. } else {
  519. getterKey = _core.types.cloneNode(key);
  520. setterKey = _core.types.cloneNode(key);
  521. }
  522. assignIdForAnonymousClass(path, className);
  523. addProxyAccessorsFor(path.node.id, newPath, getterKey, setterKey, newId, computed, isStatic, version);
  524. }
  525. if ("computed" in element.node && element.node.computed) {
  526. hasComputedKeysSideEffects || (hasComputedKeysSideEffects = !scopeParent.isStatic(element.node.key));
  527. }
  528. }
  529. if (!classDecorators && !hasElementDecorators) {
  530. if (!path.node.id && typeof className === "string") {
  531. path.node.id = _core.types.identifier(className);
  532. }
  533. if (setClassName) {
  534. path.node.body.body.unshift(createStaticBlockFromExpressions([createSetFunctionNameCall(state, setClassName)]));
  535. }
  536. return;
  537. }
  538. const elementDecoratorInfo = [];
  539. let constructorPath;
  540. const decoratedPrivateMethods = new Set();
  541. let classInitLocal, classIdLocal;
  542. let decoratorReceiverId = null;
  543. function handleDecorators(decorators) {
  544. let hasSideEffects = false;
  545. let usesFnContext = false;
  546. const decoratorsThis = [];
  547. for (const decorator of decorators) {
  548. const {
  549. expression
  550. } = decorator;
  551. let object;
  552. if ((version === "2023-11" || version === "2023-05") && _core.types.isMemberExpression(expression)) {
  553. if (_core.types.isSuper(expression.object)) {
  554. object = _core.types.thisExpression();
  555. } else if (scopeParent.isStatic(expression.object)) {
  556. object = _core.types.cloneNode(expression.object);
  557. } else {
  558. decoratorReceiverId != null ? decoratorReceiverId : decoratorReceiverId = generateLetUidIdentifier(scopeParent, "obj");
  559. object = _core.types.assignmentExpression("=", _core.types.cloneNode(decoratorReceiverId), expression.object);
  560. expression.object = _core.types.cloneNode(decoratorReceiverId);
  561. }
  562. }
  563. decoratorsThis.push(object);
  564. hasSideEffects || (hasSideEffects = !scopeParent.isStatic(expression));
  565. usesFnContext || (usesFnContext = usesFunctionContextOrYieldAwait(decorator));
  566. }
  567. return {
  568. hasSideEffects,
  569. usesFnContext,
  570. decoratorsThis
  571. };
  572. }
  573. const willExtractSomeElemDecs = hasComputedKeysSideEffects || elemDecsUseFnContext || version !== "2023-11";
  574. let needsDeclaraionForClassBinding = false;
  575. let classDecorationsFlag = 0;
  576. let classDecorations = [];
  577. let classDecorationsId;
  578. let computedKeyAssignments = [];
  579. if (classDecorators) {
  580. classInitLocal = generateLetUidIdentifier(scopeParent, "initClass");
  581. needsDeclaraionForClassBinding = path.isClassDeclaration();
  582. ({
  583. id: classIdLocal,
  584. path
  585. } = replaceClassWithVar(path, className));
  586. path.node.decorators = null;
  587. const classDecsUsePrivateName = classDecorators.some(usesPrivateField);
  588. const {
  589. hasSideEffects,
  590. usesFnContext,
  591. decoratorsThis
  592. } = handleDecorators(classDecorators);
  593. const {
  594. haveThis,
  595. decs
  596. } = generateDecorationList(classDecorators, decoratorsThis, version);
  597. classDecorationsFlag = haveThis ? 1 : 0;
  598. classDecorations = decs;
  599. if (usesFnContext || hasSideEffects && willExtractSomeElemDecs || classDecsUsePrivateName) {
  600. classDecorationsId = memoiseExpression(_core.types.arrayExpression(classDecorations), "classDecs", classAssignments);
  601. }
  602. if (!hasElementDecorators) {
  603. for (const element of path.get("body.body")) {
  604. const {
  605. node
  606. } = element;
  607. const isComputed = "computed" in node && node.computed;
  608. if (isComputed) {
  609. if (element.isClassProperty({
  610. static: true
  611. })) {
  612. if (!element.get("key").isConstantExpression()) {
  613. const key = node.key;
  614. const maybeAssignment = (0, _misc.memoiseComputedKey)(key, scopeParent, scopeParent.generateUid("computedKey"));
  615. if (maybeAssignment != null) {
  616. node.key = _core.types.cloneNode(maybeAssignment.left);
  617. computedKeyAssignments.push(maybeAssignment);
  618. }
  619. }
  620. } else if (computedKeyAssignments.length > 0) {
  621. prependExpressionsToComputedKey(computedKeyAssignments, element);
  622. computedKeyAssignments = [];
  623. }
  624. }
  625. }
  626. }
  627. } else {
  628. assignIdForAnonymousClass(path, className);
  629. classIdLocal = _core.types.cloneNode(path.node.id);
  630. }
  631. let lastInstancePrivateName;
  632. let needsInstancePrivateBrandCheck = false;
  633. let fieldInitializerExpressions = [];
  634. let staticFieldInitializerExpressions = [];
  635. if (hasElementDecorators) {
  636. if (protoInitLocal) {
  637. const protoInitCall = _core.types.callExpression(_core.types.cloneNode(protoInitLocal), [_core.types.thisExpression()]);
  638. fieldInitializerExpressions.push(protoInitCall);
  639. }
  640. for (const element of body) {
  641. if (!isClassDecoratableElementPath(element)) {
  642. if (staticFieldInitializerExpressions.length > 0 && element.isStaticBlock()) {
  643. prependExpressionsToStaticBlock(staticFieldInitializerExpressions, element);
  644. staticFieldInitializerExpressions = [];
  645. }
  646. continue;
  647. }
  648. const {
  649. node
  650. } = element;
  651. const decorators = node.decorators;
  652. const hasDecorators = !!(decorators != null && decorators.length);
  653. const isComputed = "computed" in node && node.computed;
  654. let name = "computedKey";
  655. if (node.key.type === "PrivateName") {
  656. name = node.key.id.name;
  657. } else if (!isComputed && node.key.type === "Identifier") {
  658. name = node.key.name;
  659. }
  660. let decoratorsArray;
  661. let decoratorsHaveThis;
  662. if (hasDecorators) {
  663. const {
  664. hasSideEffects,
  665. usesFnContext,
  666. decoratorsThis
  667. } = handleDecorators(decorators);
  668. const {
  669. decs,
  670. haveThis
  671. } = generateDecorationList(decorators, decoratorsThis, version);
  672. decoratorsHaveThis = haveThis;
  673. decoratorsArray = decs.length === 1 ? decs[0] : _core.types.arrayExpression(decs);
  674. if (usesFnContext || hasSideEffects && willExtractSomeElemDecs) {
  675. decoratorsArray = memoiseExpression(decoratorsArray, name + "Decs", computedKeyAssignments);
  676. }
  677. }
  678. if (isComputed) {
  679. if (!element.get("key").isConstantExpression()) {
  680. const key = node.key;
  681. const maybeAssignment = (0, _misc.memoiseComputedKey)(hasDecorators ? createToPropertyKeyCall(state, key) : key, scopeParent, scopeParent.generateUid("computedKey"));
  682. if (maybeAssignment != null) {
  683. if (classDecorators && element.isClassProperty({
  684. static: true
  685. })) {
  686. node.key = _core.types.cloneNode(maybeAssignment.left);
  687. computedKeyAssignments.push(maybeAssignment);
  688. } else {
  689. node.key = maybeAssignment;
  690. }
  691. }
  692. }
  693. }
  694. const {
  695. key,
  696. static: isStatic
  697. } = node;
  698. const isPrivate = key.type === "PrivateName";
  699. const kind = getElementKind(element);
  700. if (isPrivate && !isStatic) {
  701. if (hasDecorators) {
  702. needsInstancePrivateBrandCheck = true;
  703. }
  704. if (_core.types.isClassPrivateProperty(node) || !lastInstancePrivateName) {
  705. lastInstancePrivateName = key;
  706. }
  707. }
  708. if (element.isClassMethod({
  709. kind: "constructor"
  710. })) {
  711. constructorPath = element;
  712. }
  713. let locals;
  714. if (hasDecorators) {
  715. let privateMethods;
  716. let nameExpr;
  717. if (isComputed) {
  718. nameExpr = getComputedKeyMemoiser(element.get("key"));
  719. } else if (key.type === "PrivateName") {
  720. nameExpr = _core.types.stringLiteral(key.id.name);
  721. } else if (key.type === "Identifier") {
  722. nameExpr = _core.types.stringLiteral(key.name);
  723. } else {
  724. nameExpr = _core.types.cloneNode(key);
  725. }
  726. if (kind === ACCESSOR) {
  727. const {
  728. value
  729. } = element.node;
  730. const params = version === "2023-11" && isStatic ? [] : [_core.types.thisExpression()];
  731. if (value) {
  732. params.push(_core.types.cloneNode(value));
  733. }
  734. const newId = generateClassPrivateUid();
  735. const newFieldInitId = generateLetUidIdentifier(scopeParent, `init_${name}`);
  736. const newValue = _core.types.callExpression(_core.types.cloneNode(newFieldInitId), params);
  737. const newField = generateClassProperty(newId, newValue, isStatic);
  738. const [newPath] = element.replaceWith(newField);
  739. if (isPrivate) {
  740. privateMethods = extractProxyAccessorsFor(newId, version);
  741. const getId = generateLetUidIdentifier(scopeParent, `get_${name}`);
  742. const setId = generateLetUidIdentifier(scopeParent, `set_${name}`);
  743. addCallAccessorsFor(version, newPath, key, getId, setId, isStatic);
  744. locals = [newFieldInitId, getId, setId];
  745. } else {
  746. assignIdForAnonymousClass(path, className);
  747. addProxyAccessorsFor(path.node.id, newPath, _core.types.cloneNode(key), _core.types.isAssignmentExpression(key) ? _core.types.cloneNode(key.left) : _core.types.cloneNode(key), newId, isComputed, isStatic, version);
  748. locals = [newFieldInitId];
  749. }
  750. } else if (kind === FIELD) {
  751. const initId = generateLetUidIdentifier(scopeParent, `init_${name}`);
  752. const valuePath = element.get("value");
  753. const args = version === "2023-11" && isStatic ? [] : [_core.types.thisExpression()];
  754. if (valuePath.node) args.push(valuePath.node);
  755. valuePath.replaceWith(_core.types.callExpression(_core.types.cloneNode(initId), args));
  756. locals = [initId];
  757. if (isPrivate) {
  758. privateMethods = extractProxyAccessorsFor(key, version);
  759. }
  760. } else if (isPrivate) {
  761. const callId = generateLetUidIdentifier(scopeParent, `call_${name}`);
  762. locals = [callId];
  763. const replaceSupers = new _helperReplaceSupers.default({
  764. constantSuper,
  765. methodPath: element,
  766. objectRef: classIdLocal,
  767. superRef: path.node.superClass,
  768. file: state.file,
  769. refToPreserve: classIdLocal
  770. });
  771. replaceSupers.replace();
  772. privateMethods = [createFunctionExpressionFromPrivateMethod(element.node)];
  773. if (kind === GETTER || kind === SETTER) {
  774. movePrivateAccessor(element, _core.types.cloneNode(key), _core.types.cloneNode(callId), isStatic);
  775. } else {
  776. const node = element.node;
  777. path.node.body.body.unshift(_core.types.classPrivateProperty(key, _core.types.cloneNode(callId), [], node.static));
  778. decoratedPrivateMethods.add(key.id.name);
  779. element.remove();
  780. }
  781. }
  782. elementDecoratorInfo.push({
  783. kind,
  784. decoratorsArray,
  785. decoratorsHaveThis,
  786. name: nameExpr,
  787. isStatic,
  788. privateMethods,
  789. locals
  790. });
  791. if (element.node) {
  792. element.node.decorators = null;
  793. }
  794. }
  795. if (isComputed && computedKeyAssignments.length > 0) {
  796. if (classDecorators && element.isClassProperty({
  797. static: true
  798. })) {} else {
  799. prependExpressionsToComputedKey(computedKeyAssignments, kind === ACCESSOR ? element.getNextSibling() : element);
  800. computedKeyAssignments = [];
  801. }
  802. }
  803. if (fieldInitializerExpressions.length > 0 && !isStatic && (kind === FIELD || kind === ACCESSOR)) {
  804. prependExpressionsToFieldInitializer(fieldInitializerExpressions, element);
  805. fieldInitializerExpressions = [];
  806. }
  807. if (staticFieldInitializerExpressions.length > 0 && isStatic && (kind === FIELD || kind === ACCESSOR)) {
  808. prependExpressionsToFieldInitializer(staticFieldInitializerExpressions, element);
  809. staticFieldInitializerExpressions = [];
  810. }
  811. if (hasDecorators && version === "2023-11") {
  812. if (kind === FIELD || kind === ACCESSOR) {
  813. const initExtraId = generateLetUidIdentifier(scopeParent, `init_extra_${name}`);
  814. locals.push(initExtraId);
  815. const initExtraCall = _core.types.callExpression(_core.types.cloneNode(initExtraId), isStatic ? [] : [_core.types.thisExpression()]);
  816. if (!isStatic) {
  817. fieldInitializerExpressions.push(initExtraCall);
  818. } else {
  819. staticFieldInitializerExpressions.push(initExtraCall);
  820. }
  821. }
  822. }
  823. }
  824. }
  825. if (computedKeyAssignments.length > 0) {
  826. const elements = path.get("body.body");
  827. let lastComputedElement;
  828. for (let i = elements.length - 1; i >= 0; i--) {
  829. const path = elements[i];
  830. const node = path.node;
  831. if (node.computed) {
  832. if (classDecorators && _core.types.isClassProperty(node, {
  833. static: true
  834. })) {
  835. continue;
  836. }
  837. lastComputedElement = path;
  838. break;
  839. }
  840. }
  841. if (lastComputedElement != null) {
  842. appendExpressionsToComputedKey(computedKeyAssignments, lastComputedElement);
  843. computedKeyAssignments = [];
  844. } else {}
  845. }
  846. if (fieldInitializerExpressions.length > 0) {
  847. const isDerivedClass = !!path.node.superClass;
  848. if (constructorPath) {
  849. if (isDerivedClass) {
  850. insertExpressionsAfterSuperCallAndOptimize(fieldInitializerExpressions, constructorPath, protoInitLocal);
  851. } else {
  852. prependExpressionsToConstructor(fieldInitializerExpressions, constructorPath);
  853. }
  854. } else {
  855. path.node.body.body.unshift(createConstructorFromExpressions(fieldInitializerExpressions, isDerivedClass));
  856. }
  857. fieldInitializerExpressions = [];
  858. }
  859. if (staticFieldInitializerExpressions.length > 0) {
  860. path.node.body.body.push(createStaticBlockFromExpressions(staticFieldInitializerExpressions));
  861. staticFieldInitializerExpressions = [];
  862. }
  863. const sortedElementDecoratorInfo = toSortedDecoratorInfo(elementDecoratorInfo);
  864. const elementDecorations = generateDecorationExprs(version === "2023-11" ? elementDecoratorInfo : sortedElementDecoratorInfo, version);
  865. const elementLocals = extractElementLocalAssignments(sortedElementDecoratorInfo);
  866. if (protoInitLocal) {
  867. elementLocals.push(protoInitLocal);
  868. }
  869. if (staticInitLocal) {
  870. elementLocals.push(staticInitLocal);
  871. }
  872. const classLocals = [];
  873. let classInitInjected = false;
  874. const classInitCall = classInitLocal && _core.types.callExpression(_core.types.cloneNode(classInitLocal), []);
  875. let originalClassPath = path;
  876. const originalClass = path.node;
  877. const staticClosures = [];
  878. if (classDecorators) {
  879. classLocals.push(classIdLocal, classInitLocal);
  880. const statics = [];
  881. path.get("body.body").forEach(element => {
  882. if (element.isStaticBlock()) {
  883. if (hasInstancePrivateAccess(element, instancePrivateNames)) {
  884. const staticBlockClosureId = memoiseExpression(staticBlockToFunctionClosure(element.node), "staticBlock", staticClosures);
  885. staticFieldInitializerExpressions.push(_core.types.callExpression(_core.types.memberExpression(staticBlockClosureId, _core.types.identifier("call")), [_core.types.thisExpression()]));
  886. } else {
  887. staticFieldInitializerExpressions.push(staticBlockToIIFE(element.node));
  888. }
  889. element.remove();
  890. return;
  891. }
  892. if ((element.isClassProperty() || element.isClassPrivateProperty()) && element.node.static) {
  893. const valuePath = element.get("value");
  894. if (hasInstancePrivateAccess(valuePath, instancePrivateNames)) {
  895. const fieldValueClosureId = memoiseExpression(fieldInitializerToClosure(valuePath.node), "fieldValue", staticClosures);
  896. valuePath.replaceWith(_core.types.callExpression(_core.types.memberExpression(fieldValueClosureId, _core.types.identifier("call")), [_core.types.thisExpression()]));
  897. }
  898. if (staticFieldInitializerExpressions.length > 0) {
  899. prependExpressionsToFieldInitializer(staticFieldInitializerExpressions, element);
  900. staticFieldInitializerExpressions = [];
  901. }
  902. element.node.static = false;
  903. statics.push(element.node);
  904. element.remove();
  905. } else if (element.isClassPrivateMethod({
  906. static: true
  907. })) {
  908. if (hasInstancePrivateAccess(element, instancePrivateNames)) {
  909. const replaceSupers = new _helperReplaceSupers.default({
  910. constantSuper,
  911. methodPath: element,
  912. objectRef: classIdLocal,
  913. superRef: path.node.superClass,
  914. file: state.file,
  915. refToPreserve: classIdLocal
  916. });
  917. replaceSupers.replace();
  918. const privateMethodDelegateId = memoiseExpression(createFunctionExpressionFromPrivateMethod(element.node), element.get("key.id").node.name, staticClosures);
  919. if (ignoreFunctionLength) {
  920. element.node.params = [_core.types.restElement(_core.types.identifier("arg"))];
  921. element.node.body = _core.types.blockStatement([_core.types.returnStatement(_core.types.callExpression(_core.types.memberExpression(privateMethodDelegateId, _core.types.identifier("apply")), [_core.types.thisExpression(), _core.types.identifier("arg")]))]);
  922. } else {
  923. element.node.params = element.node.params.map((p, i) => {
  924. if (_core.types.isRestElement(p)) {
  925. return _core.types.restElement(_core.types.identifier("arg"));
  926. } else {
  927. return _core.types.identifier("_" + i);
  928. }
  929. });
  930. element.node.body = _core.types.blockStatement([_core.types.returnStatement(_core.types.callExpression(_core.types.memberExpression(privateMethodDelegateId, _core.types.identifier("apply")), [_core.types.thisExpression(), _core.types.identifier("arguments")]))]);
  931. }
  932. }
  933. element.node.static = false;
  934. statics.push(element.node);
  935. element.remove();
  936. }
  937. });
  938. if (statics.length > 0 || staticFieldInitializerExpressions.length > 0) {
  939. const staticsClass = _core.template.expression.ast`
  940. class extends ${state.addHelper("identity")} {}
  941. `;
  942. staticsClass.body.body = [_core.types.classProperty(_core.types.toExpression(originalClass), undefined, undefined, undefined, true, true), ...statics];
  943. const constructorBody = [];
  944. const newExpr = _core.types.newExpression(staticsClass, []);
  945. if (staticFieldInitializerExpressions.length > 0) {
  946. constructorBody.push(...staticFieldInitializerExpressions);
  947. }
  948. if (classInitCall) {
  949. classInitInjected = true;
  950. constructorBody.push(classInitCall);
  951. }
  952. if (constructorBody.length > 0) {
  953. constructorBody.unshift(_core.types.callExpression(_core.types.super(), [_core.types.cloneNode(classIdLocal)]));
  954. staticsClass.body.body.push(createConstructorFromExpressions(constructorBody, false));
  955. } else {
  956. newExpr.arguments.push(_core.types.cloneNode(classIdLocal));
  957. }
  958. const [newPath] = path.replaceWith(newExpr);
  959. originalClassPath = newPath.get("callee").get("body").get("body.0.key");
  960. }
  961. }
  962. if (!classInitInjected && classInitCall) {
  963. path.node.body.body.push(_core.types.staticBlock([_core.types.expressionStatement(classInitCall)]));
  964. }
  965. let {
  966. superClass
  967. } = originalClass;
  968. if (superClass && (version === "2023-11" || version === "2023-05")) {
  969. const id = path.scope.maybeGenerateMemoised(superClass);
  970. if (id) {
  971. originalClass.superClass = _core.types.assignmentExpression("=", id, superClass);
  972. superClass = id;
  973. }
  974. }
  975. const applyDecoratorWrapper = _core.types.staticBlock([]);
  976. originalClass.body.body.unshift(applyDecoratorWrapper);
  977. const applyDecsBody = applyDecoratorWrapper.body;
  978. if (computedKeyAssignments.length > 0) {
  979. const elements = originalClassPath.get("body.body");
  980. let firstPublicElement;
  981. for (const path of elements) {
  982. if ((path.isClassProperty() || path.isClassMethod()) && path.node.kind !== "constructor") {
  983. firstPublicElement = path;
  984. break;
  985. }
  986. }
  987. if (firstPublicElement != null) {
  988. convertToComputedKey(firstPublicElement);
  989. prependExpressionsToComputedKey(computedKeyAssignments, firstPublicElement);
  990. } else {
  991. originalClass.body.body.unshift(_core.types.classProperty(_core.types.sequenceExpression([...computedKeyAssignments, _core.types.stringLiteral("_")]), undefined, undefined, undefined, true, true));
  992. applyDecsBody.push(_core.types.expressionStatement(_core.types.unaryExpression("delete", _core.types.memberExpression(_core.types.thisExpression(), _core.types.identifier("_")))));
  993. }
  994. computedKeyAssignments = [];
  995. }
  996. applyDecsBody.push(_core.types.expressionStatement(createLocalsAssignment(elementLocals, classLocals, elementDecorations, classDecorationsId != null ? classDecorationsId : _core.types.arrayExpression(classDecorations), _core.types.numericLiteral(classDecorationsFlag), needsInstancePrivateBrandCheck ? lastInstancePrivateName : null, setClassName, _core.types.cloneNode(superClass), state, version)));
  997. if (staticInitLocal) {
  998. applyDecsBody.push(_core.types.expressionStatement(_core.types.callExpression(_core.types.cloneNode(staticInitLocal), [_core.types.thisExpression()])));
  999. }
  1000. if (staticClosures.length > 0) {
  1001. applyDecsBody.push(...staticClosures.map(expr => _core.types.expressionStatement(expr)));
  1002. }
  1003. path.insertBefore(classAssignments.map(expr => _core.types.expressionStatement(expr)));
  1004. if (needsDeclaraionForClassBinding) {
  1005. const classBindingInfo = scopeParent.getBinding(classIdLocal.name);
  1006. if (!classBindingInfo.constantViolations.length) {
  1007. path.insertBefore(_core.types.variableDeclaration("let", [_core.types.variableDeclarator(_core.types.cloneNode(classIdLocal))]));
  1008. } else {
  1009. const classOuterBindingDelegateLocal = scopeParent.generateUidIdentifier("t" + classIdLocal.name);
  1010. const classOuterBindingLocal = classIdLocal;
  1011. path.replaceWithMultiple([_core.types.variableDeclaration("let", [_core.types.variableDeclarator(_core.types.cloneNode(classOuterBindingLocal)), _core.types.variableDeclarator(classOuterBindingDelegateLocal)]), _core.types.blockStatement([_core.types.variableDeclaration("let", [_core.types.variableDeclarator(_core.types.cloneNode(classIdLocal))]), path.node, _core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.cloneNode(classOuterBindingDelegateLocal), _core.types.cloneNode(classIdLocal)))]), _core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.cloneNode(classOuterBindingLocal), _core.types.cloneNode(classOuterBindingDelegateLocal)))]);
  1012. }
  1013. }
  1014. if (decoratedPrivateMethods.size > 0) {
  1015. checkPrivateMethodUpdateError(path, decoratedPrivateMethods);
  1016. }
  1017. path.scope.crawl();
  1018. return path;
  1019. }
  1020. function createLocalsAssignment(elementLocals, classLocals, elementDecorations, classDecorations, classDecorationsFlag, maybePrivateBrandName, setClassName, superClass, state, version) {
  1021. let lhs, rhs;
  1022. const args = [setClassName ? createSetFunctionNameCall(state, setClassName) : _core.types.thisExpression(), classDecorations, elementDecorations];
  1023. {
  1024. if (version !== "2023-11") {
  1025. args.splice(1, 2, elementDecorations, classDecorations);
  1026. }
  1027. if (version === "2021-12" || version === "2022-03" && !state.availableHelper("applyDecs2203R")) {
  1028. lhs = _core.types.arrayPattern([...elementLocals, ...classLocals]);
  1029. rhs = _core.types.callExpression(state.addHelper(version === "2021-12" ? "applyDecs" : "applyDecs2203"), args);
  1030. return _core.types.assignmentExpression("=", lhs, rhs);
  1031. } else if (version === "2022-03") {
  1032. rhs = _core.types.callExpression(state.addHelper("applyDecs2203R"), args);
  1033. } else if (version === "2023-01") {
  1034. if (maybePrivateBrandName) {
  1035. args.push(createPrivateBrandCheckClosure(maybePrivateBrandName));
  1036. }
  1037. rhs = _core.types.callExpression(state.addHelper("applyDecs2301"), args);
  1038. } else if (version === "2023-05") {
  1039. if (maybePrivateBrandName || superClass || classDecorationsFlag.value !== 0) {
  1040. args.push(classDecorationsFlag);
  1041. }
  1042. if (maybePrivateBrandName) {
  1043. args.push(createPrivateBrandCheckClosure(maybePrivateBrandName));
  1044. } else if (superClass) {
  1045. args.push(_core.types.unaryExpression("void", _core.types.numericLiteral(0)));
  1046. }
  1047. if (superClass) args.push(superClass);
  1048. rhs = _core.types.callExpression(state.addHelper("applyDecs2305"), args);
  1049. }
  1050. }
  1051. if (version === "2023-11") {
  1052. if (maybePrivateBrandName || superClass || classDecorationsFlag.value !== 0) {
  1053. args.push(classDecorationsFlag);
  1054. }
  1055. if (maybePrivateBrandName) {
  1056. args.push(createPrivateBrandCheckClosure(maybePrivateBrandName));
  1057. } else if (superClass) {
  1058. args.push(_core.types.unaryExpression("void", _core.types.numericLiteral(0)));
  1059. }
  1060. if (superClass) args.push(superClass);
  1061. rhs = _core.types.callExpression(state.addHelper("applyDecs2311"), args);
  1062. }
  1063. if (elementLocals.length > 0) {
  1064. if (classLocals.length > 0) {
  1065. lhs = _core.types.objectPattern([_core.types.objectProperty(_core.types.identifier("e"), _core.types.arrayPattern(elementLocals)), _core.types.objectProperty(_core.types.identifier("c"), _core.types.arrayPattern(classLocals))]);
  1066. } else {
  1067. lhs = _core.types.arrayPattern(elementLocals);
  1068. rhs = _core.types.memberExpression(rhs, _core.types.identifier("e"), false, false);
  1069. }
  1070. } else {
  1071. lhs = _core.types.arrayPattern(classLocals);
  1072. rhs = _core.types.memberExpression(rhs, _core.types.identifier("c"), false, false);
  1073. }
  1074. return _core.types.assignmentExpression("=", lhs, rhs);
  1075. }
  1076. function isProtoKey(node) {
  1077. return node.type === "Identifier" ? node.name === "__proto__" : node.value === "__proto__";
  1078. }
  1079. function isDecorated(node) {
  1080. return node.decorators && node.decorators.length > 0;
  1081. }
  1082. function shouldTransformElement(node) {
  1083. switch (node.type) {
  1084. case "ClassAccessorProperty":
  1085. return true;
  1086. case "ClassMethod":
  1087. case "ClassProperty":
  1088. case "ClassPrivateMethod":
  1089. case "ClassPrivateProperty":
  1090. return isDecorated(node);
  1091. default:
  1092. return false;
  1093. }
  1094. }
  1095. function shouldTransformClass(node) {
  1096. return isDecorated(node) || node.body.body.some(shouldTransformElement);
  1097. }
  1098. function NamedEvaluationVisitoryFactory(isAnonymous, visitor) {
  1099. function handleComputedProperty(propertyPath, key, state) {
  1100. switch (key.type) {
  1101. case "StringLiteral":
  1102. return _core.types.stringLiteral(key.value);
  1103. case "NumericLiteral":
  1104. case "BigIntLiteral":
  1105. {
  1106. const keyValue = key.value + "";
  1107. propertyPath.get("key").replaceWith(_core.types.stringLiteral(keyValue));
  1108. return _core.types.stringLiteral(keyValue);
  1109. }
  1110. default:
  1111. {
  1112. const ref = propertyPath.scope.maybeGenerateMemoised(key);
  1113. propertyPath.get("key").replaceWith(_core.types.assignmentExpression("=", ref, createToPropertyKeyCall(state, key)));
  1114. return _core.types.cloneNode(ref);
  1115. }
  1116. }
  1117. }
  1118. return {
  1119. VariableDeclarator(path, state) {
  1120. const id = path.node.id;
  1121. if (id.type === "Identifier") {
  1122. const initializer = (0, _helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers)(path.get("init"));
  1123. if (isAnonymous(initializer)) {
  1124. const name = id.name;
  1125. visitor(initializer, state, name);
  1126. }
  1127. }
  1128. },
  1129. AssignmentExpression(path, state) {
  1130. const id = path.node.left;
  1131. if (id.type === "Identifier") {
  1132. const initializer = (0, _helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers)(path.get("right"));
  1133. if (isAnonymous(initializer)) {
  1134. switch (path.node.operator) {
  1135. case "=":
  1136. case "&&=":
  1137. case "||=":
  1138. case "??=":
  1139. visitor(initializer, state, id.name);
  1140. }
  1141. }
  1142. }
  1143. },
  1144. AssignmentPattern(path, state) {
  1145. const id = path.node.left;
  1146. if (id.type === "Identifier") {
  1147. const initializer = (0, _helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers)(path.get("right"));
  1148. if (isAnonymous(initializer)) {
  1149. const name = id.name;
  1150. visitor(initializer, state, name);
  1151. }
  1152. }
  1153. },
  1154. ObjectExpression(path, state) {
  1155. for (const propertyPath of path.get("properties")) {
  1156. if (!propertyPath.isObjectProperty()) continue;
  1157. const {
  1158. node
  1159. } = propertyPath;
  1160. const id = node.key;
  1161. const initializer = (0, _helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers)(propertyPath.get("value"));
  1162. if (isAnonymous(initializer)) {
  1163. if (!node.computed) {
  1164. if (!isProtoKey(id)) {
  1165. if (id.type === "Identifier") {
  1166. visitor(initializer, state, id.name);
  1167. } else {
  1168. const className = _core.types.stringLiteral(id.value + "");
  1169. visitor(initializer, state, className);
  1170. }
  1171. }
  1172. } else {
  1173. const ref = handleComputedProperty(propertyPath, id, state);
  1174. visitor(initializer, state, ref);
  1175. }
  1176. }
  1177. }
  1178. },
  1179. ClassPrivateProperty(path, state) {
  1180. const {
  1181. node
  1182. } = path;
  1183. const initializer = (0, _helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers)(path.get("value"));
  1184. if (isAnonymous(initializer)) {
  1185. const className = _core.types.stringLiteral("#" + node.key.id.name);
  1186. visitor(initializer, state, className);
  1187. }
  1188. },
  1189. ClassAccessorProperty(path, state) {
  1190. const {
  1191. node
  1192. } = path;
  1193. const id = node.key;
  1194. const initializer = (0, _helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers)(path.get("value"));
  1195. if (isAnonymous(initializer)) {
  1196. if (!node.computed) {
  1197. if (id.type === "Identifier") {
  1198. visitor(initializer, state, id.name);
  1199. } else if (id.type === "PrivateName") {
  1200. const className = _core.types.stringLiteral("#" + id.id.name);
  1201. visitor(initializer, state, className);
  1202. } else {
  1203. const className = _core.types.stringLiteral(id.value + "");
  1204. visitor(initializer, state, className);
  1205. }
  1206. } else {
  1207. const ref = handleComputedProperty(path, id, state);
  1208. visitor(initializer, state, ref);
  1209. }
  1210. }
  1211. },
  1212. ClassProperty(path, state) {
  1213. const {
  1214. node
  1215. } = path;
  1216. const id = node.key;
  1217. const initializer = (0, _helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers)(path.get("value"));
  1218. if (isAnonymous(initializer)) {
  1219. if (!node.computed) {
  1220. if (id.type === "Identifier") {
  1221. visitor(initializer, state, id.name);
  1222. } else {
  1223. const className = _core.types.stringLiteral(id.value + "");
  1224. visitor(initializer, state, className);
  1225. }
  1226. } else {
  1227. const ref = handleComputedProperty(path, id, state);
  1228. visitor(initializer, state, ref);
  1229. }
  1230. }
  1231. }
  1232. };
  1233. }
  1234. function isDecoratedAnonymousClassExpression(path) {
  1235. return path.isClassExpression({
  1236. id: null
  1237. }) && shouldTransformClass(path.node);
  1238. }
  1239. function generateLetUidIdentifier(scope, name) {
  1240. const id = scope.generateUidIdentifier(name);
  1241. scope.push({
  1242. id,
  1243. kind: "let"
  1244. });
  1245. return _core.types.cloneNode(id);
  1246. }
  1247. function _default({
  1248. assertVersion,
  1249. assumption
  1250. }, {
  1251. loose
  1252. }, version, inherits) {
  1253. var _assumption, _assumption2;
  1254. {
  1255. if (version === "2023-11" || version === "2023-05" || version === "2023-01") {
  1256. assertVersion("^7.21.0");
  1257. } else if (version === "2021-12") {
  1258. assertVersion("^7.16.0");
  1259. } else {
  1260. assertVersion("^7.19.0");
  1261. }
  1262. }
  1263. const VISITED = new WeakSet();
  1264. const constantSuper = (_assumption = assumption("constantSuper")) != null ? _assumption : loose;
  1265. const ignoreFunctionLength = (_assumption2 = assumption("ignoreFunctionLength")) != null ? _assumption2 : loose;
  1266. const namedEvaluationVisitor = NamedEvaluationVisitoryFactory(isDecoratedAnonymousClassExpression, visitClass);
  1267. function visitClass(path, state, className) {
  1268. var _node$id;
  1269. if (VISITED.has(path)) return;
  1270. const {
  1271. node
  1272. } = path;
  1273. className != null ? className : className = (_node$id = node.id) == null ? void 0 : _node$id.name;
  1274. const newPath = transformClass(path, state, constantSuper, ignoreFunctionLength, className, namedEvaluationVisitor, version);
  1275. if (newPath) {
  1276. VISITED.add(newPath);
  1277. return;
  1278. }
  1279. VISITED.add(path);
  1280. }
  1281. return {
  1282. name: "proposal-decorators",
  1283. inherits: inherits,
  1284. visitor: Object.assign({
  1285. ExportDefaultDeclaration(path, state) {
  1286. const {
  1287. declaration
  1288. } = path.node;
  1289. if ((declaration == null ? void 0 : declaration.type) === "ClassDeclaration" && isDecorated(declaration)) {
  1290. const isAnonymous = !declaration.id;
  1291. {
  1292. var _path$splitExportDecl;
  1293. (_path$splitExportDecl = path.splitExportDeclaration) != null ? _path$splitExportDecl : path.splitExportDeclaration = require("@babel/traverse").NodePath.prototype.splitExportDeclaration;
  1294. }
  1295. const updatedVarDeclarationPath = path.splitExportDeclaration();
  1296. if (isAnonymous) {
  1297. visitClass(updatedVarDeclarationPath, state, _core.types.stringLiteral("default"));
  1298. }
  1299. }
  1300. },
  1301. ExportNamedDeclaration(path) {
  1302. const {
  1303. declaration
  1304. } = path.node;
  1305. if ((declaration == null ? void 0 : declaration.type) === "ClassDeclaration" && isDecorated(declaration)) {
  1306. {
  1307. var _path$splitExportDecl2;
  1308. (_path$splitExportDecl2 = path.splitExportDeclaration) != null ? _path$splitExportDecl2 : path.splitExportDeclaration = require("@babel/traverse").NodePath.prototype.splitExportDeclaration;
  1309. }
  1310. path.splitExportDeclaration();
  1311. }
  1312. },
  1313. Class(path, state) {
  1314. visitClass(path, state, undefined);
  1315. }
  1316. }, namedEvaluationVisitor)
  1317. };
  1318. }
  1319. //# sourceMappingURL=decorators.js.map