index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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 {
  218. member.replaceWith(this.get(member));
  219. }
  220. let regular = member.node;
  221. for (let current = member; current !== endPath;) {
  222. const parentPath = current.parentPath;
  223. if (parentPath === endPath && isOptionalCall() && parent.optional) {
  224. regular = parentPath.node;
  225. break;
  226. }
  227. regular = toNonOptional(parentPath, regular);
  228. current = parentPath;
  229. }
  230. let context;
  231. const endParentPath = endPath.parentPath;
  232. if (isMemberExpression(regular) && endParentPath.isOptionalCallExpression({
  233. callee: endPath.node,
  234. optional: true
  235. })) {
  236. const {
  237. object
  238. } = regular;
  239. context = member.scope.maybeGenerateMemoised(object);
  240. if (context) {
  241. regular.object = assignmentExpression("=", context, object);
  242. }
  243. }
  244. let replacementPath = endPath;
  245. if (isDeleteOperation) {
  246. replacementPath = endParentPath;
  247. regular = endParentPath.node;
  248. }
  249. const baseMemoised = baseNeedsMemoised ? assignmentExpression("=", cloneNode(baseRef), cloneNode(startingNode)) : cloneNode(baseRef);
  250. if (willEndPathCastToBoolean) {
  251. let nonNullishCheck;
  252. if (noDocumentAll) {
  253. nonNullishCheck = binaryExpression("!=", baseMemoised, nullLiteral());
  254. } else {
  255. nonNullishCheck = logicalExpression("&&", binaryExpression("!==", baseMemoised, nullLiteral()), binaryExpression("!==", cloneNode(baseRef), scope.buildUndefinedNode()));
  256. }
  257. replacementPath.replaceWith(logicalExpression("&&", nonNullishCheck, regular));
  258. } else {
  259. let nullishCheck;
  260. if (noDocumentAll) {
  261. nullishCheck = binaryExpression("==", baseMemoised, nullLiteral());
  262. } else {
  263. nullishCheck = logicalExpression("||", binaryExpression("===", baseMemoised, nullLiteral()), binaryExpression("===", cloneNode(baseRef), scope.buildUndefinedNode()));
  264. }
  265. replacementPath.replaceWith(conditionalExpression(nullishCheck, isDeleteOperation ? booleanLiteral(true) : scope.buildUndefinedNode(), regular));
  266. }
  267. if (context) {
  268. const endParent = endParentPath.node;
  269. endParentPath.replaceWith(optionalCallExpression(optionalMemberExpression(endParent.callee, identifier("call"), false, true), [cloneNode(context), ...endParent.arguments], false));
  270. }
  271. return;
  272. }
  273. if (isUpdateExpression(parent, {
  274. argument: node
  275. })) {
  276. if (this.simpleSet) {
  277. member.replaceWith(this.simpleSet(member));
  278. return;
  279. }
  280. const {
  281. operator,
  282. prefix
  283. } = parent;
  284. this.memoise(member, 2);
  285. const ref = scope.generateUidIdentifierBasedOnNode(node);
  286. scope.push({
  287. id: ref
  288. });
  289. const seq = [assignmentExpression("=", cloneNode(ref), this.get(member))];
  290. if (prefix) {
  291. seq.push(updateExpression(operator, cloneNode(ref), prefix));
  292. const value = sequenceExpression(seq);
  293. parentPath.replaceWith(this.set(member, value));
  294. return;
  295. } else {
  296. const ref2 = scope.generateUidIdentifierBasedOnNode(node);
  297. scope.push({
  298. id: ref2
  299. });
  300. seq.push(assignmentExpression("=", cloneNode(ref2), updateExpression(operator, cloneNode(ref), prefix)), cloneNode(ref));
  301. const value = sequenceExpression(seq);
  302. parentPath.replaceWith(sequenceExpression([this.set(member, value), cloneNode(ref2)]));
  303. return;
  304. }
  305. }
  306. if (parentPath.isAssignmentExpression({
  307. left: node
  308. })) {
  309. if (this.simpleSet) {
  310. member.replaceWith(this.simpleSet(member));
  311. return;
  312. }
  313. const {
  314. operator,
  315. right: value
  316. } = parentPath.node;
  317. if (operator === "=") {
  318. parentPath.replaceWith(this.set(member, value));
  319. } else {
  320. const operatorTrunc = operator.slice(0, -1);
  321. if (LOGICAL_OPERATORS.includes(operatorTrunc)) {
  322. this.memoise(member, 1);
  323. parentPath.replaceWith(logicalExpression(operatorTrunc, this.get(member), this.set(member, value)));
  324. } else {
  325. this.memoise(member, 2);
  326. parentPath.replaceWith(this.set(member, binaryExpression(operatorTrunc, this.get(member), value)));
  327. }
  328. }
  329. return;
  330. }
  331. if (parentPath.isCallExpression({
  332. callee: node
  333. })) {
  334. parentPath.replaceWith(this.call(member, parentPath.node.arguments));
  335. return;
  336. }
  337. if (parentPath.isOptionalCallExpression({
  338. callee: node
  339. })) {
  340. if (scope.path.isPattern()) {
  341. parentPath.replaceWith(callExpression(arrowFunctionExpression([], parentPath.node), []));
  342. return;
  343. }
  344. parentPath.replaceWith(this.optionalCall(member, parentPath.node.arguments));
  345. return;
  346. }
  347. if (parentPath.isForXStatement({
  348. left: node
  349. }) || parentPath.isObjectProperty({
  350. value: node
  351. }) && parentPath.parentPath.isObjectPattern() || parentPath.isAssignmentPattern({
  352. left: node
  353. }) && parentPath.parentPath.isObjectProperty({
  354. value: parent
  355. }) && parentPath.parentPath.parentPath.isObjectPattern() || parentPath.isArrayPattern() || parentPath.isAssignmentPattern({
  356. left: node
  357. }) && parentPath.parentPath.isArrayPattern() || parentPath.isRestElement()) {
  358. member.replaceWith(this.destructureSet(member));
  359. return;
  360. }
  361. if (parentPath.isTaggedTemplateExpression()) {
  362. member.replaceWith(this.boundGet(member));
  363. } else {
  364. member.replaceWith(this.get(member));
  365. }
  366. }
  367. };
  368. function memberExpressionToFunctions(path, visitor, state) {
  369. path.traverse(visitor, Object.assign({}, handle, state, {
  370. memoiser: new AssignmentMemoiser()
  371. }));
  372. }
  373. exports["default"] = memberExpressionToFunctions;
  374. //# sourceMappingURL=index.js.map