visit.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.getVisitor = void 0;
  6. var _assert = require("assert");
  7. var _hoist = require("./hoist.js");
  8. var _emit = require("./emit.js");
  9. var _replaceShorthandObjectMethod = require("./replaceShorthandObjectMethod.js");
  10. var util = require("./util.js");
  11. const getVisitor = t => ({
  12. Method(path, state) {
  13. const node = path.node;
  14. if (!shouldRegenerate(node, state)) return;
  15. const container = t.functionExpression(null, [], t.cloneNode(node.body, false), node.generator, node.async);
  16. path.get("body").set("body", [t.returnStatement(t.callExpression(container, []))]);
  17. node.async = false;
  18. node.generator = false;
  19. path.get("body.body.0.argument.callee").unwrapFunctionEnvironment();
  20. },
  21. Function: {
  22. exit: util.wrapWithTypes(t, function (path, state) {
  23. let node = path.node;
  24. if (!shouldRegenerate(node, state)) return;
  25. path = (0, _replaceShorthandObjectMethod.default)(path);
  26. node = path.node;
  27. const contextId = path.scope.generateUidIdentifier("context");
  28. const argsId = path.scope.generateUidIdentifier("args");
  29. path.ensureBlock();
  30. const bodyBlockPath = path.get("body");
  31. if (node.async) {
  32. bodyBlockPath.traverse(awaitVisitor);
  33. }
  34. bodyBlockPath.traverse(functionSentVisitor, {
  35. context: contextId
  36. });
  37. const outerBody = [];
  38. const innerBody = [];
  39. bodyBlockPath.get("body").forEach(function (childPath) {
  40. const node = childPath.node;
  41. if (t.isExpressionStatement(node) && t.isStringLiteral(node.expression)) {
  42. outerBody.push(node);
  43. } else if ((node == null ? void 0 : node._blockHoist) != null) {
  44. outerBody.push(node);
  45. } else {
  46. innerBody.push(node);
  47. }
  48. });
  49. if (outerBody.length > 0) {
  50. bodyBlockPath.node.body = innerBody;
  51. }
  52. const outerFnExpr = getOuterFnExpr(path);
  53. t.assertIdentifier(node.id);
  54. const innerFnId = t.identifier(node.id.name + "$");
  55. let vars = (0, _hoist.hoist)(path);
  56. const context = {
  57. usesThis: false,
  58. usesArguments: false,
  59. getArgsId: () => t.clone(argsId)
  60. };
  61. path.traverse(argumentsThisVisitor, context);
  62. if (context.usesArguments) {
  63. vars = vars || t.variableDeclaration("var", []);
  64. vars.declarations.push(t.variableDeclarator(t.clone(argsId), t.identifier("arguments")));
  65. }
  66. const emitter = new _emit.Emitter(contextId);
  67. emitter.explode(path.get("body"));
  68. if (vars && vars.declarations.length > 0) {
  69. outerBody.push(vars);
  70. }
  71. const wrapArgs = [emitter.getContextFunction(innerFnId)];
  72. const tryLocsList = emitter.getTryLocsList();
  73. if (node.generator) {
  74. wrapArgs.push(outerFnExpr);
  75. } else if (context.usesThis || tryLocsList || node.async) {
  76. wrapArgs.push(t.nullLiteral());
  77. }
  78. if (context.usesThis) {
  79. wrapArgs.push(t.thisExpression());
  80. } else if (tryLocsList || node.async) {
  81. wrapArgs.push(t.nullLiteral());
  82. }
  83. if (tryLocsList) {
  84. wrapArgs.push(tryLocsList);
  85. } else if (node.async) {
  86. wrapArgs.push(t.nullLiteral());
  87. }
  88. if (node.async) {
  89. let currentScope = path.scope;
  90. do {
  91. if (currentScope.hasOwnBinding("Promise")) currentScope.rename("Promise");
  92. } while (currentScope = currentScope.parent);
  93. wrapArgs.push(t.identifier("Promise"));
  94. }
  95. const wrapCall = t.callExpression(util.runtimeProperty(node.async ? "async" : "wrap"), wrapArgs);
  96. outerBody.push(t.returnStatement(wrapCall));
  97. node.body = t.blockStatement(outerBody);
  98. path.get("body.body").forEach(p => p.scope.registerDeclaration(p));
  99. const oldDirectives = bodyBlockPath.node.directives;
  100. if (oldDirectives) {
  101. node.body.directives = oldDirectives;
  102. }
  103. const wasGeneratorFunction = node.generator;
  104. if (wasGeneratorFunction) {
  105. node.generator = false;
  106. }
  107. if (node.async) {
  108. node.async = false;
  109. }
  110. if (wasGeneratorFunction && t.isExpression(node)) {
  111. util.replaceWithOrRemove(path, t.callExpression(util.runtimeProperty("mark"), [node]));
  112. path.addComment("leading", "#__PURE__");
  113. }
  114. const insertedLocs = emitter.getInsertedLocs();
  115. path.traverse({
  116. NumericLiteral(path) {
  117. if (!insertedLocs.has(path.node)) {
  118. return;
  119. }
  120. path.replaceWith(t.numericLiteral(path.node.value));
  121. }
  122. });
  123. path.requeue();
  124. })
  125. }
  126. });
  127. exports.getVisitor = getVisitor;
  128. function shouldRegenerate(node, state) {
  129. if (node.generator) {
  130. if (node.async) {
  131. return state.opts.asyncGenerators !== false;
  132. } else {
  133. return state.opts.generators !== false;
  134. }
  135. } else if (node.async) {
  136. return state.opts.async !== false;
  137. } else {
  138. return false;
  139. }
  140. }
  141. function getOuterFnExpr(funPath) {
  142. const t = util.getTypes();
  143. const node = funPath.node;
  144. t.assertFunction(node);
  145. if (!node.id) {
  146. node.id = funPath.scope.parent.generateUidIdentifier("callee");
  147. }
  148. if (node.generator && t.isFunctionDeclaration(node)) {
  149. return getMarkedFunctionId(funPath);
  150. }
  151. return t.clone(node.id);
  152. }
  153. const markInfo = new WeakMap();
  154. function getMarkInfo(node) {
  155. if (!markInfo.has(node)) {
  156. markInfo.set(node, {});
  157. }
  158. return markInfo.get(node);
  159. }
  160. function getMarkedFunctionId(funPath) {
  161. const t = util.getTypes();
  162. const node = funPath.node;
  163. t.assertIdentifier(node.id);
  164. const blockPath = funPath.findParent(function (path) {
  165. return path.isProgram() || path.isBlockStatement();
  166. });
  167. if (!blockPath) {
  168. return node.id;
  169. }
  170. const block = blockPath.node;
  171. _assert.ok(Array.isArray(block.body));
  172. const info = getMarkInfo(block);
  173. if (!info.decl) {
  174. info.decl = t.variableDeclaration("var", []);
  175. blockPath.unshiftContainer("body", info.decl);
  176. info.declPath = blockPath.get("body.0");
  177. }
  178. _assert.strictEqual(info.declPath.node, info.decl);
  179. const markedId = blockPath.scope.generateUidIdentifier("marked");
  180. const markCallExp = t.callExpression(util.runtimeProperty("mark"), [t.clone(node.id)]);
  181. const index = info.decl.declarations.push(t.variableDeclarator(markedId, markCallExp)) - 1;
  182. const markCallExpPath = info.declPath.get("declarations." + index + ".init");
  183. _assert.strictEqual(markCallExpPath.node, markCallExp);
  184. markCallExpPath.addComment("leading", "#__PURE__");
  185. return t.clone(markedId);
  186. }
  187. const argumentsThisVisitor = {
  188. "FunctionExpression|FunctionDeclaration|Method": function (path) {
  189. path.skip();
  190. },
  191. Identifier: function (path, state) {
  192. if (path.node.name === "arguments" && util.isReference(path)) {
  193. util.replaceWithOrRemove(path, state.getArgsId());
  194. state.usesArguments = true;
  195. }
  196. },
  197. ThisExpression: function (path, state) {
  198. state.usesThis = true;
  199. }
  200. };
  201. const functionSentVisitor = {
  202. MetaProperty(path) {
  203. const {
  204. node
  205. } = path;
  206. if (node.meta.name === "function" && node.property.name === "sent") {
  207. const t = util.getTypes();
  208. util.replaceWithOrRemove(path, t.memberExpression(t.clone(this.context), t.identifier("_sent")));
  209. }
  210. }
  211. };
  212. const awaitVisitor = {
  213. Function: function (path) {
  214. path.skip();
  215. },
  216. AwaitExpression: function (path) {
  217. const t = util.getTypes();
  218. const argument = path.node.argument;
  219. util.replaceWithOrRemove(path, t.yieldExpression(t.callExpression(util.runtimeProperty("awrap"), [argument]), false));
  220. }
  221. };
  222. //# sourceMappingURL=visit.js.map