index.js 10 KB

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