index.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. Object.defineProperty(exports, "FEATURES", {
  6. enumerable: true,
  7. get: function () {
  8. return _features.FEATURES;
  9. }
  10. });
  11. Object.defineProperty(exports, "buildCheckInRHS", {
  12. enumerable: true,
  13. get: function () {
  14. return _fields.buildCheckInRHS;
  15. }
  16. });
  17. Object.defineProperty(exports, "buildNamedEvaluationVisitor", {
  18. enumerable: true,
  19. get: function () {
  20. return _decorators.buildNamedEvaluationVisitor;
  21. }
  22. });
  23. exports.createClassFeaturePlugin = createClassFeaturePlugin;
  24. Object.defineProperty(exports, "enableFeature", {
  25. enumerable: true,
  26. get: function () {
  27. return _features.enableFeature;
  28. }
  29. });
  30. Object.defineProperty(exports, "injectInitialization", {
  31. enumerable: true,
  32. get: function () {
  33. return _misc.injectInitialization;
  34. }
  35. });
  36. var _core = require("@babel/core");
  37. var _semver = require("semver");
  38. var _fields = require("./fields.js");
  39. var _decorators = require("./decorators.js");
  40. var _decorators2 = require("./decorators-2018-09.js");
  41. var _misc = require("./misc.js");
  42. var _features = require("./features.js");
  43. var _typescript = require("./typescript.js");
  44. const versionKey = "@babel/plugin-class-features/version";
  45. function createClassFeaturePlugin({
  46. name,
  47. feature,
  48. loose,
  49. manipulateOptions,
  50. api,
  51. inherits,
  52. decoratorVersion
  53. }) {
  54. var _api$assumption;
  55. if (feature & _features.FEATURES.decorators) {
  56. if (decoratorVersion === "2023-11" || decoratorVersion === "2023-05" || decoratorVersion === "2023-01" || decoratorVersion === "2022-03" || decoratorVersion === "2021-12") {
  57. return (0, _decorators.default)(api, {
  58. loose
  59. }, decoratorVersion, inherits);
  60. }
  61. }
  62. api != null ? api : api = {
  63. assumption: () => void 0
  64. };
  65. const setPublicClassFields = api.assumption("setPublicClassFields");
  66. const privateFieldsAsSymbols = api.assumption("privateFieldsAsSymbols");
  67. const privateFieldsAsProperties = api.assumption("privateFieldsAsProperties");
  68. const noUninitializedPrivateFieldAccess = (_api$assumption = api.assumption("noUninitializedPrivateFieldAccess")) != null ? _api$assumption : false;
  69. const constantSuper = api.assumption("constantSuper");
  70. const noDocumentAll = api.assumption("noDocumentAll");
  71. if (privateFieldsAsProperties && privateFieldsAsSymbols) {
  72. throw new Error(`Cannot enable both the "privateFieldsAsProperties" and ` + `"privateFieldsAsSymbols" assumptions as the same time.`);
  73. }
  74. const privateFieldsAsSymbolsOrProperties = privateFieldsAsProperties || privateFieldsAsSymbols;
  75. if (loose === true) {
  76. const explicit = [];
  77. if (setPublicClassFields !== undefined) {
  78. explicit.push(`"setPublicClassFields"`);
  79. }
  80. if (privateFieldsAsProperties !== undefined) {
  81. explicit.push(`"privateFieldsAsProperties"`);
  82. }
  83. if (privateFieldsAsSymbols !== undefined) {
  84. explicit.push(`"privateFieldsAsSymbols"`);
  85. }
  86. if (explicit.length !== 0) {
  87. console.warn(`[${name}]: You are using the "loose: true" option and you are` + ` explicitly setting a value for the ${explicit.join(" and ")}` + ` assumption${explicit.length > 1 ? "s" : ""}. The "loose" option` + ` can cause incompatibilities with the other class features` + ` plugins, so it's recommended that you replace it with the` + ` following top-level option:\n` + `\t"assumptions": {\n` + `\t\t"setPublicClassFields": true,\n` + `\t\t"privateFieldsAsSymbols": true\n` + `\t}`);
  88. }
  89. }
  90. return {
  91. name,
  92. manipulateOptions,
  93. inherits,
  94. pre(file) {
  95. (0, _features.enableFeature)(file, feature, loose);
  96. if (typeof file.get(versionKey) === "number") {
  97. file.set(versionKey, "7.28.6");
  98. return;
  99. }
  100. if (!file.get(versionKey) || _semver.lt(file.get(versionKey), "7.28.6")) {
  101. file.set(versionKey, "7.28.6");
  102. }
  103. },
  104. visitor: {
  105. Class(path, {
  106. file
  107. }) {
  108. if (file.get(versionKey) !== "7.28.6") return;
  109. if (!(0, _features.shouldTransform)(path, file)) return;
  110. const pathIsClassDeclaration = path.isClassDeclaration();
  111. if (pathIsClassDeclaration) (0, _typescript.assertFieldTransformed)(path);
  112. const loose = (0, _features.isLoose)(file, feature);
  113. let constructor;
  114. const isDecorated = (0, _decorators.hasDecorators)(path.node);
  115. const props = [];
  116. const elements = [];
  117. const computedPaths = [];
  118. const privateNames = new Set();
  119. const body = path.get("body");
  120. for (const path of body.get("body")) {
  121. if ((path.isClassProperty() || path.isClassMethod()) && path.node.computed) {
  122. computedPaths.push(path);
  123. }
  124. if (path.isPrivate()) {
  125. const {
  126. name
  127. } = path.node.key.id;
  128. const getName = `get ${name}`;
  129. const setName = `set ${name}`;
  130. if (path.isClassPrivateMethod()) {
  131. if (path.node.kind === "get") {
  132. if (privateNames.has(getName) || privateNames.has(name) && !privateNames.has(setName)) {
  133. throw path.buildCodeFrameError("Duplicate private field");
  134. }
  135. privateNames.add(getName).add(name);
  136. } else if (path.node.kind === "set") {
  137. if (privateNames.has(setName) || privateNames.has(name) && !privateNames.has(getName)) {
  138. throw path.buildCodeFrameError("Duplicate private field");
  139. }
  140. privateNames.add(setName).add(name);
  141. }
  142. } else {
  143. if (privateNames.has(name) && !privateNames.has(getName) && !privateNames.has(setName) || privateNames.has(name) && (privateNames.has(getName) || privateNames.has(setName))) {
  144. throw path.buildCodeFrameError("Duplicate private field");
  145. }
  146. privateNames.add(name);
  147. }
  148. }
  149. if (path.isClassMethod({
  150. kind: "constructor"
  151. })) {
  152. constructor = path;
  153. } else {
  154. elements.push(path);
  155. if (path.isProperty() || path.isPrivate() || path.isStaticBlock != null && path.isStaticBlock()) {
  156. props.push(path);
  157. }
  158. }
  159. }
  160. if (!props.length && !isDecorated) return;
  161. const innerBinding = path.node.id;
  162. let ref;
  163. if (!innerBinding || !pathIsClassDeclaration) {
  164. var _path$ensureFunctionN;
  165. (_path$ensureFunctionN = path.ensureFunctionName) != null ? _path$ensureFunctionN : path.ensureFunctionName = require("@babel/traverse").NodePath.prototype.ensureFunctionName;
  166. path.ensureFunctionName(false);
  167. ref = path.scope.generateUidIdentifier((innerBinding == null ? void 0 : innerBinding.name) || "Class");
  168. }
  169. const classRefForDefine = ref != null ? ref : _core.types.cloneNode(innerBinding);
  170. const privateNamesMap = (0, _fields.buildPrivateNamesMap)(classRefForDefine.name, privateFieldsAsSymbolsOrProperties != null ? privateFieldsAsSymbolsOrProperties : loose, props, file);
  171. const privateNamesNodes = (0, _fields.buildPrivateNamesNodes)(privateNamesMap, privateFieldsAsProperties != null ? privateFieldsAsProperties : loose, privateFieldsAsSymbols != null ? privateFieldsAsSymbols : false, file);
  172. (0, _fields.transformPrivateNamesUsage)(classRefForDefine, path, privateNamesMap, {
  173. privateFieldsAsProperties: privateFieldsAsSymbolsOrProperties != null ? privateFieldsAsSymbolsOrProperties : loose,
  174. noUninitializedPrivateFieldAccess,
  175. noDocumentAll,
  176. innerBinding
  177. }, file);
  178. let keysNodes, staticNodes, instanceNodes, lastInstanceNodeReturnsThis, pureStaticNodes, classBindingNode, wrapClass;
  179. if (isDecorated) {
  180. staticNodes = pureStaticNodes = keysNodes = [];
  181. ({
  182. instanceNodes,
  183. wrapClass
  184. } = (0, _decorators2.buildDecoratedClass)(classRefForDefine, path, elements, file));
  185. } else {
  186. keysNodes = (0, _misc.extractComputedKeys)(path, computedPaths, file);
  187. ({
  188. staticNodes,
  189. pureStaticNodes,
  190. instanceNodes,
  191. lastInstanceNodeReturnsThis,
  192. classBindingNode,
  193. wrapClass
  194. } = (0, _fields.buildFieldsInitNodes)(ref, path.node.superClass, props, privateNamesMap, file, setPublicClassFields != null ? setPublicClassFields : loose, privateFieldsAsSymbolsOrProperties != null ? privateFieldsAsSymbolsOrProperties : loose, noUninitializedPrivateFieldAccess, constantSuper != null ? constantSuper : loose, innerBinding));
  195. }
  196. if (instanceNodes.length > 0) {
  197. (0, _misc.injectInitialization)(path, constructor, instanceNodes, (referenceVisitor, state) => {
  198. if (isDecorated) return;
  199. for (const prop of props) {
  200. if (_core.types.isStaticBlock != null && _core.types.isStaticBlock(prop.node) || prop.node.static) continue;
  201. prop.traverse(referenceVisitor, state);
  202. }
  203. }, lastInstanceNodeReturnsThis);
  204. }
  205. const wrappedPath = wrapClass(path);
  206. wrappedPath.insertBefore([...privateNamesNodes, ...keysNodes]);
  207. if (staticNodes.length > 0) {
  208. wrappedPath.insertAfter(staticNodes);
  209. }
  210. if (pureStaticNodes.length > 0) {
  211. wrappedPath.find(parent => parent.isStatement() || parent.isDeclaration()).insertAfter(pureStaticNodes);
  212. }
  213. if (classBindingNode != null && pathIsClassDeclaration) {
  214. wrappedPath.insertAfter(classBindingNode);
  215. }
  216. },
  217. ExportDefaultDeclaration(path, {
  218. file
  219. }) {
  220. if (file.get(versionKey) !== "7.28.6") return;
  221. const decl = path.get("declaration");
  222. if (decl.isClassDeclaration() && (0, _decorators.hasDecorators)(decl.node)) {
  223. if (decl.node.id) {
  224. var _path$splitExportDecl;
  225. (_path$splitExportDecl = path.splitExportDeclaration) != null ? _path$splitExportDecl : path.splitExportDeclaration = require("@babel/traverse").NodePath.prototype.splitExportDeclaration;
  226. path.splitExportDeclaration();
  227. } else {
  228. decl.node.type = "ClassExpression";
  229. }
  230. }
  231. }
  232. }
  233. };
  234. }
  235. //# sourceMappingURL=index.js.map