index.js 7.9 KB

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