index.js 7.6 KB

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