index.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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.17.9".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() {
  65. (0, _features.enableFeature)(this.file, feature, loose);
  66. if (!this.file.get(versionKey) || this.file.get(versionKey) < version) {
  67. this.file.set(versionKey, version);
  68. }
  69. },
  70. visitor: {
  71. Class(path, state) {
  72. if (this.file.get(versionKey) !== version) return;
  73. if (!(0, _features.shouldTransform)(path, this.file)) return;
  74. if (path.isClassDeclaration()) (0, _typescript.assertFieldTransformed)(path);
  75. const loose = (0, _features.isLoose)(this.file, feature);
  76. let constructor;
  77. const isDecorated = (0, _decorators.hasDecorators)(path.node);
  78. const props = [];
  79. const elements = [];
  80. const computedPaths = [];
  81. const privateNames = new Set();
  82. const body = path.get("body");
  83. for (const path of body.get("body")) {
  84. if ((path.isClassProperty() || path.isClassMethod()) && path.node.computed) {
  85. computedPaths.push(path);
  86. }
  87. if (path.isPrivate()) {
  88. const {
  89. name
  90. } = path.node.key.id;
  91. const getName = `get ${name}`;
  92. const setName = `set ${name}`;
  93. if (path.isClassPrivateMethod()) {
  94. if (path.node.kind === "get") {
  95. if (privateNames.has(getName) || privateNames.has(name) && !privateNames.has(setName)) {
  96. throw path.buildCodeFrameError("Duplicate private field");
  97. }
  98. privateNames.add(getName).add(name);
  99. } else if (path.node.kind === "set") {
  100. if (privateNames.has(setName) || privateNames.has(name) && !privateNames.has(getName)) {
  101. throw path.buildCodeFrameError("Duplicate private field");
  102. }
  103. privateNames.add(setName).add(name);
  104. }
  105. } else {
  106. if (privateNames.has(name) && !privateNames.has(getName) && !privateNames.has(setName) || privateNames.has(name) && (privateNames.has(getName) || privateNames.has(setName))) {
  107. throw path.buildCodeFrameError("Duplicate private field");
  108. }
  109. privateNames.add(name);
  110. }
  111. }
  112. if (path.isClassMethod({
  113. kind: "constructor"
  114. })) {
  115. constructor = path;
  116. } else {
  117. elements.push(path);
  118. if (path.isProperty() || path.isPrivate() || path.isStaticBlock != null && path.isStaticBlock()) {
  119. props.push(path);
  120. }
  121. }
  122. }
  123. if (!props.length && !isDecorated) return;
  124. const innerBinding = path.node.id;
  125. let ref;
  126. if (!innerBinding || path.isClassExpression()) {
  127. (0, _helperFunctionName.default)(path);
  128. ref = path.scope.generateUidIdentifier("class");
  129. } else {
  130. ref = _core.types.cloneNode(path.node.id);
  131. }
  132. const privateNamesMap = (0, _fields.buildPrivateNamesMap)(props);
  133. const privateNamesNodes = (0, _fields.buildPrivateNamesNodes)(privateNamesMap, privateFieldsAsProperties != null ? privateFieldsAsProperties : loose, state);
  134. (0, _fields.transformPrivateNamesUsage)(ref, path, privateNamesMap, {
  135. privateFieldsAsProperties: privateFieldsAsProperties != null ? privateFieldsAsProperties : loose,
  136. noDocumentAll,
  137. innerBinding
  138. }, state);
  139. let keysNodes, staticNodes, instanceNodes, pureStaticNodes, wrapClass;
  140. if (isDecorated) {
  141. staticNodes = pureStaticNodes = keysNodes = [];
  142. ({
  143. instanceNodes,
  144. wrapClass
  145. } = (0, _decorators.buildDecoratedClass)(ref, path, elements, this.file));
  146. } else {
  147. keysNodes = (0, _misc.extractComputedKeys)(ref, path, computedPaths, this.file);
  148. ({
  149. staticNodes,
  150. pureStaticNodes,
  151. instanceNodes,
  152. wrapClass
  153. } = (0, _fields.buildFieldsInitNodes)(ref, path.node.superClass, props, privateNamesMap, state, setPublicClassFields != null ? setPublicClassFields : loose, privateFieldsAsProperties != null ? privateFieldsAsProperties : loose, constantSuper != null ? constantSuper : loose, innerBinding));
  154. }
  155. if (instanceNodes.length > 0) {
  156. (0, _misc.injectInitialization)(path, constructor, instanceNodes, (referenceVisitor, state) => {
  157. if (isDecorated) return;
  158. for (const prop of props) {
  159. if (_core.types.isStaticBlock != null && _core.types.isStaticBlock(prop.node) || prop.node.static) continue;
  160. prop.traverse(referenceVisitor, state);
  161. }
  162. });
  163. }
  164. const wrappedPath = wrapClass(path);
  165. wrappedPath.insertBefore([...privateNamesNodes, ...keysNodes]);
  166. if (staticNodes.length > 0) {
  167. wrappedPath.insertAfter(staticNodes);
  168. }
  169. if (pureStaticNodes.length > 0) {
  170. wrappedPath.find(parent => parent.isStatement() || parent.isDeclaration()).insertAfter(pureStaticNodes);
  171. }
  172. },
  173. ExportDefaultDeclaration(path) {
  174. if (this.file.get(versionKey) !== version) return;
  175. const decl = path.get("declaration");
  176. if (decl.isClassDeclaration() && (0, _decorators.hasDecorators)(decl.node)) {
  177. if (decl.node.id) {
  178. (0, _helperSplitExportDeclaration.default)(path);
  179. } else {
  180. decl.node.type = "ClassExpression";
  181. }
  182. }
  183. }
  184. }
  185. };
  186. }