index.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _helperPluginUtils = require("@babel/helper-plugin-utils");
  7. var _core = require("@babel/core");
  8. var _default = (0, _helperPluginUtils.declare)(api => {
  9. api.assertVersion(7);
  10. function statementList(key, path) {
  11. const paths = path.get(key);
  12. for (const path of paths) {
  13. const func = path.node;
  14. if (!path.isFunctionDeclaration()) continue;
  15. const declar = _core.types.variableDeclaration("let", [_core.types.variableDeclarator(func.id, _core.types.toExpression(func))]);
  16. declar._blockHoist = 2;
  17. func.id = null;
  18. path.replaceWith(declar);
  19. }
  20. }
  21. return {
  22. name: "transform-block-scoped-functions",
  23. visitor: {
  24. BlockStatement(path) {
  25. const {
  26. node,
  27. parent
  28. } = path;
  29. if (_core.types.isFunction(parent, {
  30. body: node
  31. }) || _core.types.isExportDeclaration(parent)) {
  32. return;
  33. }
  34. statementList("body", path);
  35. },
  36. SwitchCase(path) {
  37. statementList("consequent", path);
  38. }
  39. }
  40. };
  41. });
  42. exports.default = _default;