index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var _t = require('@babel/types');
  4. function _interopNamespace(e) {
  5. if (e && e.__esModule) return e;
  6. var n = Object.create(null);
  7. if (e) {
  8. Object.keys(e).forEach(function (k) {
  9. if (k !== 'default') {
  10. var d = Object.getOwnPropertyDescriptor(e, k);
  11. Object.defineProperty(n, k, d.get ? d : {
  12. enumerable: true,
  13. get: function () { return e[k]; }
  14. });
  15. }
  16. });
  17. }
  18. n.default = e;
  19. return Object.freeze(n);
  20. }
  21. var _t__namespace = /*#__PURE__*/_interopNamespace(_t);
  22. function willPathCastToBoolean(path) {
  23. const maybeWrapped = path;
  24. const {
  25. node,
  26. parentPath
  27. } = maybeWrapped;
  28. if (parentPath.isLogicalExpression()) {
  29. const {
  30. operator,
  31. right
  32. } = parentPath.node;
  33. if (operator === "&&" || operator === "||" || operator === "??" && node === right) {
  34. return willPathCastToBoolean(parentPath);
  35. }
  36. }
  37. if (parentPath.isSequenceExpression()) {
  38. const {
  39. expressions
  40. } = parentPath.node;
  41. if (expressions[expressions.length - 1] === node) {
  42. return willPathCastToBoolean(parentPath);
  43. } else {
  44. return true;
  45. }
  46. }
  47. return parentPath.isConditional({
  48. test: node
  49. }) || parentPath.isUnaryExpression({
  50. operator: "!"
  51. }) || parentPath.isLoop({
  52. test: node
  53. });
  54. }
  55. const {
  56. LOGICAL_OPERATORS,
  57. arrowFunctionExpression,
  58. assignmentExpression,
  59. binaryExpression,
  60. booleanLiteral,
  61. callExpression,
  62. cloneNode,
  63. conditionalExpression,
  64. identifier,
  65. isMemberExpression,
  66. isOptionalCallExpression,
  67. isOptionalMemberExpression,
  68. isUpdateExpression,
  69. logicalExpression,
  70. memberExpression,
  71. nullLiteral,
  72. optionalCallExpression,
  73. optionalMemberExpression,
  74. sequenceExpression,
  75. updateExpression
  76. } = _t__namespace;
  77. class AssignmentMemoiser {
  78. constructor() {
  79. this._map = void 0;
  80. this._map = new WeakMap();
  81. }
  82. has(key) {
  83. return this._map.has(key);
  84. }
  85. get(key) {
  86. if (!this.has(key)) return;
  87. const record = this._map.get(key);
  88. const {
  89. value
  90. } = record;
  91. record.count--;
  92. if (record.count === 0) {
  93. return assignmentExpression("=", value, key);
  94. }
  95. return value;
  96. }
  97. set(key, value, count) {
  98. return this._map.set(key, {
  99. count,
  100. value
  101. });
  102. }
  103. }
  104. function toNonOptional(path, base) {
  105. const {
  106. node
  107. } = path;
  108. if (isOptionalMemberExpression(node)) {
  109. return memberExpression(base, node.property, node.computed);
  110. }
  111. if (path.isOptionalCallExpression()) {
  112. const callee = path.get("callee");
  113. if (path.node.optional && callee.isOptionalMemberExpression()) {
  114. const object = callee.node.object;
  115. const context = path.scope.maybeGenerateMemoised(object);
  116. callee.get("object").replaceWith(assignmentExpression("=", context, object));
  117. return callExpression(memberExpression(base, identifier("call")), [context, ...path.node.arguments]);
  118. }
  119. return callExpression(base, path.node.arguments);
  120. }
  121. return path.node;
  122. }
  123. function isInDetachedTree(path) {
  124. while (path) {
  125. if (path.isProgram()) break;
  126. const {
  127. parentPath,
  128. container,
  129. listKey
  130. } = path;
  131. const parentNode = parentPath.node;
  132. if (listKey) {
  133. if (container !== parentNode[listKey]) {
  134. return true;
  135. }
  136. } else {
  137. if (container !== parentNode) return true;
  138. }
  139. path = parentPath;
  140. }
  141. return false;
  142. }
  143. const handle = {
  144. memoise() {},
  145. handle(member, noDocumentAll) {
  146. const {
  147. node,
  148. parent,
  149. parentPath,
  150. scope
  151. } = member;
  152. if (member.isOptionalMemberExpression()) {
  153. if (isInDetachedTree(member)) return;
  154. const endPath = member.find(({
  155. node,
  156. parent
  157. }) => {
  158. if (isOptionalMemberExpression(parent)) {
  159. return parent.optional || parent.object !== node;
  160. }
  161. if (isOptionalCallExpression(parent)) {
  162. return node !== member.node && parent.optional || parent.callee !== node;
  163. }
  164. return true;
  165. });
  166. if (scope.path.isPattern()) {
  167. endPath.replaceWith(callExpression(arrowFunctionExpression([], endPath.node), []));
  168. return;
  169. }
  170. const willEndPathCastToBoolean = willPathCastToBoolean(endPath);
  171. const rootParentPath = endPath.parentPath;
  172. if (rootParentPath.isUpdateExpression({
  173. argument: node
  174. })) {
  175. throw member.buildCodeFrameError(`can't handle update expression`);
  176. }
  177. const isAssignment = rootParentPath.isAssignmentExpression({
  178. left: endPath.node
  179. });
  180. const isDeleteOperation = rootParentPath.isUnaryExpression({
  181. operator: "delete"
  182. });
  183. if (isDeleteOperation && endPath.isOptionalMemberExpression() && endPath.get("property").isPrivateName()) {
  184. throw member.buildCodeFrameError(`can't delete a private class element`);
  185. }
  186. let startingOptional = member;
  187. for (;;) {
  188. if (startingOptional.isOptionalMemberExpression()) {
  189. if (startingOptional.node.optional) break;
  190. startingOptional = startingOptional.get("object");
  191. continue;
  192. } else if (startingOptional.isOptionalCallExpression()) {
  193. if (startingOptional.node.optional) break;
  194. startingOptional = startingOptional.get("callee");
  195. continue;
  196. }
  197. throw new Error(`Internal error: unexpected ${startingOptional.node.type}`);
  198. }
  199. const startingNode = startingOptional.isOptionalMemberExpression() ? startingOptional.node.object : startingOptional.node.callee;
  200. const baseNeedsMemoised = scope.maybeGenerateMemoised(startingNode);
  201. const baseRef = baseNeedsMemoised != null ? baseNeedsMemoised : startingNode;
  202. const parentIsOptionalCall = parentPath.isOptionalCallExpression({
  203. callee: node
  204. });
  205. const isOptionalCall = parent => parentIsOptionalCall;
  206. const parentIsCall = parentPath.isCallExpression({
  207. callee: node
  208. });
  209. startingOptional.replaceWith(toNonOptional(startingOptional, baseRef));
  210. if (isOptionalCall()) {
  211. if (parent.optional) {
  212. parentPath.replaceWith(this.optionalCall(member, parent.arguments));
  213. } else {
  214. parentPath.replaceWith(this.call(member, parent.arguments));
  215. }
  216. } else if (parentIsCall) {
  217. member.replaceWith(this.boundGet(member));
  218. } else if (this.delete && parentPath.isUnaryExpression({
  219. operator: "delete"
  220. })) {
  221. parentPath.replaceWith(this.delete(member));
  222. } else if (parentPath.isAssignmentExpression()) {
  223. handleAssignment(this, member, parentPath);
  224. } else {
  225. member.replaceWith(this.get(member));
  226. }
  227. let regular = member.node;
  228. for (let current = member; current !== endPath;) {
  229. const parentPath = current.parentPath;
  230. if (parentPath === endPath && isOptionalCall() && parent.optional) {
  231. regular = parentPath.node;
  232. break;
  233. }
  234. regular = toNonOptional(parentPath, regular);
  235. current = parentPath;
  236. }
  237. let context;
  238. const endParentPath = endPath.parentPath;
  239. if (isMemberExpression(regular) && endParentPath.isOptionalCallExpression({
  240. callee: endPath.node,
  241. optional: true
  242. })) {
  243. const {
  244. object
  245. } = regular;
  246. context = member.scope.maybeGenerateMemoised(object);
  247. if (context) {
  248. regular.object = assignmentExpression("=", context, object);
  249. }
  250. }
  251. let replacementPath = endPath;
  252. if (isDeleteOperation || isAssignment) {
  253. replacementPath = endParentPath;
  254. regular = endParentPath.node;
  255. }
  256. const baseMemoised = baseNeedsMemoised ? assignmentExpression("=", cloneNode(baseRef), cloneNode(startingNode)) : cloneNode(baseRef);
  257. if (willEndPathCastToBoolean) {
  258. let nonNullishCheck;
  259. if (noDocumentAll) {
  260. nonNullishCheck = binaryExpression("!=", baseMemoised, nullLiteral());
  261. } else {
  262. nonNullishCheck = logicalExpression("&&", binaryExpression("!==", baseMemoised, nullLiteral()), binaryExpression("!==", cloneNode(baseRef), scope.buildUndefinedNode()));
  263. }
  264. replacementPath.replaceWith(logicalExpression("&&", nonNullishCheck, regular));
  265. } else {
  266. let nullishCheck;
  267. if (noDocumentAll) {
  268. nullishCheck = binaryExpression("==", baseMemoised, nullLiteral());
  269. } else {
  270. nullishCheck = logicalExpression("||", binaryExpression("===", baseMemoised, nullLiteral()), binaryExpression("===", cloneNode(baseRef), scope.buildUndefinedNode()));
  271. }
  272. replacementPath.replaceWith(conditionalExpression(nullishCheck, isDeleteOperation ? booleanLiteral(true) : scope.buildUndefinedNode(), regular));
  273. }
  274. if (context) {
  275. const endParent = endParentPath.node;
  276. endParentPath.replaceWith(optionalCallExpression(optionalMemberExpression(endParent.callee, identifier("call"), false, true), [cloneNode(context), ...endParent.arguments], false));
  277. }
  278. return;
  279. }
  280. if (isUpdateExpression(parent, {
  281. argument: node
  282. })) {
  283. if (this.simpleSet) {
  284. member.replaceWith(this.simpleSet(member));
  285. return;
  286. }
  287. const {
  288. operator,
  289. prefix
  290. } = parent;
  291. this.memoise(member, 2);
  292. const ref = scope.generateUidIdentifierBasedOnNode(node);
  293. scope.push({
  294. id: ref
  295. });
  296. const seq = [assignmentExpression("=", cloneNode(ref), this.get(member))];
  297. if (prefix) {
  298. seq.push(updateExpression(operator, cloneNode(ref), prefix));
  299. const value = sequenceExpression(seq);
  300. parentPath.replaceWith(this.set(member, value));
  301. return;
  302. } else {
  303. const ref2 = scope.generateUidIdentifierBasedOnNode(node);
  304. scope.push({
  305. id: ref2
  306. });
  307. seq.push(assignmentExpression("=", cloneNode(ref2), updateExpression(operator, cloneNode(ref), prefix)), cloneNode(ref));
  308. const value = sequenceExpression(seq);
  309. parentPath.replaceWith(sequenceExpression([this.set(member, value), cloneNode(ref2)]));
  310. return;
  311. }
  312. }
  313. if (parentPath.isAssignmentExpression({
  314. left: node
  315. })) {
  316. handleAssignment(this, member, parentPath);
  317. return;
  318. }
  319. if (parentPath.isCallExpression({
  320. callee: node
  321. })) {
  322. parentPath.replaceWith(this.call(member, parentPath.node.arguments));
  323. return;
  324. }
  325. if (parentPath.isOptionalCallExpression({
  326. callee: node
  327. })) {
  328. if (scope.path.isPattern()) {
  329. parentPath.replaceWith(callExpression(arrowFunctionExpression([], parentPath.node), []));
  330. return;
  331. }
  332. parentPath.replaceWith(this.optionalCall(member, parentPath.node.arguments));
  333. return;
  334. }
  335. if (this.delete && parentPath.isUnaryExpression({
  336. operator: "delete"
  337. })) {
  338. parentPath.replaceWith(this.delete(member));
  339. return;
  340. }
  341. if (parentPath.isForXStatement({
  342. left: node
  343. }) || parentPath.isObjectProperty({
  344. value: node
  345. }) && parentPath.parentPath.isObjectPattern() || parentPath.isAssignmentPattern({
  346. left: node
  347. }) && parentPath.parentPath.isObjectProperty({
  348. value: parent
  349. }) && parentPath.parentPath.parentPath.isObjectPattern() || parentPath.isArrayPattern() || parentPath.isAssignmentPattern({
  350. left: node
  351. }) && parentPath.parentPath.isArrayPattern() || parentPath.isRestElement()) {
  352. member.replaceWith(this.destructureSet(member));
  353. return;
  354. }
  355. if (parentPath.isTaggedTemplateExpression()) {
  356. member.replaceWith(this.boundGet(member));
  357. } else {
  358. member.replaceWith(this.get(member));
  359. }
  360. }
  361. };
  362. function handleAssignment(state, member, parentPath) {
  363. if (state.simpleSet) {
  364. member.replaceWith(state.simpleSet(member));
  365. return;
  366. }
  367. const {
  368. operator,
  369. right: value
  370. } = parentPath.node;
  371. if (operator === "=") {
  372. parentPath.replaceWith(state.set(member, value));
  373. } else {
  374. const operatorTrunc = operator.slice(0, -1);
  375. if (LOGICAL_OPERATORS.includes(operatorTrunc)) {
  376. state.memoise(member, 1);
  377. parentPath.replaceWith(logicalExpression(operatorTrunc, state.get(member), state.set(member, value)));
  378. } else {
  379. state.memoise(member, 2);
  380. parentPath.replaceWith(state.set(member, binaryExpression(operatorTrunc, state.get(member), value)));
  381. }
  382. }
  383. }
  384. function memberExpressionToFunctions(path, visitor, state) {
  385. path.traverse(visitor, Object.assign({}, handle, state, {
  386. memoiser: new AssignmentMemoiser()
  387. }));
  388. }
  389. exports.default = memberExpressionToFunctions;
  390. //# sourceMappingURL=index.js.map