index.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. var _helperAnnotateAsPure = require("@babel/helper-annotate-as-pure");
  9. var _default = exports.default = (0, _helperPluginUtils.declare)((api, opt) => {
  10. api.assertVersion("^7.0.0-0 || >8.0.0-alpha <8.0.0-beta");
  11. const {
  12. types: t,
  13. template
  14. } = api;
  15. const {
  16. loose
  17. } = opt;
  18. const classWeakSets = new WeakMap();
  19. const fieldsWeakSets = new WeakMap();
  20. function unshadow(name, targetScope, scope) {
  21. while (scope !== targetScope) {
  22. if (scope.hasOwnBinding(name)) scope.rename(name);
  23. scope = scope.parent;
  24. }
  25. }
  26. function injectToFieldInit(fieldPath, expr, before = false) {
  27. if (fieldPath.node.value) {
  28. const value = fieldPath.get("value");
  29. if (before) {
  30. value.insertBefore(expr);
  31. } else {
  32. value.insertAfter(expr);
  33. }
  34. } else {
  35. fieldPath.set("value", t.unaryExpression("void", expr));
  36. }
  37. }
  38. function injectInitialization(classPath, init) {
  39. let firstFieldPath;
  40. let constructorPath;
  41. for (const el of classPath.get("body.body")) {
  42. if ((el.isClassProperty() || el.isClassPrivateProperty()) && !el.node.static) {
  43. firstFieldPath = el;
  44. break;
  45. }
  46. if (!constructorPath && el.isClassMethod({
  47. kind: "constructor"
  48. })) {
  49. constructorPath = el;
  50. }
  51. }
  52. if (firstFieldPath) {
  53. injectToFieldInit(firstFieldPath, init, true);
  54. } else {
  55. (0, _helperCreateClassFeaturesPlugin.injectInitialization)(classPath, constructorPath, [t.expressionStatement(init)]);
  56. }
  57. }
  58. function getWeakSetId(weakSets, outerClass, reference, name = "", inject) {
  59. let id = weakSets.get(reference.node);
  60. if (!id) {
  61. id = outerClass.scope.generateUidIdentifier(`${name || ""} brandCheck`);
  62. weakSets.set(reference.node, id);
  63. inject(reference, template.expression.ast`${t.cloneNode(id)}.add(this)`);
  64. const newExpr = t.newExpression(t.identifier("WeakSet"), []);
  65. (0, _helperAnnotateAsPure.default)(newExpr);
  66. outerClass.insertBefore(template.ast`var ${id} = ${newExpr}`);
  67. }
  68. return t.cloneNode(id);
  69. }
  70. return {
  71. name: "transform-private-property-in-object",
  72. manipulateOptions: (_, parser) => parser.plugins.push("privateIn"),
  73. pre() {
  74. (0, _helperCreateClassFeaturesPlugin.enableFeature)(this.file, _helperCreateClassFeaturesPlugin.FEATURES.privateIn, loose);
  75. },
  76. visitor: {
  77. BinaryExpression(path, state) {
  78. const {
  79. node
  80. } = path;
  81. const {
  82. file
  83. } = state;
  84. if (node.operator !== "in") return;
  85. if (!t.isPrivateName(node.left)) return;
  86. const {
  87. name
  88. } = node.left.id;
  89. let privateElement;
  90. const outerClass = path.findParent(path => {
  91. if (!path.isClass()) return false;
  92. privateElement = path.get("body.body").find(({
  93. node
  94. }) => t.isPrivate(node) && node.key.id.name === name);
  95. return !!privateElement;
  96. });
  97. if (outerClass.parentPath.scope.path.isPattern()) {
  98. outerClass.replaceWith(template.ast`(() => ${outerClass.node})()`);
  99. return;
  100. }
  101. if (privateElement.node.type === "ClassPrivateMethod") {
  102. if (privateElement.node.static) {
  103. if (outerClass.node.id) {
  104. unshadow(outerClass.node.id.name, outerClass.scope, path.scope);
  105. } else {
  106. outerClass.set("id", path.scope.generateUidIdentifier("class"));
  107. }
  108. path.replaceWith(template.expression.ast`
  109. ${t.cloneNode(outerClass.node.id)} === ${(0, _helperCreateClassFeaturesPlugin.buildCheckInRHS)(node.right, file)}
  110. `);
  111. } else {
  112. var _outerClass$node$id;
  113. const id = getWeakSetId(classWeakSets, outerClass, outerClass, (_outerClass$node$id = outerClass.node.id) == null ? void 0 : _outerClass$node$id.name, injectInitialization);
  114. path.replaceWith(template.expression.ast`${id}.has(${(0, _helperCreateClassFeaturesPlugin.buildCheckInRHS)(node.right, file)})`);
  115. }
  116. } else {
  117. const id = getWeakSetId(fieldsWeakSets, outerClass, privateElement, privateElement.node.key.id.name, injectToFieldInit);
  118. path.replaceWith(template.expression.ast`${id}.has(${(0, _helperCreateClassFeaturesPlugin.buildCheckInRHS)(node.right, file)})`);
  119. }
  120. }
  121. }
  122. };
  123. });
  124. //# sourceMappingURL=index.js.map