index.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 _helperCreateClassFeaturesPlugin = require("@babel/helper-create-class-features-plugin");
  8. function generateUid(scope, denyList) {
  9. const name = "";
  10. let uid;
  11. let i = 1;
  12. do {
  13. uid = scope._generateUid(name, i);
  14. i++;
  15. } while (denyList.has(uid));
  16. return uid;
  17. }
  18. var _default = exports.default = (0, _helperPluginUtils.declare)(({
  19. types: t,
  20. template,
  21. assertVersion
  22. }) => {
  23. assertVersion("^7.12.0");
  24. return {
  25. name: "transform-class-static-block",
  26. inherits: require("@babel/plugin-syntax-class-static-block").default,
  27. pre() {
  28. (0, _helperCreateClassFeaturesPlugin.enableFeature)(this.file, _helperCreateClassFeaturesPlugin.FEATURES.staticBlocks, false);
  29. },
  30. visitor: {
  31. ClassBody(classBody) {
  32. const {
  33. scope
  34. } = classBody;
  35. const privateNames = new Set();
  36. const body = classBody.get("body");
  37. for (const path of body) {
  38. if (path.isPrivate()) {
  39. privateNames.add(path.get("key.id").node.name);
  40. }
  41. }
  42. for (const path of body) {
  43. if (!path.isStaticBlock()) continue;
  44. const staticBlockPrivateId = generateUid(scope, privateNames);
  45. privateNames.add(staticBlockPrivateId);
  46. const staticBlockRef = t.privateName(t.identifier(staticBlockPrivateId));
  47. let replacement;
  48. const blockBody = path.node.body;
  49. if (blockBody.length === 1 && t.isExpressionStatement(blockBody[0])) {
  50. replacement = t.inheritsComments(blockBody[0].expression, blockBody[0]);
  51. } else {
  52. replacement = template.expression.ast`(() => { ${blockBody} })()`;
  53. }
  54. path.replaceWith(t.classPrivateProperty(staticBlockRef, replacement, [], true));
  55. }
  56. }
  57. }
  58. };
  59. });
  60. //# sourceMappingURL=index.js.map