fields.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.buildFieldsInitNodes = buildFieldsInitNodes;
  6. exports.buildPrivateNamesMap = buildPrivateNamesMap;
  7. exports.buildPrivateNamesNodes = buildPrivateNamesNodes;
  8. exports.transformPrivateNamesUsage = transformPrivateNamesUsage;
  9. var _core = require("@babel/core");
  10. var _helperReplaceSupers = require("@babel/helper-replace-supers");
  11. var _helperEnvironmentVisitor = require("@babel/helper-environment-visitor");
  12. var _helperMemberExpressionToFunctions = require("@babel/helper-member-expression-to-functions");
  13. var _helperOptimiseCallExpression = require("@babel/helper-optimise-call-expression");
  14. var _helperAnnotateAsPure = require("@babel/helper-annotate-as-pure");
  15. var ts = require("./typescript");
  16. function buildPrivateNamesMap(props) {
  17. const privateNamesMap = new Map();
  18. for (const prop of props) {
  19. if (prop.isPrivate()) {
  20. const {
  21. name
  22. } = prop.node.key.id;
  23. const update = privateNamesMap.has(name) ? privateNamesMap.get(name) : {
  24. id: prop.scope.generateUidIdentifier(name),
  25. static: prop.node.static,
  26. method: !prop.isProperty()
  27. };
  28. if (prop.isClassPrivateMethod()) {
  29. if (prop.node.kind === "get") {
  30. update.getId = prop.scope.generateUidIdentifier(`get_${name}`);
  31. } else if (prop.node.kind === "set") {
  32. update.setId = prop.scope.generateUidIdentifier(`set_${name}`);
  33. } else if (prop.node.kind === "method") {
  34. update.methodId = prop.scope.generateUidIdentifier(name);
  35. }
  36. }
  37. privateNamesMap.set(name, update);
  38. }
  39. }
  40. return privateNamesMap;
  41. }
  42. function buildPrivateNamesNodes(privateNamesMap, privateFieldsAsProperties, state) {
  43. const initNodes = [];
  44. for (const [name, value] of privateNamesMap) {
  45. const {
  46. static: isStatic,
  47. method: isMethod,
  48. getId,
  49. setId
  50. } = value;
  51. const isAccessor = getId || setId;
  52. const id = _core.types.cloneNode(value.id);
  53. let init;
  54. if (privateFieldsAsProperties) {
  55. init = _core.types.callExpression(state.addHelper("classPrivateFieldLooseKey"), [_core.types.stringLiteral(name)]);
  56. } else if (!isStatic) {
  57. init = _core.types.newExpression(_core.types.identifier(!isMethod || isAccessor ? "WeakMap" : "WeakSet"), []);
  58. }
  59. if (init) {
  60. (0, _helperAnnotateAsPure.default)(init);
  61. initNodes.push(_core.template.statement.ast`var ${id} = ${init}`);
  62. }
  63. }
  64. return initNodes;
  65. }
  66. function privateNameVisitorFactory(visitor) {
  67. const privateNameVisitor = Object.assign({}, visitor, {
  68. Class(path) {
  69. const {
  70. privateNamesMap
  71. } = this;
  72. const body = path.get("body.body");
  73. const visiblePrivateNames = new Map(privateNamesMap);
  74. const redeclared = [];
  75. for (const prop of body) {
  76. if (!prop.isPrivate()) continue;
  77. const {
  78. name
  79. } = prop.node.key.id;
  80. visiblePrivateNames.delete(name);
  81. redeclared.push(name);
  82. }
  83. if (!redeclared.length) {
  84. return;
  85. }
  86. path.get("body").traverse(nestedVisitor, Object.assign({}, this, {
  87. redeclared
  88. }));
  89. path.traverse(privateNameVisitor, Object.assign({}, this, {
  90. privateNamesMap: visiblePrivateNames
  91. }));
  92. path.skipKey("body");
  93. }
  94. });
  95. const nestedVisitor = _core.traverse.visitors.merge([Object.assign({}, visitor), _helperEnvironmentVisitor.default]);
  96. return privateNameVisitor;
  97. }
  98. const privateNameVisitor = privateNameVisitorFactory({
  99. PrivateName(path, {
  100. noDocumentAll
  101. }) {
  102. const {
  103. privateNamesMap,
  104. redeclared
  105. } = this;
  106. const {
  107. node,
  108. parentPath
  109. } = path;
  110. if (!parentPath.isMemberExpression({
  111. property: node
  112. }) && !parentPath.isOptionalMemberExpression({
  113. property: node
  114. })) {
  115. return;
  116. }
  117. const {
  118. name
  119. } = node.id;
  120. if (!privateNamesMap.has(name)) return;
  121. if (redeclared && redeclared.includes(name)) return;
  122. this.handle(parentPath, noDocumentAll);
  123. }
  124. });
  125. function unshadow(name, scope, innerBinding) {
  126. while ((_scope = scope) != null && _scope.hasBinding(name) && !scope.bindingIdentifierEquals(name, innerBinding)) {
  127. var _scope;
  128. scope.rename(name);
  129. scope = scope.parent;
  130. }
  131. }
  132. const privateInVisitor = privateNameVisitorFactory({
  133. BinaryExpression(path) {
  134. const {
  135. operator,
  136. left,
  137. right
  138. } = path.node;
  139. if (operator !== "in") return;
  140. if (!_core.types.isPrivateName(left)) return;
  141. const {
  142. privateFieldsAsProperties,
  143. privateNamesMap,
  144. redeclared
  145. } = this;
  146. const {
  147. name
  148. } = left.id;
  149. if (!privateNamesMap.has(name)) return;
  150. if (redeclared && redeclared.includes(name)) return;
  151. unshadow(this.classRef.name, path.scope, this.innerBinding);
  152. if (privateFieldsAsProperties) {
  153. const {
  154. id
  155. } = privateNamesMap.get(name);
  156. path.replaceWith(_core.template.expression.ast`
  157. Object.prototype.hasOwnProperty.call(${right}, ${_core.types.cloneNode(id)})
  158. `);
  159. return;
  160. }
  161. const {
  162. id,
  163. static: isStatic
  164. } = privateNamesMap.get(name);
  165. if (isStatic) {
  166. path.replaceWith(_core.template.expression.ast`${right} === ${this.classRef}`);
  167. return;
  168. }
  169. path.replaceWith(_core.template.expression.ast`${_core.types.cloneNode(id)}.has(${right})`);
  170. }
  171. });
  172. const privateNameHandlerSpec = {
  173. memoise(member, count) {
  174. const {
  175. scope
  176. } = member;
  177. const {
  178. object
  179. } = member.node;
  180. const memo = scope.maybeGenerateMemoised(object);
  181. if (!memo) {
  182. return;
  183. }
  184. this.memoiser.set(object, memo, count);
  185. },
  186. receiver(member) {
  187. const {
  188. object
  189. } = member.node;
  190. if (this.memoiser.has(object)) {
  191. return _core.types.cloneNode(this.memoiser.get(object));
  192. }
  193. return _core.types.cloneNode(object);
  194. },
  195. get(member) {
  196. const {
  197. classRef,
  198. privateNamesMap,
  199. file,
  200. innerBinding
  201. } = this;
  202. const {
  203. name
  204. } = member.node.property.id;
  205. const {
  206. id,
  207. static: isStatic,
  208. method: isMethod,
  209. methodId,
  210. getId,
  211. setId
  212. } = privateNamesMap.get(name);
  213. const isAccessor = getId || setId;
  214. if (isStatic) {
  215. const helperName = isMethod && !isAccessor ? "classStaticPrivateMethodGet" : "classStaticPrivateFieldSpecGet";
  216. unshadow(classRef.name, member.scope, innerBinding);
  217. return _core.types.callExpression(file.addHelper(helperName), [this.receiver(member), _core.types.cloneNode(classRef), _core.types.cloneNode(id)]);
  218. }
  219. if (isMethod) {
  220. if (isAccessor) {
  221. if (!getId && setId) {
  222. if (file.availableHelper("writeOnlyError")) {
  223. return _core.types.sequenceExpression([this.receiver(member), _core.types.callExpression(file.addHelper("writeOnlyError"), [_core.types.stringLiteral(`#${name}`)])]);
  224. }
  225. console.warn(`@babel/helpers is outdated, update it to silence this warning.`);
  226. }
  227. return _core.types.callExpression(file.addHelper("classPrivateFieldGet"), [this.receiver(member), _core.types.cloneNode(id)]);
  228. }
  229. return _core.types.callExpression(file.addHelper("classPrivateMethodGet"), [this.receiver(member), _core.types.cloneNode(id), _core.types.cloneNode(methodId)]);
  230. }
  231. return _core.types.callExpression(file.addHelper("classPrivateFieldGet"), [this.receiver(member), _core.types.cloneNode(id)]);
  232. },
  233. boundGet(member) {
  234. this.memoise(member, 1);
  235. return _core.types.callExpression(_core.types.memberExpression(this.get(member), _core.types.identifier("bind")), [this.receiver(member)]);
  236. },
  237. set(member, value) {
  238. const {
  239. classRef,
  240. privateNamesMap,
  241. file
  242. } = this;
  243. const {
  244. name
  245. } = member.node.property.id;
  246. const {
  247. id,
  248. static: isStatic,
  249. method: isMethod,
  250. setId,
  251. getId
  252. } = privateNamesMap.get(name);
  253. const isAccessor = getId || setId;
  254. if (isStatic) {
  255. const helperName = isMethod && !isAccessor ? "classStaticPrivateMethodSet" : "classStaticPrivateFieldSpecSet";
  256. return _core.types.callExpression(file.addHelper(helperName), [this.receiver(member), _core.types.cloneNode(classRef), _core.types.cloneNode(id), value]);
  257. }
  258. if (isMethod) {
  259. if (setId) {
  260. return _core.types.callExpression(file.addHelper("classPrivateFieldSet"), [this.receiver(member), _core.types.cloneNode(id), value]);
  261. }
  262. return _core.types.sequenceExpression([this.receiver(member), value, _core.types.callExpression(file.addHelper("readOnlyError"), [_core.types.stringLiteral(`#${name}`)])]);
  263. }
  264. return _core.types.callExpression(file.addHelper("classPrivateFieldSet"), [this.receiver(member), _core.types.cloneNode(id), value]);
  265. },
  266. destructureSet(member) {
  267. const {
  268. classRef,
  269. privateNamesMap,
  270. file
  271. } = this;
  272. const {
  273. name
  274. } = member.node.property.id;
  275. const {
  276. id,
  277. static: isStatic
  278. } = privateNamesMap.get(name);
  279. if (isStatic) {
  280. try {
  281. var helper = file.addHelper("classStaticPrivateFieldDestructureSet");
  282. } catch (_unused) {
  283. throw new Error("Babel can not transpile `[C.#p] = [0]` with @babel/helpers < 7.13.10, \n" + "please update @babel/helpers to the latest version.");
  284. }
  285. return _core.types.memberExpression(_core.types.callExpression(helper, [this.receiver(member), _core.types.cloneNode(classRef), _core.types.cloneNode(id)]), _core.types.identifier("value"));
  286. }
  287. return _core.types.memberExpression(_core.types.callExpression(file.addHelper("classPrivateFieldDestructureSet"), [this.receiver(member), _core.types.cloneNode(id)]), _core.types.identifier("value"));
  288. },
  289. call(member, args) {
  290. this.memoise(member, 1);
  291. return (0, _helperOptimiseCallExpression.default)(this.get(member), this.receiver(member), args, false);
  292. },
  293. optionalCall(member, args) {
  294. this.memoise(member, 1);
  295. return (0, _helperOptimiseCallExpression.default)(this.get(member), this.receiver(member), args, true);
  296. }
  297. };
  298. const privateNameHandlerLoose = {
  299. get(member) {
  300. const {
  301. privateNamesMap,
  302. file
  303. } = this;
  304. const {
  305. object
  306. } = member.node;
  307. const {
  308. name
  309. } = member.node.property.id;
  310. return _core.template.expression`BASE(REF, PROP)[PROP]`({
  311. BASE: file.addHelper("classPrivateFieldLooseBase"),
  312. REF: _core.types.cloneNode(object),
  313. PROP: _core.types.cloneNode(privateNamesMap.get(name).id)
  314. });
  315. },
  316. set() {
  317. throw new Error("private name handler with loose = true don't need set()");
  318. },
  319. boundGet(member) {
  320. return _core.types.callExpression(_core.types.memberExpression(this.get(member), _core.types.identifier("bind")), [_core.types.cloneNode(member.node.object)]);
  321. },
  322. simpleSet(member) {
  323. return this.get(member);
  324. },
  325. destructureSet(member) {
  326. return this.get(member);
  327. },
  328. call(member, args) {
  329. return _core.types.callExpression(this.get(member), args);
  330. },
  331. optionalCall(member, args) {
  332. return _core.types.optionalCallExpression(this.get(member), args, true);
  333. }
  334. };
  335. function transformPrivateNamesUsage(ref, path, privateNamesMap, {
  336. privateFieldsAsProperties,
  337. noDocumentAll,
  338. innerBinding
  339. }, state) {
  340. if (!privateNamesMap.size) return;
  341. const body = path.get("body");
  342. const handler = privateFieldsAsProperties ? privateNameHandlerLoose : privateNameHandlerSpec;
  343. (0, _helperMemberExpressionToFunctions.default)(body, privateNameVisitor, Object.assign({
  344. privateNamesMap,
  345. classRef: ref,
  346. file: state
  347. }, handler, {
  348. noDocumentAll,
  349. innerBinding
  350. }));
  351. body.traverse(privateInVisitor, {
  352. privateNamesMap,
  353. classRef: ref,
  354. file: state,
  355. privateFieldsAsProperties,
  356. innerBinding
  357. });
  358. }
  359. function buildPrivateFieldInitLoose(ref, prop, privateNamesMap) {
  360. const {
  361. id
  362. } = privateNamesMap.get(prop.node.key.id.name);
  363. const value = prop.node.value || prop.scope.buildUndefinedNode();
  364. return _core.template.statement.ast`
  365. Object.defineProperty(${ref}, ${_core.types.cloneNode(id)}, {
  366. // configurable is false by default
  367. // enumerable is false by default
  368. writable: true,
  369. value: ${value}
  370. });
  371. `;
  372. }
  373. function buildPrivateInstanceFieldInitSpec(ref, prop, privateNamesMap, state) {
  374. const {
  375. id
  376. } = privateNamesMap.get(prop.node.key.id.name);
  377. const value = prop.node.value || prop.scope.buildUndefinedNode();
  378. {
  379. if (!state.availableHelper("classPrivateFieldInitSpec")) {
  380. return _core.template.statement.ast`${_core.types.cloneNode(id)}.set(${ref}, {
  381. // configurable is always false for private elements
  382. // enumerable is always false for private elements
  383. writable: true,
  384. value: ${value},
  385. })`;
  386. }
  387. }
  388. const helper = state.addHelper("classPrivateFieldInitSpec");
  389. return _core.template.statement.ast`${helper}(
  390. ${_core.types.thisExpression()},
  391. ${_core.types.cloneNode(id)},
  392. {
  393. writable: true,
  394. value: ${value}
  395. },
  396. )`;
  397. }
  398. function buildPrivateStaticFieldInitSpec(prop, privateNamesMap) {
  399. const privateName = privateNamesMap.get(prop.node.key.id.name);
  400. const {
  401. id,
  402. getId,
  403. setId,
  404. initAdded
  405. } = privateName;
  406. const isAccessor = getId || setId;
  407. if (!prop.isProperty() && (initAdded || !isAccessor)) return;
  408. if (isAccessor) {
  409. privateNamesMap.set(prop.node.key.id.name, Object.assign({}, privateName, {
  410. initAdded: true
  411. }));
  412. return _core.template.statement.ast`
  413. var ${_core.types.cloneNode(id)} = {
  414. // configurable is false by default
  415. // enumerable is false by default
  416. // writable is false by default
  417. get: ${getId ? getId.name : prop.scope.buildUndefinedNode()},
  418. set: ${setId ? setId.name : prop.scope.buildUndefinedNode()}
  419. }
  420. `;
  421. }
  422. const value = prop.node.value || prop.scope.buildUndefinedNode();
  423. return _core.template.statement.ast`
  424. var ${_core.types.cloneNode(id)} = {
  425. // configurable is false by default
  426. // enumerable is false by default
  427. writable: true,
  428. value: ${value}
  429. };
  430. `;
  431. }
  432. function buildPrivateMethodInitLoose(ref, prop, privateNamesMap) {
  433. const privateName = privateNamesMap.get(prop.node.key.id.name);
  434. const {
  435. methodId,
  436. id,
  437. getId,
  438. setId,
  439. initAdded
  440. } = privateName;
  441. if (initAdded) return;
  442. if (methodId) {
  443. return _core.template.statement.ast`
  444. Object.defineProperty(${ref}, ${id}, {
  445. // configurable is false by default
  446. // enumerable is false by default
  447. // writable is false by default
  448. value: ${methodId.name}
  449. });
  450. `;
  451. }
  452. const isAccessor = getId || setId;
  453. if (isAccessor) {
  454. privateNamesMap.set(prop.node.key.id.name, Object.assign({}, privateName, {
  455. initAdded: true
  456. }));
  457. return _core.template.statement.ast`
  458. Object.defineProperty(${ref}, ${id}, {
  459. // configurable is false by default
  460. // enumerable is false by default
  461. // writable is false by default
  462. get: ${getId ? getId.name : prop.scope.buildUndefinedNode()},
  463. set: ${setId ? setId.name : prop.scope.buildUndefinedNode()}
  464. });
  465. `;
  466. }
  467. }
  468. function buildPrivateInstanceMethodInitSpec(ref, prop, privateNamesMap, state) {
  469. const privateName = privateNamesMap.get(prop.node.key.id.name);
  470. const {
  471. getId,
  472. setId,
  473. initAdded
  474. } = privateName;
  475. if (initAdded) return;
  476. const isAccessor = getId || setId;
  477. if (isAccessor) {
  478. return buildPrivateAccessorInitialization(ref, prop, privateNamesMap, state);
  479. }
  480. return buildPrivateInstanceMethodInitalization(ref, prop, privateNamesMap, state);
  481. }
  482. function buildPrivateAccessorInitialization(ref, prop, privateNamesMap, state) {
  483. const privateName = privateNamesMap.get(prop.node.key.id.name);
  484. const {
  485. id,
  486. getId,
  487. setId
  488. } = privateName;
  489. privateNamesMap.set(prop.node.key.id.name, Object.assign({}, privateName, {
  490. initAdded: true
  491. }));
  492. {
  493. if (!state.availableHelper("classPrivateFieldInitSpec")) {
  494. return _core.template.statement.ast`
  495. ${id}.set(${ref}, {
  496. get: ${getId ? getId.name : prop.scope.buildUndefinedNode()},
  497. set: ${setId ? setId.name : prop.scope.buildUndefinedNode()}
  498. });
  499. `;
  500. }
  501. }
  502. const helper = state.addHelper("classPrivateFieldInitSpec");
  503. return _core.template.statement.ast`${helper}(
  504. ${_core.types.thisExpression()},
  505. ${_core.types.cloneNode(id)},
  506. {
  507. get: ${getId ? getId.name : prop.scope.buildUndefinedNode()},
  508. set: ${setId ? setId.name : prop.scope.buildUndefinedNode()}
  509. },
  510. )`;
  511. }
  512. function buildPrivateInstanceMethodInitalization(ref, prop, privateNamesMap, state) {
  513. const privateName = privateNamesMap.get(prop.node.key.id.name);
  514. const {
  515. id
  516. } = privateName;
  517. {
  518. if (!state.availableHelper("classPrivateMethodInitSpec")) {
  519. return _core.template.statement.ast`${id}.add(${ref})`;
  520. }
  521. }
  522. const helper = state.addHelper("classPrivateMethodInitSpec");
  523. return _core.template.statement.ast`${helper}(
  524. ${_core.types.thisExpression()},
  525. ${_core.types.cloneNode(id)}
  526. )`;
  527. }
  528. function buildPublicFieldInitLoose(ref, prop) {
  529. const {
  530. key,
  531. computed
  532. } = prop.node;
  533. const value = prop.node.value || prop.scope.buildUndefinedNode();
  534. return _core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.memberExpression(ref, key, computed || _core.types.isLiteral(key)), value));
  535. }
  536. function buildPublicFieldInitSpec(ref, prop, state) {
  537. const {
  538. key,
  539. computed
  540. } = prop.node;
  541. const value = prop.node.value || prop.scope.buildUndefinedNode();
  542. return _core.types.expressionStatement(_core.types.callExpression(state.addHelper("defineProperty"), [ref, computed || _core.types.isLiteral(key) ? key : _core.types.stringLiteral(key.name), value]));
  543. }
  544. function buildPrivateStaticMethodInitLoose(ref, prop, state, privateNamesMap) {
  545. const privateName = privateNamesMap.get(prop.node.key.id.name);
  546. const {
  547. id,
  548. methodId,
  549. getId,
  550. setId,
  551. initAdded
  552. } = privateName;
  553. if (initAdded) return;
  554. const isAccessor = getId || setId;
  555. if (isAccessor) {
  556. privateNamesMap.set(prop.node.key.id.name, Object.assign({}, privateName, {
  557. initAdded: true
  558. }));
  559. return _core.template.statement.ast`
  560. Object.defineProperty(${ref}, ${id}, {
  561. // configurable is false by default
  562. // enumerable is false by default
  563. // writable is false by default
  564. get: ${getId ? getId.name : prop.scope.buildUndefinedNode()},
  565. set: ${setId ? setId.name : prop.scope.buildUndefinedNode()}
  566. })
  567. `;
  568. }
  569. return _core.template.statement.ast`
  570. Object.defineProperty(${ref}, ${id}, {
  571. // configurable is false by default
  572. // enumerable is false by default
  573. // writable is false by default
  574. value: ${methodId.name}
  575. });
  576. `;
  577. }
  578. function buildPrivateMethodDeclaration(prop, privateNamesMap, privateFieldsAsProperties = false) {
  579. const privateName = privateNamesMap.get(prop.node.key.id.name);
  580. const {
  581. id,
  582. methodId,
  583. getId,
  584. setId,
  585. getterDeclared,
  586. setterDeclared,
  587. static: isStatic
  588. } = privateName;
  589. const {
  590. params,
  591. body,
  592. generator,
  593. async
  594. } = prop.node;
  595. const isGetter = getId && !getterDeclared && params.length === 0;
  596. const isSetter = setId && !setterDeclared && params.length > 0;
  597. let declId = methodId;
  598. if (isGetter) {
  599. privateNamesMap.set(prop.node.key.id.name, Object.assign({}, privateName, {
  600. getterDeclared: true
  601. }));
  602. declId = getId;
  603. } else if (isSetter) {
  604. privateNamesMap.set(prop.node.key.id.name, Object.assign({}, privateName, {
  605. setterDeclared: true
  606. }));
  607. declId = setId;
  608. } else if (isStatic && !privateFieldsAsProperties) {
  609. declId = id;
  610. }
  611. return _core.types.functionDeclaration(_core.types.cloneNode(declId), params, body, generator, async);
  612. }
  613. const thisContextVisitor = _core.traverse.visitors.merge([{
  614. ThisExpression(path, state) {
  615. state.needsClassRef = true;
  616. path.replaceWith(_core.types.cloneNode(state.classRef));
  617. },
  618. MetaProperty(path) {
  619. const meta = path.get("meta");
  620. const property = path.get("property");
  621. const {
  622. scope
  623. } = path;
  624. if (meta.isIdentifier({
  625. name: "new"
  626. }) && property.isIdentifier({
  627. name: "target"
  628. })) {
  629. path.replaceWith(scope.buildUndefinedNode());
  630. }
  631. }
  632. }, _helperEnvironmentVisitor.default]);
  633. const innerReferencesVisitor = {
  634. ReferencedIdentifier(path, state) {
  635. if (path.scope.bindingIdentifierEquals(path.node.name, state.innerBinding)) {
  636. state.needsClassRef = true;
  637. path.node.name = state.classRef.name;
  638. }
  639. }
  640. };
  641. function replaceThisContext(path, ref, getSuperRef, file, isStaticBlock, constantSuper, innerBindingRef) {
  642. var _state$classRef;
  643. const state = {
  644. classRef: ref,
  645. needsClassRef: false,
  646. innerBinding: innerBindingRef
  647. };
  648. const replacer = new _helperReplaceSupers.default({
  649. methodPath: path,
  650. constantSuper,
  651. file,
  652. refToPreserve: ref,
  653. getSuperRef,
  654. getObjectRef() {
  655. state.needsClassRef = true;
  656. return _core.types.isStaticBlock != null && _core.types.isStaticBlock(path.node) || path.node.static ? ref : _core.types.memberExpression(ref, _core.types.identifier("prototype"));
  657. }
  658. });
  659. replacer.replace();
  660. if (isStaticBlock || path.isProperty()) {
  661. path.traverse(thisContextVisitor, state);
  662. }
  663. if (innerBindingRef != null && (_state$classRef = state.classRef) != null && _state$classRef.name && state.classRef.name !== (innerBindingRef == null ? void 0 : innerBindingRef.name)) {
  664. path.traverse(innerReferencesVisitor, state);
  665. }
  666. return state.needsClassRef;
  667. }
  668. function isNameOrLength({
  669. key,
  670. computed
  671. }) {
  672. if (key.type === "Identifier") {
  673. return !computed && (key.name === "name" || key.name === "length");
  674. }
  675. if (key.type === "StringLiteral") {
  676. return key.value === "name" || key.value === "length";
  677. }
  678. return false;
  679. }
  680. function buildFieldsInitNodes(ref, superRef, props, privateNamesMap, state, setPublicClassFields, privateFieldsAsProperties, constantSuper, innerBindingRef) {
  681. let needsClassRef = false;
  682. let injectSuperRef;
  683. const staticNodes = [];
  684. const instanceNodes = [];
  685. const pureStaticNodes = [];
  686. const getSuperRef = _core.types.isIdentifier(superRef) ? () => superRef : () => {
  687. var _injectSuperRef;
  688. (_injectSuperRef = injectSuperRef) != null ? _injectSuperRef : injectSuperRef = props[0].scope.generateUidIdentifierBasedOnNode(superRef);
  689. return injectSuperRef;
  690. };
  691. for (const prop of props) {
  692. prop.isClassProperty() && ts.assertFieldTransformed(prop);
  693. const isStatic = !(_core.types.isStaticBlock != null && _core.types.isStaticBlock(prop.node)) && prop.node.static;
  694. const isInstance = !isStatic;
  695. const isPrivate = prop.isPrivate();
  696. const isPublic = !isPrivate;
  697. const isField = prop.isProperty();
  698. const isMethod = !isField;
  699. const isStaticBlock = prop.isStaticBlock == null ? void 0 : prop.isStaticBlock();
  700. if (isStatic || isMethod && isPrivate || isStaticBlock) {
  701. const replaced = replaceThisContext(prop, ref, getSuperRef, state, isStaticBlock, constantSuper, innerBindingRef);
  702. needsClassRef = needsClassRef || replaced;
  703. }
  704. switch (true) {
  705. case isStaticBlock:
  706. {
  707. const blockBody = prop.node.body;
  708. if (blockBody.length === 1 && _core.types.isExpressionStatement(blockBody[0])) {
  709. staticNodes.push(blockBody[0]);
  710. } else {
  711. staticNodes.push(_core.template.statement.ast`(() => { ${blockBody} })()`);
  712. }
  713. break;
  714. }
  715. case isStatic && isPrivate && isField && privateFieldsAsProperties:
  716. needsClassRef = true;
  717. staticNodes.push(buildPrivateFieldInitLoose(_core.types.cloneNode(ref), prop, privateNamesMap));
  718. break;
  719. case isStatic && isPrivate && isField && !privateFieldsAsProperties:
  720. needsClassRef = true;
  721. staticNodes.push(buildPrivateStaticFieldInitSpec(prop, privateNamesMap));
  722. break;
  723. case isStatic && isPublic && isField && setPublicClassFields:
  724. if (!isNameOrLength(prop.node)) {
  725. needsClassRef = true;
  726. staticNodes.push(buildPublicFieldInitLoose(_core.types.cloneNode(ref), prop));
  727. break;
  728. }
  729. case isStatic && isPublic && isField && !setPublicClassFields:
  730. needsClassRef = true;
  731. staticNodes.push(buildPublicFieldInitSpec(_core.types.cloneNode(ref), prop, state));
  732. break;
  733. case isInstance && isPrivate && isField && privateFieldsAsProperties:
  734. instanceNodes.push(buildPrivateFieldInitLoose(_core.types.thisExpression(), prop, privateNamesMap));
  735. break;
  736. case isInstance && isPrivate && isField && !privateFieldsAsProperties:
  737. instanceNodes.push(buildPrivateInstanceFieldInitSpec(_core.types.thisExpression(), prop, privateNamesMap, state));
  738. break;
  739. case isInstance && isPrivate && isMethod && privateFieldsAsProperties:
  740. instanceNodes.unshift(buildPrivateMethodInitLoose(_core.types.thisExpression(), prop, privateNamesMap));
  741. pureStaticNodes.push(buildPrivateMethodDeclaration(prop, privateNamesMap, privateFieldsAsProperties));
  742. break;
  743. case isInstance && isPrivate && isMethod && !privateFieldsAsProperties:
  744. instanceNodes.unshift(buildPrivateInstanceMethodInitSpec(_core.types.thisExpression(), prop, privateNamesMap, state));
  745. pureStaticNodes.push(buildPrivateMethodDeclaration(prop, privateNamesMap, privateFieldsAsProperties));
  746. break;
  747. case isStatic && isPrivate && isMethod && !privateFieldsAsProperties:
  748. needsClassRef = true;
  749. staticNodes.unshift(buildPrivateStaticFieldInitSpec(prop, privateNamesMap));
  750. pureStaticNodes.push(buildPrivateMethodDeclaration(prop, privateNamesMap, privateFieldsAsProperties));
  751. break;
  752. case isStatic && isPrivate && isMethod && privateFieldsAsProperties:
  753. needsClassRef = true;
  754. staticNodes.unshift(buildPrivateStaticMethodInitLoose(_core.types.cloneNode(ref), prop, state, privateNamesMap));
  755. pureStaticNodes.push(buildPrivateMethodDeclaration(prop, privateNamesMap, privateFieldsAsProperties));
  756. break;
  757. case isInstance && isPublic && isField && setPublicClassFields:
  758. instanceNodes.push(buildPublicFieldInitLoose(_core.types.thisExpression(), prop));
  759. break;
  760. case isInstance && isPublic && isField && !setPublicClassFields:
  761. instanceNodes.push(buildPublicFieldInitSpec(_core.types.thisExpression(), prop, state));
  762. break;
  763. default:
  764. throw new Error("Unreachable.");
  765. }
  766. }
  767. return {
  768. staticNodes: staticNodes.filter(Boolean),
  769. instanceNodes: instanceNodes.filter(Boolean),
  770. pureStaticNodes: pureStaticNodes.filter(Boolean),
  771. wrapClass(path) {
  772. for (const prop of props) {
  773. prop.remove();
  774. }
  775. if (injectSuperRef) {
  776. path.scope.push({
  777. id: _core.types.cloneNode(injectSuperRef)
  778. });
  779. path.set("superClass", _core.types.assignmentExpression("=", injectSuperRef, path.node.superClass));
  780. }
  781. if (!needsClassRef) return path;
  782. if (path.isClassExpression()) {
  783. path.scope.push({
  784. id: ref
  785. });
  786. path.replaceWith(_core.types.assignmentExpression("=", _core.types.cloneNode(ref), path.node));
  787. } else if (!path.node.id) {
  788. path.node.id = ref;
  789. }
  790. return path;
  791. }
  792. };
  793. }