hoist.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. var assert = require("assert");
  11. var types = require("recast").types;
  12. var n = types.namedTypes;
  13. var b = types.builders;
  14. var hasOwn = Object.prototype.hasOwnProperty;
  15. // The hoist function takes a FunctionExpression or FunctionDeclaration
  16. // and replaces any Declaration nodes in its body with assignments, then
  17. // returns a VariableDeclaration containing just the names of the removed
  18. // declarations.
  19. exports.hoist = function(funPath) {
  20. assert.ok(funPath instanceof types.NodePath);
  21. n.Function.assert(funPath.value);
  22. var vars = {};
  23. function varDeclToExpr(vdec, includeIdentifiers) {
  24. n.VariableDeclaration.assert(vdec);
  25. var exprs = [];
  26. vdec.declarations.forEach(function(dec) {
  27. vars[dec.id.name] = dec.id;
  28. if (dec.init) {
  29. exprs.push(b.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 b.sequenceExpression(exprs);
  41. }
  42. types.visit(funPath.get("body"), {
  43. visitVariableDeclaration: function(path) {
  44. var expr = varDeclToExpr(path.value, false);
  45. if (expr === null) {
  46. path.replace();
  47. } else {
  48. // We don't need to traverse this expression any further because
  49. // there can't be any new declarations inside an expression.
  50. return b.expressionStatement(expr);
  51. }
  52. // Since the original node has been either removed or replaced,
  53. // avoid traversing it any further.
  54. return false;
  55. },
  56. visitForStatement: function(path) {
  57. var init = path.value.init;
  58. if (n.VariableDeclaration.check(init)) {
  59. path.get("init").replace(varDeclToExpr(init, false));
  60. }
  61. this.traverse(path);
  62. },
  63. visitForInStatement: function(path) {
  64. var left = path.value.left;
  65. if (n.VariableDeclaration.check(left)) {
  66. path.get("left").replace(varDeclToExpr(left, true));
  67. }
  68. this.traverse(path);
  69. },
  70. visitFunctionDeclaration: function(path) {
  71. var node = path.value;
  72. vars[node.id.name] = node.id;
  73. var parentNode = path.parent.node;
  74. var assignment = b.expressionStatement(
  75. b.assignmentExpression(
  76. "=",
  77. node.id,
  78. b.functionExpression(
  79. node.id,
  80. node.params,
  81. node.body,
  82. node.generator,
  83. node.expression
  84. )
  85. )
  86. );
  87. if (n.BlockStatement.check(path.parent.node)) {
  88. // Insert the assignment form before the first statement in the
  89. // enclosing block.
  90. path.parent.get("body").unshift(assignment);
  91. // Remove the function declaration now that we've inserted the
  92. // equivalent assignment form at the beginning of the block.
  93. path.replace();
  94. } else {
  95. // If the parent node is not a block statement, then we can just
  96. // replace the declaration with the equivalent assignment form
  97. // without worrying about hoisting it.
  98. path.replace(assignment);
  99. }
  100. // Don't hoist variables out of inner functions.
  101. return false;
  102. },
  103. visitFunctionExpression: function(path) {
  104. // Don't descend into nested function expressions.
  105. return false;
  106. }
  107. });
  108. var paramNames = {};
  109. funPath.get("params").each(function(paramPath) {
  110. var param = paramPath.value;
  111. if (n.Identifier.check(param)) {
  112. paramNames[param.name] = param;
  113. } else {
  114. // Variables declared by destructuring parameter patterns will be
  115. // harmlessly re-declared.
  116. }
  117. });
  118. var declarations = [];
  119. Object.keys(vars).forEach(function(name) {
  120. if (!hasOwn.call(paramNames, name)) {
  121. declarations.push(b.variableDeclarator(vars[name], null));
  122. }
  123. });
  124. if (declarations.length === 0) {
  125. return null; // Be sure to handle this case!
  126. }
  127. return b.variableDeclaration("var", declarations);
  128. };