index.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 _pluginSyntaxClassStaticBlock = _interopRequireDefault(require("@babel/plugin-syntax-class-static-block"));
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. function generateUid(scope, denyList) {
  10. const name = "";
  11. let uid;
  12. let i = 1;
  13. do {
  14. uid = scope._generateUid(name, i);
  15. i++;
  16. } while (denyList.has(uid));
  17. return uid;
  18. }
  19. var _default = (0, _helperPluginUtils.declare)(({
  20. types: t,
  21. template,
  22. assertVersion
  23. }) => {
  24. assertVersion("^7.12.0");
  25. return {
  26. name: "proposal-class-static-block",
  27. inherits: _pluginSyntaxClassStaticBlock.default,
  28. visitor: {
  29. Class(path) {
  30. const {
  31. scope
  32. } = path;
  33. const classBody = path.get("body");
  34. const privateNames = new Set();
  35. const body = classBody.get("body");
  36. for (const path of body) {
  37. if (path.isPrivate()) {
  38. privateNames.add(path.get("key.id").node.name);
  39. }
  40. }
  41. for (const path of body) {
  42. if (!path.isStaticBlock()) continue;
  43. const staticBlockPrivateId = generateUid(scope, privateNames);
  44. privateNames.add(staticBlockPrivateId);
  45. const staticBlockRef = t.privateName(t.identifier(staticBlockPrivateId));
  46. path.replaceWith(t.classPrivateProperty(staticBlockRef, template.expression.ast`(() => { ${path.node.body} })()`, [], true));
  47. }
  48. }
  49. }
  50. };
  51. });
  52. exports.default = _default;