index.js 1.4 KB

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