index.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. if (!path.get("declaration").isClassDeclaration()) return;
  32. {
  33. var _path$splitExportDecl;
  34. (_path$splitExportDecl = path.splitExportDeclaration) != null ? _path$splitExportDecl : path.splitExportDeclaration = require("@babel/traverse").NodePath.prototype.splitExportDeclaration;
  35. }
  36. path.splitExportDeclaration();
  37. },
  38. ClassDeclaration(path) {
  39. const {
  40. node
  41. } = path;
  42. const ref = node.id ? _core.types.cloneNode(node.id) : path.scope.generateUidIdentifier("class");
  43. path.replaceWith(_core.types.variableDeclaration("let", [_core.types.variableDeclarator(ref, _core.types.toExpression(node))]));
  44. },
  45. ClassExpression(path, state) {
  46. const {
  47. node
  48. } = path;
  49. if (VISITED.has(node)) return;
  50. {
  51. var _path$ensureFunctionN;
  52. (_path$ensureFunctionN = path.ensureFunctionName) != null ? _path$ensureFunctionN : path.ensureFunctionName = require("@babel/traverse").NodePath.prototype.ensureFunctionName;
  53. }
  54. const replacement = path.ensureFunctionName(supportUnicodeId);
  55. if (replacement && replacement.node !== node) return;
  56. VISITED.add(node);
  57. const [replacedPath] = path.replaceWith((0, _transformClass.default)(path, state.file, builtinClasses, loose, {
  58. setClassMethods,
  59. constantSuper,
  60. superIsCallableConstructor,
  61. noClassCalls
  62. }, supportUnicodeId));
  63. if (replacedPath.isCallExpression()) {
  64. (0, _helperAnnotateAsPure.default)(replacedPath);
  65. const callee = replacedPath.get("callee");
  66. if (callee.isArrowFunctionExpression()) {
  67. callee.arrowFunctionToExpression();
  68. }
  69. }
  70. }
  71. }
  72. };
  73. });
  74. //# sourceMappingURL=index.js.map