index.js 7.6 KB

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