index.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 consturctorPath;
  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 (!consturctorPath && el.isClassMethod({
  48. kind: "constructor"
  49. })) {
  50. consturctorPath = el;
  51. }
  52. }
  53. if (firstFieldPath) {
  54. injectToFieldInit(firstFieldPath, init, true);
  55. } else {
  56. (0, _helperCreateClassFeaturesPlugin.injectInitialization)(classPath, consturctorPath, [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) {
  79. const {
  80. node
  81. } = path;
  82. if (node.operator !== "in") return;
  83. if (!t.isPrivateName(node.left)) return;
  84. const {
  85. name
  86. } = node.left.id;
  87. let privateElement;
  88. const outerClass = path.findParent(path => {
  89. if (!path.isClass()) return false;
  90. privateElement = path.get("body.body").find(({
  91. node
  92. }) => t.isPrivate(node) && node.key.id.name === name);
  93. return !!privateElement;
  94. });
  95. if (outerClass.parentPath.scope.path.isPattern()) {
  96. outerClass.replaceWith(template.ast`(() => ${outerClass.node})()`);
  97. return;
  98. }
  99. if (privateElement.node.type === "ClassPrivateMethod") {
  100. if (privateElement.node.static) {
  101. if (outerClass.node.id) {
  102. unshadow(outerClass.node.id.name, outerClass.scope, path.scope);
  103. } else {
  104. outerClass.set("id", path.scope.generateUidIdentifier("class"));
  105. }
  106. path.replaceWith(template.expression.ast`
  107. ${t.cloneNode(outerClass.node.id)} === ${path.node.right}
  108. `);
  109. } else {
  110. var _outerClass$node$id;
  111. const id = getWeakSetId(classWeakSets, outerClass, outerClass, (_outerClass$node$id = outerClass.node.id) == null ? void 0 : _outerClass$node$id.name, injectInitialization);
  112. path.replaceWith(template.expression.ast`${id}.has(${path.node.right})`);
  113. }
  114. } else {
  115. const id = getWeakSetId(fieldsWeakSets, outerClass, privateElement, privateElement.node.key.id.name, injectToFieldInit);
  116. path.replaceWith(template.expression.ast`${id}.has(${path.node.right})`);
  117. }
  118. }
  119. }
  120. };
  121. });
  122. exports.default = _default;