index.js 7.9 KB

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