index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 _helperCompilationTargets = require("@babel/helper-compilation-targets");
  8. var _helperAnnotateAsPure = require("@babel/helper-annotate-as-pure");
  9. var _core = require("@babel/core");
  10. var _transformClass = require("./transformClass.js");
  11. const globalsBrowserUpper = require("@babel/helper-globals/data/browser-upper.json"),
  12. globalsBuiltinUpper = require("@babel/helper-globals/data/builtin-upper.json");
  13. const builtinClasses = new Set([...globalsBrowserUpper, ...globalsBuiltinUpper]);
  14. builtinClasses.delete("Iterator");
  15. var _default = exports.default = (0, _helperPluginUtils.declare)((api, options) => {
  16. var _api$assumption, _api$assumption2, _api$assumption3, _api$assumption4;
  17. api.assertVersion(7);
  18. const {
  19. loose = false
  20. } = options;
  21. const setClassMethods = (_api$assumption = api.assumption("setClassMethods")) != null ? _api$assumption : loose;
  22. const constantSuper = (_api$assumption2 = api.assumption("constantSuper")) != null ? _api$assumption2 : loose;
  23. const superIsCallableConstructor = (_api$assumption3 = api.assumption("superIsCallableConstructor")) != null ? _api$assumption3 : loose;
  24. const noClassCalls = (_api$assumption4 = api.assumption("noClassCalls")) != null ? _api$assumption4 : loose;
  25. const supportUnicodeId = !(0, _helperCompilationTargets.isRequired)("transform-unicode-escapes", api.targets());
  26. const VISITED = new WeakSet();
  27. return {
  28. name: "transform-classes",
  29. visitor: {
  30. ExportDefaultDeclaration(path) {
  31. var _path$splitExportDecl;
  32. if (!path.get("declaration").isClassDeclaration()) return;
  33. (_path$splitExportDecl = path.splitExportDeclaration) != null ? _path$splitExportDecl : path.splitExportDeclaration = require("@babel/traverse").NodePath.prototype.splitExportDeclaration;
  34. path.splitExportDeclaration();
  35. },
  36. ClassDeclaration(path) {
  37. const {
  38. node
  39. } = path;
  40. const ref = node.id ? _core.types.cloneNode(node.id) : path.scope.generateUidIdentifier("class");
  41. path.replaceWith(_core.types.variableDeclaration("let", [_core.types.variableDeclarator(ref, _core.types.toExpression(node))]));
  42. },
  43. ClassExpression(path, state) {
  44. var _path$ensureFunctionN;
  45. const {
  46. node
  47. } = path;
  48. if (VISITED.has(node)) return;
  49. (_path$ensureFunctionN = path.ensureFunctionName) != null ? _path$ensureFunctionN : path.ensureFunctionName = require("@babel/traverse").NodePath.prototype.ensureFunctionName;
  50. const replacement = path.ensureFunctionName(supportUnicodeId);
  51. if (replacement && replacement.node !== node) return;
  52. VISITED.add(node);
  53. const [replacedPath] = path.replaceWith((0, _transformClass.default)(path, state.file, builtinClasses, loose, {
  54. setClassMethods,
  55. constantSuper,
  56. superIsCallableConstructor,
  57. noClassCalls
  58. }, supportUnicodeId));
  59. if (replacedPath.isCallExpression()) {
  60. (0, _helperAnnotateAsPure.default)(replacedPath);
  61. const callee = replacedPath.get("callee");
  62. if (callee.isArrowFunctionExpression()) {
  63. callee.arrowFunctionToExpression();
  64. }
  65. }
  66. }
  67. }
  68. };
  69. });
  70. //# sourceMappingURL=index.js.map