index.js 13 KB

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