index.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. "use strict";
  2. exports.__esModule = true;
  3. var _getIterator2 = require("babel-runtime/core-js/get-iterator");
  4. var _getIterator3 = _interopRequireDefault(_getIterator2);
  5. exports.default = function (_ref) {
  6. var t = _ref.types;
  7. function statementList(key, path) {
  8. var paths = path.get(key);
  9. for (var _iterator = paths, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator3.default)(_iterator);;) {
  10. var _ref2;
  11. if (_isArray) {
  12. if (_i >= _iterator.length) break;
  13. _ref2 = _iterator[_i++];
  14. } else {
  15. _i = _iterator.next();
  16. if (_i.done) break;
  17. _ref2 = _i.value;
  18. }
  19. var _path = _ref2;
  20. var func = _path.node;
  21. if (!_path.isFunctionDeclaration()) continue;
  22. var declar = t.variableDeclaration("let", [t.variableDeclarator(func.id, t.toExpression(func))]);
  23. declar._blockHoist = 2;
  24. func.id = null;
  25. _path.replaceWith(declar);
  26. }
  27. }
  28. return {
  29. visitor: {
  30. BlockStatement: function BlockStatement(path) {
  31. var node = path.node,
  32. parent = path.parent;
  33. if (t.isFunction(parent, { body: node }) || t.isExportDeclaration(parent)) {
  34. return;
  35. }
  36. statementList("body", path);
  37. },
  38. SwitchCase: function SwitchCase(path) {
  39. statementList("consequent", path);
  40. }
  41. }
  42. };
  43. };
  44. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  45. module.exports = exports["default"];