hoist.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. "use strict";
  2. var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
  3. var util = _interopRequireWildcard(require("./util"));
  4. /**
  5. * Copyright (c) 2014-present, Facebook, Inc.
  6. *
  7. * This source code is licensed under the MIT license found in the
  8. * LICENSE file in the root directory of this source tree.
  9. */
  10. var hasOwn = Object.prototype.hasOwnProperty; // The hoist function takes a FunctionExpression or FunctionDeclaration
  11. // and replaces any Declaration nodes in its body with assignments, then
  12. // returns a VariableDeclaration containing just the names of the removed
  13. // declarations.
  14. exports.hoist = function (funPath) {
  15. var t = util.getTypes();
  16. t.assertFunction(funPath.node);
  17. var vars = {};
  18. function varDeclToExpr(_ref, includeIdentifiers) {
  19. var vdec = _ref.node,
  20. scope = _ref.scope;
  21. t.assertVariableDeclaration(vdec); // TODO assert.equal(vdec.kind, "var");
  22. var exprs = [];
  23. vdec.declarations.forEach(function (dec) {
  24. // Note: We duplicate 'dec.id' here to ensure that the variable declaration IDs don't
  25. // have the same 'loc' value, since that can make sourcemaps and retainLines behave poorly.
  26. vars[dec.id.name] = t.identifier(dec.id.name); // Remove the binding, to avoid "duplicate declaration" errors when it will
  27. // be injected again.
  28. scope.removeBinding(dec.id.name);
  29. if (dec.init) {
  30. exprs.push(t.assignmentExpression("=", dec.id, dec.init));
  31. } else if (includeIdentifiers) {
  32. exprs.push(dec.id);
  33. }
  34. });
  35. if (exprs.length === 0) return null;
  36. if (exprs.length === 1) return exprs[0];
  37. return t.sequenceExpression(exprs);
  38. }
  39. funPath.get("body").traverse({
  40. VariableDeclaration: {
  41. exit: function exit(path) {
  42. var expr = varDeclToExpr(path, false);
  43. if (expr === null) {
  44. path.remove();
  45. } else {
  46. // We don't need to traverse this expression any further because
  47. // there can't be any new declarations inside an expression.
  48. util.replaceWithOrRemove(path, t.expressionStatement(expr));
  49. } // Since the original node has been either removed or replaced,
  50. // avoid traversing it any further.
  51. path.skip();
  52. }
  53. },
  54. ForStatement: function ForStatement(path) {
  55. var init = path.get("init");
  56. if (init.isVariableDeclaration()) {
  57. util.replaceWithOrRemove(init, varDeclToExpr(init, false));
  58. }
  59. },
  60. ForXStatement: function ForXStatement(path) {
  61. var left = path.get("left");
  62. if (left.isVariableDeclaration()) {
  63. util.replaceWithOrRemove(left, varDeclToExpr(left, true));
  64. }
  65. },
  66. FunctionDeclaration: function FunctionDeclaration(path) {
  67. var node = path.node;
  68. vars[node.id.name] = node.id;
  69. var assignment = t.expressionStatement(t.assignmentExpression("=", t.clone(node.id), t.functionExpression(path.scope.generateUidIdentifierBasedOnNode(node), node.params, node.body, node.generator, node.expression)));
  70. if (path.parentPath.isBlockStatement()) {
  71. // Insert the assignment form before the first statement in the
  72. // enclosing block.
  73. path.parentPath.unshiftContainer("body", assignment); // Remove the function declaration now that we've inserted the
  74. // equivalent assignment form at the beginning of the block.
  75. path.remove();
  76. } else {
  77. // If the parent node is not a block statement, then we can just
  78. // replace the declaration with the equivalent assignment form
  79. // without worrying about hoisting it.
  80. util.replaceWithOrRemove(path, assignment);
  81. } // Remove the binding, to avoid "duplicate declaration" errors when it will
  82. // be injected again.
  83. path.scope.removeBinding(node.id.name); // Don't hoist variables out of inner functions.
  84. path.skip();
  85. },
  86. FunctionExpression: function FunctionExpression(path) {
  87. // Don't descend into nested function expressions.
  88. path.skip();
  89. },
  90. ArrowFunctionExpression: function ArrowFunctionExpression(path) {
  91. // Don't descend into nested function expressions.
  92. path.skip();
  93. }
  94. });
  95. var paramNames = {};
  96. funPath.get("params").forEach(function (paramPath) {
  97. var param = paramPath.node;
  98. if (t.isIdentifier(param)) {
  99. paramNames[param.name] = param;
  100. } else {// Variables declared by destructuring parameter patterns will be
  101. // harmlessly re-declared.
  102. }
  103. });
  104. var declarations = [];
  105. Object.keys(vars).forEach(function (name) {
  106. if (!hasOwn.call(paramNames, name)) {
  107. declarations.push(t.variableDeclarator(vars[name], null));
  108. }
  109. });
  110. if (declarations.length === 0) {
  111. return null; // Be sure to handle this case!
  112. }
  113. return t.variableDeclaration("var", declarations);
  114. };