index.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. // Add this to the end to not affect the source map
  127. maybeWarn: try {
  128. var NAME = "@babel/plugin-proposal-private-property-in-object";
  129. var stackTraceLimit = Error.stackTraceLimit;
  130. Error.stackTraceLimit = Infinity;
  131. var stack = new Error().stack;
  132. Error.stackTraceLimit = stackTraceLimit;
  133. if (!stack.includes("babel-preset-react-app")) break maybeWarn;
  134. var pkg = require("babel-preset-react-app/package.json");
  135. if (NAME in pkg.dependencies) break maybeWarn;
  136. var path = require("path");
  137. var segments = __dirname.split(path.sep).length;
  138. var up = "";
  139. while (--segments > 0) {
  140. up += ".." + path.sep;
  141. try {
  142. var pkg = require(path.join(path.resolve(__dirname, up, "package.json")));
  143. if (NAME in (pkg.dependencies || {}) || NAME in (pkg.devDependencies || {})) break maybeWarn;
  144. } catch (e) {}
  145. }
  146. setTimeout(console.warn, 2500, `\
  147. \x1B[0;33mOne of your dependencies, babel-preset-react-app, is importing the
  148. "@babel/plugin-proposal-private-property-in-object" package without
  149. declaring it in its dependencies. This is currently working because
  150. "@babel/plugin-proposal-private-property-in-object" is already in your
  151. node_modules folder for unrelated reasons, but it \x1B[1mmay break at any time\x1B[0;33m.
  152. babel-preset-react-app is part of the create-react-app project, \x1B[1mwhich
  153. is not maintianed anymore\x1B[0;33m. It is thus unlikely that this bug will ever
  154. be fixed. If you are starting a new project, you may consider using
  155. maintained alternatives such as Vite (https://vitejs.dev/) instead.
  156. Add "@babel/plugin-proposal-private-property-in-object" to your
  157. devDependencies to work around this error. This will make this message
  158. go away.\x1B[0m
  159. `);
  160. } catch (e) {}
  161. //# sourceMappingURL=index.js.map