index.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 _semver = require("semver");
  32. var _fields = require("./fields.js");
  33. var _decorators = require("./decorators.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. api != null ? api : api = {
  60. assumption: () => void 0
  61. };
  62. }
  63. const setPublicClassFields = api.assumption("setPublicClassFields");
  64. const privateFieldsAsSymbols = api.assumption("privateFieldsAsSymbols");
  65. const privateFieldsAsProperties = api.assumption("privateFieldsAsProperties");
  66. const noUninitializedPrivateFieldAccess = (_api$assumption = api.assumption("noUninitializedPrivateFieldAccess")) != null ? _api$assumption : false;
  67. const constantSuper = api.assumption("constantSuper");
  68. const noDocumentAll = api.assumption("noDocumentAll");
  69. if (privateFieldsAsProperties && privateFieldsAsSymbols) {
  70. throw new Error(`Cannot enable both the "privateFieldsAsProperties" and ` + `"privateFieldsAsSymbols" assumptions as the same time.`);
  71. }
  72. const privateFieldsAsSymbolsOrProperties = privateFieldsAsProperties || privateFieldsAsSymbols;
  73. if (loose === true) {
  74. const explicit = [];
  75. if (setPublicClassFields !== undefined) {
  76. explicit.push(`"setPublicClassFields"`);
  77. }
  78. if (privateFieldsAsProperties !== undefined) {
  79. explicit.push(`"privateFieldsAsProperties"`);
  80. }
  81. if (privateFieldsAsSymbols !== undefined) {
  82. explicit.push(`"privateFieldsAsSymbols"`);
  83. }
  84. if (explicit.length !== 0) {
  85. 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}`);
  86. }
  87. }
  88. return {
  89. name,
  90. manipulateOptions,
  91. inherits,
  92. pre(file) {
  93. (0, _features.enableFeature)(file, feature, loose);
  94. {
  95. if (typeof file.get(versionKey) === "number") {
  96. file.set(versionKey, "7.27.0");
  97. return;
  98. }
  99. }
  100. if (!file.get(versionKey) || _semver.lt(file.get(versionKey), "7.27.0")) {
  101. file.set(versionKey, "7.27.0");
  102. }
  103. },
  104. visitor: {
  105. Class(path, {
  106. file
  107. }) {
  108. if (file.get(versionKey) !== "7.27.0") 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. {
  161. if (!props.length && !isDecorated) return;
  162. }
  163. const innerBinding = path.node.id;
  164. let ref;
  165. if (!innerBinding || !pathIsClassDeclaration) {
  166. {
  167. var _path$ensureFunctionN;
  168. (_path$ensureFunctionN = path.ensureFunctionName) != null ? _path$ensureFunctionN : path.ensureFunctionName = require("@babel/traverse").NodePath.prototype.ensureFunctionName;
  169. }
  170. path.ensureFunctionName(false);
  171. ref = path.scope.generateUidIdentifier((innerBinding == null ? void 0 : innerBinding.name) || "Class");
  172. }
  173. const classRefForDefine = ref != null ? ref : _core.types.cloneNode(innerBinding);
  174. const privateNamesMap = (0, _fields.buildPrivateNamesMap)(classRefForDefine.name, privateFieldsAsSymbolsOrProperties != null ? privateFieldsAsSymbolsOrProperties : loose, props, file);
  175. const privateNamesNodes = (0, _fields.buildPrivateNamesNodes)(privateNamesMap, privateFieldsAsProperties != null ? privateFieldsAsProperties : loose, privateFieldsAsSymbols != null ? privateFieldsAsSymbols : false, file);
  176. (0, _fields.transformPrivateNamesUsage)(classRefForDefine, path, privateNamesMap, {
  177. privateFieldsAsProperties: privateFieldsAsSymbolsOrProperties != null ? privateFieldsAsSymbolsOrProperties : loose,
  178. noUninitializedPrivateFieldAccess,
  179. noDocumentAll,
  180. innerBinding
  181. }, file);
  182. let keysNodes, staticNodes, instanceNodes, lastInstanceNodeReturnsThis, pureStaticNodes, classBindingNode, wrapClass;
  183. {
  184. if (isDecorated) {
  185. staticNodes = pureStaticNodes = keysNodes = [];
  186. ({
  187. instanceNodes,
  188. wrapClass
  189. } = (0, _decorators2.buildDecoratedClass)(classRefForDefine, path, elements, file));
  190. } else {
  191. keysNodes = (0, _misc.extractComputedKeys)(path, computedPaths, file);
  192. ({
  193. staticNodes,
  194. pureStaticNodes,
  195. instanceNodes,
  196. lastInstanceNodeReturnsThis,
  197. classBindingNode,
  198. wrapClass
  199. } = (0, _fields.buildFieldsInitNodes)(ref, path.node.superClass, props, privateNamesMap, file, setPublicClassFields != null ? setPublicClassFields : loose, privateFieldsAsSymbolsOrProperties != null ? privateFieldsAsSymbolsOrProperties : loose, noUninitializedPrivateFieldAccess, constantSuper != null ? constantSuper : loose, innerBinding));
  200. }
  201. }
  202. if (instanceNodes.length > 0) {
  203. (0, _misc.injectInitialization)(path, constructor, instanceNodes, (referenceVisitor, state) => {
  204. {
  205. if (isDecorated) return;
  206. }
  207. for (const prop of props) {
  208. if (_core.types.isStaticBlock != null && _core.types.isStaticBlock(prop.node) || prop.node.static) continue;
  209. prop.traverse(referenceVisitor, state);
  210. }
  211. }, lastInstanceNodeReturnsThis);
  212. }
  213. const wrappedPath = wrapClass(path);
  214. wrappedPath.insertBefore([...privateNamesNodes, ...keysNodes]);
  215. if (staticNodes.length > 0) {
  216. wrappedPath.insertAfter(staticNodes);
  217. }
  218. if (pureStaticNodes.length > 0) {
  219. wrappedPath.find(parent => parent.isStatement() || parent.isDeclaration()).insertAfter(pureStaticNodes);
  220. }
  221. if (classBindingNode != null && pathIsClassDeclaration) {
  222. wrappedPath.insertAfter(classBindingNode);
  223. }
  224. },
  225. ExportDefaultDeclaration(path, {
  226. file
  227. }) {
  228. {
  229. if (file.get(versionKey) !== "7.27.0") return;
  230. const decl = path.get("declaration");
  231. if (decl.isClassDeclaration() && (0, _decorators.hasDecorators)(decl.node)) {
  232. if (decl.node.id) {
  233. {
  234. var _path$splitExportDecl;
  235. (_path$splitExportDecl = path.splitExportDeclaration) != null ? _path$splitExportDecl : path.splitExportDeclaration = require("@babel/traverse").NodePath.prototype.splitExportDeclaration;
  236. }
  237. path.splitExportDeclaration();
  238. } else {
  239. decl.node.type = "ClassExpression";
  240. }
  241. }
  242. }
  243. }
  244. }
  245. };
  246. }
  247. //# sourceMappingURL=index.js.map