hoist.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. "use strict";
  2. var util = _interopRequireWildcard(require("./util"));
  3. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
  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(vdec, includeIdentifiers) {
  19. t.assertVariableDeclaration(vdec); // TODO assert.equal(vdec.kind, "var");
  20. var exprs = [];
  21. vdec.declarations.forEach(function (dec) {
  22. // Note: We duplicate 'dec.id' here to ensure that the variable declaration IDs don't
  23. // have the same 'loc' value, since that can make sourcemaps and retainLines behave poorly.
  24. vars[dec.id.name] = t.identifier(dec.id.name);
  25. if (dec.init) {
  26. exprs.push(t.assignmentExpression("=", dec.id, dec.init));
  27. } else if (includeIdentifiers) {
  28. exprs.push(dec.id);
  29. }
  30. });
  31. if (exprs.length === 0) return null;
  32. if (exprs.length === 1) return exprs[0];
  33. return t.sequenceExpression(exprs);
  34. }
  35. funPath.get("body").traverse({
  36. VariableDeclaration: {
  37. exit: function exit(path) {
  38. var expr = varDeclToExpr(path.node, false);
  39. if (expr === null) {
  40. path.remove();
  41. } else {
  42. // We don't need to traverse this expression any further because
  43. // there can't be any new declarations inside an expression.
  44. util.replaceWithOrRemove(path, t.expressionStatement(expr));
  45. } // Since the original node has been either removed or replaced,
  46. // avoid traversing it any further.
  47. path.skip();
  48. }
  49. },
  50. ForStatement: function ForStatement(path) {
  51. var init = path.node.init;
  52. if (t.isVariableDeclaration(init)) {
  53. util.replaceWithOrRemove(path.get("init"), varDeclToExpr(init, false));
  54. }
  55. },
  56. ForXStatement: function ForXStatement(path) {
  57. var left = path.get("left");
  58. if (left.isVariableDeclaration()) {
  59. util.replaceWithOrRemove(left, varDeclToExpr(left.node, true));
  60. }
  61. },
  62. FunctionDeclaration: function FunctionDeclaration(path) {
  63. var node = path.node;
  64. vars[node.id.name] = node.id;
  65. var assignment = t.expressionStatement(t.assignmentExpression("=", t.clone(node.id), t.functionExpression(path.scope.generateUidIdentifierBasedOnNode(node), node.params, node.body, node.generator, node.expression)));
  66. if (path.parentPath.isBlockStatement()) {
  67. // Insert the assignment form before the first statement in the
  68. // enclosing block.
  69. path.parentPath.unshiftContainer("body", assignment); // Remove the function declaration now that we've inserted the
  70. // equivalent assignment form at the beginning of the block.
  71. path.remove();
  72. } else {
  73. // If the parent node is not a block statement, then we can just
  74. // replace the declaration with the equivalent assignment form
  75. // without worrying about hoisting it.
  76. util.replaceWithOrRemove(path, assignment);
  77. } // Don't hoist variables out of inner functions.
  78. path.skip();
  79. },
  80. FunctionExpression: function FunctionExpression(path) {
  81. // Don't descend into nested function expressions.
  82. path.skip();
  83. },
  84. ArrowFunctionExpression: function ArrowFunctionExpression(path) {
  85. // Don't descend into nested function expressions.
  86. path.skip();
  87. }
  88. });
  89. var paramNames = {};
  90. funPath.get("params").forEach(function (paramPath) {
  91. var param = paramPath.node;
  92. if (t.isIdentifier(param)) {
  93. paramNames[param.name] = param;
  94. } else {// Variables declared by destructuring parameter patterns will be
  95. // harmlessly re-declared.
  96. }
  97. });
  98. var declarations = [];
  99. Object.keys(vars).forEach(function (name) {
  100. if (!hasOwn.call(paramNames, name)) {
  101. declarations.push(t.variableDeclarator(vars[name], null));
  102. }
  103. });
  104. if (declarations.length === 0) {
  105. return null; // Be sure to handle this case!
  106. }
  107. return t.variableDeclaration("var", declarations);
  108. };