index.js 4.7 KB

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