index.js 10 KB

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