hoist.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.hoist = hoist;
  6. var util = require("./util.js");
  7. const hasOwn = Object.prototype.hasOwnProperty;
  8. function hoist(funPath) {
  9. const t = util.getTypes();
  10. t.assertFunction(funPath.node);
  11. const vars = {};
  12. function varDeclToExpr({
  13. node: vdec,
  14. scope
  15. }, includeIdentifiers) {
  16. t.assertVariableDeclaration(vdec);
  17. const exprs = [];
  18. vdec.declarations.forEach(function (dec) {
  19. vars[dec.id.name] = t.identifier(dec.id.name);
  20. scope.removeBinding(dec.id.name);
  21. if (dec.init) {
  22. exprs.push(t.assignmentExpression("=", dec.id, dec.init));
  23. } else if (includeIdentifiers) {
  24. exprs.push(dec.id);
  25. }
  26. });
  27. if (exprs.length === 0) return null;
  28. if (exprs.length === 1) return exprs[0];
  29. return t.sequenceExpression(exprs);
  30. }
  31. funPath.get("body").traverse({
  32. VariableDeclaration: {
  33. exit: function (path) {
  34. const expr = varDeclToExpr(path, false);
  35. if (expr === null) {
  36. path.remove();
  37. } else {
  38. util.replaceWithOrRemove(path, t.expressionStatement(expr));
  39. }
  40. path.skip();
  41. }
  42. },
  43. ForStatement: function (path) {
  44. const init = path.get("init");
  45. if (init.isVariableDeclaration()) {
  46. util.replaceWithOrRemove(init, varDeclToExpr(init, false));
  47. }
  48. },
  49. ForXStatement: function (path) {
  50. const left = path.get("left");
  51. if (left.isVariableDeclaration()) {
  52. util.replaceWithOrRemove(left, varDeclToExpr(left, true));
  53. }
  54. },
  55. FunctionDeclaration: function (path) {
  56. const node = path.node;
  57. vars[node.id.name] = node.id;
  58. const assignment = t.expressionStatement(t.assignmentExpression("=", t.clone(node.id), t.functionExpression(path.scope.generateUidIdentifierBasedOnNode(node), node.params, node.body, node.generator, node.expression)));
  59. if (path.parentPath.isBlockStatement()) {
  60. path.parentPath.unshiftContainer("body", assignment);
  61. path.remove();
  62. } else {
  63. util.replaceWithOrRemove(path, assignment);
  64. }
  65. path.scope.removeBinding(node.id.name);
  66. path.skip();
  67. },
  68. FunctionExpression: function (path) {
  69. path.skip();
  70. },
  71. ArrowFunctionExpression: function (path) {
  72. path.skip();
  73. }
  74. });
  75. const paramNames = {};
  76. funPath.get("params").forEach(function (paramPath) {
  77. const param = paramPath.node;
  78. if (t.isIdentifier(param)) {
  79. paramNames[param.name] = param;
  80. } else {}
  81. });
  82. const declarations = [];
  83. Object.keys(vars).forEach(function (name) {
  84. if (!hasOwn.call(paramNames, name)) {
  85. declarations.push(t.variableDeclarator(vars[name], null));
  86. }
  87. });
  88. if (declarations.length === 0) {
  89. return null;
  90. }
  91. return t.variableDeclaration("var", declarations);
  92. }
  93. //# sourceMappingURL=hoist.js.map