index.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. exports.createClassFeaturePlugin = createClassFeaturePlugin;
  12. Object.defineProperty(exports, "enableFeature", {
  13. enumerable: true,
  14. get: function () {
  15. return _features.enableFeature;
  16. }
  17. });
  18. Object.defineProperty(exports, "injectInitialization", {
  19. enumerable: true,
  20. get: function () {
  21. return _misc.injectInitialization;
  22. }
  23. });
  24. var _core = require("@babel/core");
  25. var _helperFunctionName = require("@babel/helper-function-name");
  26. var _helperSplitExportDeclaration = require("@babel/helper-split-export-declaration");
  27. var _fields = require("./fields");
  28. var _decorators = require("./decorators");
  29. var _misc = require("./misc");
  30. var _features = require("./features");
  31. var _typescript = require("./typescript");
  32. const version = "7.18.6".split(".").reduce((v, x) => v * 1e5 + +x, 0);
  33. const versionKey = "@babel/plugin-class-features/version";
  34. function createClassFeaturePlugin({
  35. name,
  36. feature,
  37. loose,
  38. manipulateOptions,
  39. api = {
  40. assumption: () => void 0
  41. },
  42. inherits
  43. }) {
  44. const setPublicClassFields = api.assumption("setPublicClassFields");
  45. const privateFieldsAsProperties = api.assumption("privateFieldsAsProperties");
  46. const constantSuper = api.assumption("constantSuper");
  47. const noDocumentAll = api.assumption("noDocumentAll");
  48. if (loose === true) {
  49. const explicit = [];
  50. if (setPublicClassFields !== undefined) {
  51. explicit.push(`"setPublicClassFields"`);
  52. }
  53. if (privateFieldsAsProperties !== undefined) {
  54. explicit.push(`"privateFieldsAsProperties"`);
  55. }
  56. if (explicit.length !== 0) {
  57. 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"privateFieldsAsProperties": true\n` + `\t}`);
  58. }
  59. }
  60. return {
  61. name,
  62. manipulateOptions,
  63. inherits,
  64. pre(file) {
  65. (0, _features.enableFeature)(file, feature, loose);
  66. if (!file.get(versionKey) || file.get(versionKey) < version) {
  67. file.set(versionKey, version);
  68. }
  69. },
  70. visitor: {
  71. Class(path, {
  72. file
  73. }) {
  74. if (file.get(versionKey) !== version) return;
  75. if (!(0, _features.shouldTransform)(path, file)) return;
  76. if (path.isClassDeclaration()) (0, _typescript.assertFieldTransformed)(path);
  77. const loose = (0, _features.isLoose)(file, feature);
  78. let constructor;
  79. const isDecorated = (0, _decorators.hasDecorators)(path.node);
  80. const props = [];
  81. const elements = [];
  82. const computedPaths = [];
  83. const privateNames = new Set();
  84. const body = path.get("body");
  85. for (const path of body.get("body")) {
  86. if ((path.isClassProperty() || path.isClassMethod()) && path.node.computed) {
  87. computedPaths.push(path);
  88. }
  89. if (path.isPrivate()) {
  90. const {
  91. name
  92. } = path.node.key.id;
  93. const getName = `get ${name}`;
  94. const setName = `set ${name}`;
  95. if (path.isClassPrivateMethod()) {
  96. if (path.node.kind === "get") {
  97. if (privateNames.has(getName) || privateNames.has(name) && !privateNames.has(setName)) {
  98. throw path.buildCodeFrameError("Duplicate private field");
  99. }
  100. privateNames.add(getName).add(name);
  101. } else if (path.node.kind === "set") {
  102. if (privateNames.has(setName) || privateNames.has(name) && !privateNames.has(getName)) {
  103. throw path.buildCodeFrameError("Duplicate private field");
  104. }
  105. privateNames.add(setName).add(name);
  106. }
  107. } else {
  108. if (privateNames.has(name) && !privateNames.has(getName) && !privateNames.has(setName) || privateNames.has(name) && (privateNames.has(getName) || privateNames.has(setName))) {
  109. throw path.buildCodeFrameError("Duplicate private field");
  110. }
  111. privateNames.add(name);
  112. }
  113. }
  114. if (path.isClassMethod({
  115. kind: "constructor"
  116. })) {
  117. constructor = path;
  118. } else {
  119. elements.push(path);
  120. if (path.isProperty() || path.isPrivate() || path.isStaticBlock != null && path.isStaticBlock()) {
  121. props.push(path);
  122. }
  123. }
  124. }
  125. {
  126. if (!props.length && !isDecorated) return;
  127. }
  128. const innerBinding = path.node.id;
  129. let ref;
  130. if (!innerBinding || path.isClassExpression()) {
  131. (0, _helperFunctionName.default)(path);
  132. ref = path.scope.generateUidIdentifier("class");
  133. } else {
  134. ref = _core.types.cloneNode(path.node.id);
  135. }
  136. const privateNamesMap = (0, _fields.buildPrivateNamesMap)(props);
  137. const privateNamesNodes = (0, _fields.buildPrivateNamesNodes)(privateNamesMap, privateFieldsAsProperties != null ? privateFieldsAsProperties : loose, file);
  138. (0, _fields.transformPrivateNamesUsage)(ref, path, privateNamesMap, {
  139. privateFieldsAsProperties: privateFieldsAsProperties != null ? privateFieldsAsProperties : loose,
  140. noDocumentAll,
  141. innerBinding
  142. }, file);
  143. let keysNodes, staticNodes, instanceNodes, pureStaticNodes, wrapClass;
  144. {
  145. if (isDecorated) {
  146. staticNodes = pureStaticNodes = keysNodes = [];
  147. ({
  148. instanceNodes,
  149. wrapClass
  150. } = (0, _decorators.buildDecoratedClass)(ref, path, elements, file));
  151. } else {
  152. keysNodes = (0, _misc.extractComputedKeys)(path, computedPaths, file);
  153. ({
  154. staticNodes,
  155. pureStaticNodes,
  156. instanceNodes,
  157. wrapClass
  158. } = (0, _fields.buildFieldsInitNodes)(ref, path.node.superClass, props, privateNamesMap, file, setPublicClassFields != null ? setPublicClassFields : loose, privateFieldsAsProperties != null ? privateFieldsAsProperties : loose, constantSuper != null ? constantSuper : loose, innerBinding));
  159. }
  160. }
  161. if (instanceNodes.length > 0) {
  162. (0, _misc.injectInitialization)(path, constructor, instanceNodes, (referenceVisitor, state) => {
  163. {
  164. if (isDecorated) return;
  165. }
  166. for (const prop of props) {
  167. if (_core.types.isStaticBlock != null && _core.types.isStaticBlock(prop.node) || prop.node.static) continue;
  168. prop.traverse(referenceVisitor, state);
  169. }
  170. });
  171. }
  172. const wrappedPath = wrapClass(path);
  173. wrappedPath.insertBefore([...privateNamesNodes, ...keysNodes]);
  174. if (staticNodes.length > 0) {
  175. wrappedPath.insertAfter(staticNodes);
  176. }
  177. if (pureStaticNodes.length > 0) {
  178. wrappedPath.find(parent => parent.isStatement() || parent.isDeclaration()).insertAfter(pureStaticNodes);
  179. }
  180. },
  181. ExportDefaultDeclaration(path, {
  182. file
  183. }) {
  184. {
  185. if (file.get(versionKey) !== version) return;
  186. const decl = path.get("declaration");
  187. if (decl.isClassDeclaration() && (0, _decorators.hasDecorators)(decl.node)) {
  188. if (decl.node.id) {
  189. (0, _helperSplitExportDeclaration.default)(path);
  190. } else {
  191. decl.node.type = "ClassExpression";
  192. }
  193. }
  194. }
  195. }
  196. }
  197. };
  198. }