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.

77 lines
2.3 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 _helperAnnotateAsPure = _interopRequireDefault(require("@babel/helper-annotate-as-pure"));
  8. var _helperFunctionName = _interopRequireDefault(require("@babel/helper-function-name"));
  9. var _helperSplitExportDeclaration = _interopRequireDefault(require("@babel/helper-split-export-declaration"));
  10. var _core = require("@babel/core");
  11. var _globals = _interopRequireDefault(require("globals"));
  12. var _transformClass = _interopRequireDefault(require("./transformClass"));
  13. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  14. const getBuiltinClasses = category => Object.keys(_globals.default[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. api.assertVersion(7);
  18. const {
  19. loose
  20. } = options;
  21. const VISITED = Symbol();
  22. return {
  23. name: "transform-classes",
  24. visitor: {
  25. ExportDefaultDeclaration(path) {
  26. if (!path.get("declaration").isClassDeclaration()) return;
  27. (0, _helperSplitExportDeclaration.default)(path);
  28. },
  29. ClassDeclaration(path) {
  30. const {
  31. node
  32. } = path;
  33. const ref = node.id || path.scope.generateUidIdentifier("class");
  34. path.replaceWith(_core.types.variableDeclaration("let", [_core.types.variableDeclarator(ref, _core.types.toExpression(node))]));
  35. },
  36. ClassExpression(path, state) {
  37. const {
  38. node
  39. } = path;
  40. if (node[VISITED]) return;
  41. const inferred = (0, _helperFunctionName.default)(path);
  42. if (inferred && inferred !== node) {
  43. path.replaceWith(inferred);
  44. return;
  45. }
  46. node[VISITED] = true;
  47. path.replaceWith((0, _transformClass.default)(path, state.file, builtinClasses, loose));
  48. if (path.isCallExpression()) {
  49. (0, _helperAnnotateAsPure.default)(path);
  50. if (path.get("callee").isArrowFunctionExpression()) {
  51. path.get("callee").arrowFunctionToExpression();
  52. }
  53. }
  54. }
  55. }
  56. };
  57. });
  58. exports.default = _default;