index.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 _helperFunctionName = require("@babel/helper-function-name");
  10. var _helperSplitExportDeclaration = require("@babel/helper-split-export-declaration");
  11. var _core = require("@babel/core");
  12. var _globals = require("globals");
  13. var _transformClass = require("./transformClass");
  14. const getBuiltinClasses = category => Object.keys(_globals[category]).filter(name => /^[A-Z]/.test(name));
  15. const builtinClasses = new Set([...getBuiltinClasses("builtin"), ...getBuiltinClasses("browser")]);
  16. var _default = (0, _helperPluginUtils.declare)((api, options) => {
  17. var _api$assumption, _api$assumption2, _api$assumption3, _api$assumption4;
  18. api.assertVersion(7);
  19. const {
  20. loose = false
  21. } = options;
  22. const setClassMethods = (_api$assumption = api.assumption("setClassMethods")) != null ? _api$assumption : loose;
  23. const constantSuper = (_api$assumption2 = api.assumption("constantSuper")) != null ? _api$assumption2 : loose;
  24. const superIsCallableConstructor = (_api$assumption3 = api.assumption("superIsCallableConstructor")) != null ? _api$assumption3 : loose;
  25. const noClassCalls = (_api$assumption4 = api.assumption("noClassCalls")) != null ? _api$assumption4 : loose;
  26. const supportUnicodeId = !(0, _helperCompilationTargets.isRequired)("transform-unicode-escapes", api.targets());
  27. const VISITED = new WeakSet();
  28. return {
  29. name: "transform-classes",
  30. visitor: {
  31. ExportDefaultDeclaration(path) {
  32. if (!path.get("declaration").isClassDeclaration()) return;
  33. (0, _helperSplitExportDeclaration.default)(path);
  34. },
  35. ClassDeclaration(path) {
  36. const {
  37. node
  38. } = path;
  39. const ref = node.id || path.scope.generateUidIdentifier("class");
  40. path.replaceWith(_core.types.variableDeclaration("let", [_core.types.variableDeclarator(ref, _core.types.toExpression(node))]));
  41. },
  42. ClassExpression(path, state) {
  43. const {
  44. node
  45. } = path;
  46. if (VISITED.has(node)) return;
  47. const inferred = (0, _helperFunctionName.default)(path, undefined, supportUnicodeId);
  48. if (inferred && inferred !== node) {
  49. path.replaceWith(inferred);
  50. return;
  51. }
  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. exports.default = _default;
  71. //# sourceMappingURL=index.js.map