5351a1244561796b058ca247c373daa781ab20fa176cf6e5bbc0a1930fd1057b03cbe6a1eddefe0271411762ecb84f361edeb273310f6818a0b75ea8e73745 4.3 KB

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