index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. }) || rootParentPath.isAssignmentExpression({
  175. left: node
  176. })) {
  177. throw member.buildCodeFrameError(`can't handle assignment`);
  178. }
  179. const isDeleteOperation = rootParentPath.isUnaryExpression({
  180. operator: "delete"
  181. });
  182. if (isDeleteOperation && endPath.isOptionalMemberExpression() && endPath.get("property").isPrivateName()) {
  183. throw member.buildCodeFrameError(`can't delete a private class element`);
  184. }
  185. let startingOptional = member;
  186. for (;;) {
  187. if (startingOptional.isOptionalMemberExpression()) {
  188. if (startingOptional.node.optional) break;
  189. startingOptional = startingOptional.get("object");
  190. continue;
  191. } else if (startingOptional.isOptionalCallExpression()) {
  192. if (startingOptional.node.optional) break;
  193. startingOptional = startingOptional.get("callee");
  194. continue;
  195. }
  196. throw new Error(`Internal error: unexpected ${startingOptional.node.type}`);
  197. }
  198. const startingNode = startingOptional.isOptionalMemberExpression() ? startingOptional.node.object : startingOptional.node.callee;
  199. const baseNeedsMemoised = scope.maybeGenerateMemoised(startingNode);
  200. const baseRef = baseNeedsMemoised != null ? baseNeedsMemoised : startingNode;
  201. const parentIsOptionalCall = parentPath.isOptionalCallExpression({
  202. callee: node
  203. });
  204. const isOptionalCall = parent => parentIsOptionalCall;
  205. const parentIsCall = parentPath.isCallExpression({
  206. callee: node
  207. });
  208. startingOptional.replaceWith(toNonOptional(startingOptional, baseRef));
  209. if (isOptionalCall()) {
  210. if (parent.optional) {
  211. parentPath.replaceWith(this.optionalCall(member, parent.arguments));
  212. } else {
  213. parentPath.replaceWith(this.call(member, parent.arguments));
  214. }
  215. } else if (parentIsCall) {
  216. member.replaceWith(this.boundGet(member));
  217. } else if (this.delete && parentPath.isUnaryExpression({
  218. operator: "delete"
  219. })) {
  220. parentPath.replaceWith(this.delete(member));
  221. } else {
  222. member.replaceWith(this.get(member));
  223. }
  224. let regular = member.node;
  225. for (let current = member; current !== endPath;) {
  226. const parentPath = current.parentPath;
  227. if (parentPath === endPath && isOptionalCall() && parent.optional) {
  228. regular = parentPath.node;
  229. break;
  230. }
  231. regular = toNonOptional(parentPath, regular);
  232. current = parentPath;
  233. }
  234. let context;
  235. const endParentPath = endPath.parentPath;
  236. if (isMemberExpression(regular) && endParentPath.isOptionalCallExpression({
  237. callee: endPath.node,
  238. optional: true
  239. })) {
  240. const {
  241. object
  242. } = regular;
  243. context = member.scope.maybeGenerateMemoised(object);
  244. if (context) {
  245. regular.object = assignmentExpression("=", context, object);
  246. }
  247. }
  248. let replacementPath = endPath;
  249. if (isDeleteOperation) {
  250. replacementPath = endParentPath;
  251. regular = endParentPath.node;
  252. }
  253. const baseMemoised = baseNeedsMemoised ? assignmentExpression("=", cloneNode(baseRef), cloneNode(startingNode)) : cloneNode(baseRef);
  254. if (willEndPathCastToBoolean) {
  255. let nonNullishCheck;
  256. if (noDocumentAll) {
  257. nonNullishCheck = binaryExpression("!=", baseMemoised, nullLiteral());
  258. } else {
  259. nonNullishCheck = logicalExpression("&&", binaryExpression("!==", baseMemoised, nullLiteral()), binaryExpression("!==", cloneNode(baseRef), scope.buildUndefinedNode()));
  260. }
  261. replacementPath.replaceWith(logicalExpression("&&", nonNullishCheck, regular));
  262. } else {
  263. let nullishCheck;
  264. if (noDocumentAll) {
  265. nullishCheck = binaryExpression("==", baseMemoised, nullLiteral());
  266. } else {
  267. nullishCheck = logicalExpression("||", binaryExpression("===", baseMemoised, nullLiteral()), binaryExpression("===", cloneNode(baseRef), scope.buildUndefinedNode()));
  268. }
  269. replacementPath.replaceWith(conditionalExpression(nullishCheck, isDeleteOperation ? booleanLiteral(true) : scope.buildUndefinedNode(), regular));
  270. }
  271. if (context) {
  272. const endParent = endParentPath.node;
  273. endParentPath.replaceWith(optionalCallExpression(optionalMemberExpression(endParent.callee, identifier("call"), false, true), [cloneNode(context), ...endParent.arguments], false));
  274. }
  275. return;
  276. }
  277. if (isUpdateExpression(parent, {
  278. argument: node
  279. })) {
  280. if (this.simpleSet) {
  281. member.replaceWith(this.simpleSet(member));
  282. return;
  283. }
  284. const {
  285. operator,
  286. prefix
  287. } = parent;
  288. this.memoise(member, 2);
  289. const ref = scope.generateUidIdentifierBasedOnNode(node);
  290. scope.push({
  291. id: ref
  292. });
  293. const seq = [assignmentExpression("=", cloneNode(ref), this.get(member))];
  294. if (prefix) {
  295. seq.push(updateExpression(operator, cloneNode(ref), prefix));
  296. const value = sequenceExpression(seq);
  297. parentPath.replaceWith(this.set(member, value));
  298. return;
  299. } else {
  300. const ref2 = scope.generateUidIdentifierBasedOnNode(node);
  301. scope.push({
  302. id: ref2
  303. });
  304. seq.push(assignmentExpression("=", cloneNode(ref2), updateExpression(operator, cloneNode(ref), prefix)), cloneNode(ref));
  305. const value = sequenceExpression(seq);
  306. parentPath.replaceWith(sequenceExpression([this.set(member, value), cloneNode(ref2)]));
  307. return;
  308. }
  309. }
  310. if (parentPath.isAssignmentExpression({
  311. left: node
  312. })) {
  313. if (this.simpleSet) {
  314. member.replaceWith(this.simpleSet(member));
  315. return;
  316. }
  317. const {
  318. operator,
  319. right: value
  320. } = parentPath.node;
  321. if (operator === "=") {
  322. parentPath.replaceWith(this.set(member, value));
  323. } else {
  324. const operatorTrunc = operator.slice(0, -1);
  325. if (LOGICAL_OPERATORS.includes(operatorTrunc)) {
  326. this.memoise(member, 1);
  327. parentPath.replaceWith(logicalExpression(operatorTrunc, this.get(member), this.set(member, value)));
  328. } else {
  329. this.memoise(member, 2);
  330. parentPath.replaceWith(this.set(member, binaryExpression(operatorTrunc, this.get(member), value)));
  331. }
  332. }
  333. return;
  334. }
  335. if (parentPath.isCallExpression({
  336. callee: node
  337. })) {
  338. parentPath.replaceWith(this.call(member, parentPath.node.arguments));
  339. return;
  340. }
  341. if (parentPath.isOptionalCallExpression({
  342. callee: node
  343. })) {
  344. if (scope.path.isPattern()) {
  345. parentPath.replaceWith(callExpression(arrowFunctionExpression([], parentPath.node), []));
  346. return;
  347. }
  348. parentPath.replaceWith(this.optionalCall(member, parentPath.node.arguments));
  349. return;
  350. }
  351. if (this.delete && parentPath.isUnaryExpression({
  352. operator: "delete"
  353. })) {
  354. parentPath.replaceWith(this.delete(member));
  355. return;
  356. }
  357. if (parentPath.isForXStatement({
  358. left: node
  359. }) || parentPath.isObjectProperty({
  360. value: node
  361. }) && parentPath.parentPath.isObjectPattern() || parentPath.isAssignmentPattern({
  362. left: node
  363. }) && parentPath.parentPath.isObjectProperty({
  364. value: parent
  365. }) && parentPath.parentPath.parentPath.isObjectPattern() || parentPath.isArrayPattern() || parentPath.isAssignmentPattern({
  366. left: node
  367. }) && parentPath.parentPath.isArrayPattern() || parentPath.isRestElement()) {
  368. member.replaceWith(this.destructureSet(member));
  369. return;
  370. }
  371. if (parentPath.isTaggedTemplateExpression()) {
  372. member.replaceWith(this.boundGet(member));
  373. } else {
  374. member.replaceWith(this.get(member));
  375. }
  376. }
  377. };
  378. function memberExpressionToFunctions(path, visitor, state) {
  379. path.traverse(visitor, Object.assign({}, handle, state, {
  380. memoiser: new AssignmentMemoiser()
  381. }));
  382. }
  383. exports["default"] = memberExpressionToFunctions;
  384. //# sourceMappingURL=index.js.map