index.js 7.8 KB

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