hoist.js 4.8 KB

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