index.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 transformStatementList(paths) {
  11. for (const path of paths) {
  12. if (!path.isFunctionDeclaration()) continue;
  13. const func = path.node;
  14. const declar = _core.types.variableDeclaration("let", [_core.types.variableDeclarator(func.id, _core.types.toExpression(func))]);
  15. declar._blockHoist = 2;
  16. func.id = null;
  17. path.replaceWith(declar);
  18. }
  19. }
  20. return {
  21. name: "transform-block-scoped-functions",
  22. visitor: {
  23. BlockStatement(path) {
  24. const {
  25. node,
  26. parent
  27. } = path;
  28. if (_core.types.isFunction(parent, {
  29. body: node
  30. }) || _core.types.isExportDeclaration(parent)) {
  31. return;
  32. }
  33. transformStatementList(path.get("body"));
  34. },
  35. SwitchCase(path) {
  36. transformStatementList(path.get("consequent"));
  37. }
  38. }
  39. };
  40. });
  41. exports.default = _default;