index.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 _helperFunctionName = require("@babel/helper-function-name");
  32. var _helperSplitExportDeclaration = require("@babel/helper-split-export-declaration");
  33. var _decorators = require("./decorators.js");
  34. var _semver = require("semver");
  35. var _fields = require("./fields.js");
  36. var _decorators2 = require("./decorators-2018-09.js");
  37. var _misc = require("./misc.js");
  38. var _features = require("./features.js");
  39. var _typescript = require("./typescript.js");
  40. const versionKey = "@babel/plugin-class-features/version";
  41. function createClassFeaturePlugin({
  42. name,
  43. feature,
  44. loose,
  45. manipulateOptions,
  46. api,
  47. inherits,
  48. decoratorVersion
  49. }) {
  50. if (feature & _features.FEATURES.decorators) {
  51. {
  52. if (decoratorVersion === "2021-12" || decoratorVersion === "2022-03" || decoratorVersion === "2023-01" || decoratorVersion === "2023-05") {
  53. return (0, _decorators.default)(api, {
  54. loose
  55. }, decoratorVersion, inherits);
  56. }
  57. }
  58. }
  59. {
  60. var _api;
  61. (_api = api) != null ? _api : api = {
  62. assumption: () => void 0
  63. };
  64. }
  65. const setPublicClassFields = api.assumption("setPublicClassFields");
  66. const privateFieldsAsSymbols = api.assumption("privateFieldsAsSymbols");
  67. const privateFieldsAsProperties = api.assumption("privateFieldsAsProperties");
  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.23.10");
  98. return;
  99. }
  100. }
  101. if (!file.get(versionKey) || _semver.lt(file.get(versionKey), "7.23.10")) {
  102. file.set(versionKey, "7.23.10");
  103. }
  104. },
  105. visitor: {
  106. Class(path, {
  107. file
  108. }) {
  109. var _ref;
  110. if (file.get(versionKey) !== "7.23.10") 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. (0, _helperFunctionName.default)(path);
  169. ref = path.scope.generateUidIdentifier((innerBinding == null ? void 0 : innerBinding.name) || "Class");
  170. }
  171. const classRefForDefine = (_ref = ref) != null ? _ref : _core.types.cloneNode(innerBinding);
  172. const privateNamesMap = (0, _fields.buildPrivateNamesMap)(props);
  173. const privateNamesNodes = (0, _fields.buildPrivateNamesNodes)(privateNamesMap, privateFieldsAsProperties != null ? privateFieldsAsProperties : loose, privateFieldsAsSymbols != null ? privateFieldsAsSymbols : false, file);
  174. (0, _fields.transformPrivateNamesUsage)(classRefForDefine, path, privateNamesMap, {
  175. privateFieldsAsProperties: privateFieldsAsSymbolsOrProperties != null ? privateFieldsAsSymbolsOrProperties : loose,
  176. noDocumentAll,
  177. innerBinding
  178. }, file);
  179. let keysNodes, staticNodes, instanceNodes, lastInstanceNodeReturnsThis, pureStaticNodes, classBindingNode, wrapClass;
  180. {
  181. if (isDecorated) {
  182. staticNodes = pureStaticNodes = keysNodes = [];
  183. ({
  184. instanceNodes,
  185. wrapClass
  186. } = (0, _decorators2.buildDecoratedClass)(classRefForDefine, path, elements, file));
  187. } else {
  188. keysNodes = (0, _misc.extractComputedKeys)(path, computedPaths, file);
  189. ({
  190. staticNodes,
  191. pureStaticNodes,
  192. instanceNodes,
  193. lastInstanceNodeReturnsThis,
  194. classBindingNode,
  195. wrapClass
  196. } = (0, _fields.buildFieldsInitNodes)(ref, path.node.superClass, props, privateNamesMap, file, setPublicClassFields != null ? setPublicClassFields : loose, privateFieldsAsSymbolsOrProperties != null ? privateFieldsAsSymbolsOrProperties : loose, constantSuper != null ? constantSuper : loose, innerBinding));
  197. }
  198. }
  199. if (instanceNodes.length > 0) {
  200. (0, _misc.injectInitialization)(path, constructor, instanceNodes, (referenceVisitor, state) => {
  201. {
  202. if (isDecorated) return;
  203. }
  204. for (const prop of props) {
  205. if (_core.types.isStaticBlock != null && _core.types.isStaticBlock(prop.node) || prop.node.static) continue;
  206. prop.traverse(referenceVisitor, state);
  207. }
  208. }, lastInstanceNodeReturnsThis);
  209. }
  210. const wrappedPath = wrapClass(path);
  211. wrappedPath.insertBefore([...privateNamesNodes, ...keysNodes]);
  212. if (staticNodes.length > 0) {
  213. wrappedPath.insertAfter(staticNodes);
  214. }
  215. if (pureStaticNodes.length > 0) {
  216. wrappedPath.find(parent => parent.isStatement() || parent.isDeclaration()).insertAfter(pureStaticNodes);
  217. }
  218. if (classBindingNode != null && pathIsClassDeclaration) {
  219. wrappedPath.insertAfter(classBindingNode);
  220. }
  221. },
  222. ExportDefaultDeclaration(path, {
  223. file
  224. }) {
  225. {
  226. if (file.get(versionKey) !== "7.23.10") return;
  227. const decl = path.get("declaration");
  228. if (decl.isClassDeclaration() && (0, _decorators2.hasDecorators)(decl.node)) {
  229. if (decl.node.id) {
  230. (0, _helperSplitExportDeclaration.default)(path);
  231. } else {
  232. decl.node.type = "ClassExpression";
  233. }
  234. }
  235. }
  236. }
  237. }
  238. };
  239. }
  240. //# sourceMappingURL=index.js.map