index.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 _helperAnnotateAsPure = require("@babel/helper-annotate-as-pure");
  8. var _helperFunctionName = require("@babel/helper-function-name");
  9. var _helperSplitExportDeclaration = require("@babel/helper-split-export-declaration");
  10. var _core = require("@babel/core");
  11. var _globals = require("globals");
  12. var _transformClass = require("./transformClass");
  13. const getBuiltinClasses = category => Object.keys(_globals[category]).filter(name => /^[A-Z]/.test(name));
  14. const builtinClasses = new Set([...getBuiltinClasses("builtin"), ...getBuiltinClasses("browser")]);
  15. var _default = (0, _helperPluginUtils.declare)((api, options) => {
  16. var _api$assumption, _api$assumption2, _api$assumption3, _api$assumption4;
  17. api.assertVersion(7);
  18. const {
  19. loose
  20. } = options;
  21. const setClassMethods = (_api$assumption = api.assumption("setClassMethods")) != null ? _api$assumption : options.loose;
  22. const constantSuper = (_api$assumption2 = api.assumption("constantSuper")) != null ? _api$assumption2 : options.loose;
  23. const superIsCallableConstructor = (_api$assumption3 = api.assumption("superIsCallableConstructor")) != null ? _api$assumption3 : options.loose;
  24. const noClassCalls = (_api$assumption4 = api.assumption("noClassCalls")) != null ? _api$assumption4 : options.loose;
  25. const VISITED = Symbol();
  26. return {
  27. name: "transform-classes",
  28. visitor: {
  29. ExportDefaultDeclaration(path) {
  30. if (!path.get("declaration").isClassDeclaration()) return;
  31. (0, _helperSplitExportDeclaration.default)(path);
  32. },
  33. ClassDeclaration(path) {
  34. const {
  35. node
  36. } = path;
  37. const ref = node.id || path.scope.generateUidIdentifier("class");
  38. path.replaceWith(_core.types.variableDeclaration("let", [_core.types.variableDeclarator(ref, _core.types.toExpression(node))]));
  39. },
  40. ClassExpression(path, state) {
  41. const {
  42. node
  43. } = path;
  44. if (node[VISITED]) return;
  45. const inferred = (0, _helperFunctionName.default)(path);
  46. if (inferred && inferred !== node) {
  47. path.replaceWith(inferred);
  48. return;
  49. }
  50. node[VISITED] = true;
  51. path.replaceWith((0, _transformClass.default)(path, state.file, builtinClasses, loose, {
  52. setClassMethods,
  53. constantSuper,
  54. superIsCallableConstructor,
  55. noClassCalls
  56. }));
  57. if (path.isCallExpression()) {
  58. (0, _helperAnnotateAsPure.default)(path);
  59. const callee = path.get("callee");
  60. if (callee.isArrowFunctionExpression()) {
  61. callee.arrowFunctionToExpression();
  62. }
  63. }
  64. }
  65. }
  66. };
  67. });
  68. exports.default = _default;