You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.2 KiB

4 years ago
  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;