index.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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.2".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()) {
  114. props.push(path);
  115. }
  116. }
  117. if (!isDecorated) isDecorated = (0, _decorators.hasOwnDecorators)(path.node);
  118. if (path.isStaticBlock != null && path.isStaticBlock()) {
  119. throw path.buildCodeFrameError(`Incorrect plugin order, \`@babel/plugin-proposal-class-static-block\` should be placed before class features plugins
  120. {
  121. "plugins": [
  122. "@babel/plugin-proposal-class-static-block",
  123. "@babel/plugin-proposal-private-property-in-object",
  124. "@babel/plugin-proposal-private-methods",
  125. "@babel/plugin-proposal-class-properties",
  126. ]
  127. }`);
  128. }
  129. }
  130. if (!props.length && !isDecorated) return;
  131. let ref;
  132. if (path.isClassExpression() || !path.node.id) {
  133. (0, _helperFunctionName.default)(path);
  134. ref = path.scope.generateUidIdentifier("class");
  135. } else {
  136. ref = _core.types.cloneNode(path.node.id);
  137. }
  138. const privateNamesMap = (0, _fields.buildPrivateNamesMap)(props);
  139. const privateNamesNodes = (0, _fields.buildPrivateNamesNodes)(privateNamesMap, privateFieldsAsProperties != null ? privateFieldsAsProperties : loose, state);
  140. (0, _fields.transformPrivateNamesUsage)(ref, path, privateNamesMap, {
  141. privateFieldsAsProperties: privateFieldsAsProperties != null ? privateFieldsAsProperties : loose,
  142. noDocumentAll
  143. }, state);
  144. let keysNodes, staticNodes, pureStaticNodes, instanceNodes, wrapClass;
  145. if (isDecorated) {
  146. staticNodes = pureStaticNodes = keysNodes = [];
  147. ({
  148. instanceNodes,
  149. wrapClass
  150. } = (0, _decorators.buildDecoratedClass)(ref, path, elements, this.file));
  151. } else {
  152. keysNodes = (0, _misc.extractComputedKeys)(ref, path, computedPaths, this.file);
  153. ({
  154. staticNodes,
  155. pureStaticNodes,
  156. instanceNodes,
  157. wrapClass
  158. } = (0, _fields.buildFieldsInitNodes)(ref, path.node.superClass, props, privateNamesMap, state, setPublicClassFields != null ? setPublicClassFields : loose, privateFieldsAsProperties != null ? privateFieldsAsProperties : loose, constantSuper != null ? constantSuper : loose));
  159. }
  160. if (instanceNodes.length > 0) {
  161. (0, _misc.injectInitialization)(path, constructor, instanceNodes, (referenceVisitor, state) => {
  162. if (isDecorated) return;
  163. for (const prop of props) {
  164. if (prop.node.static) continue;
  165. prop.traverse(referenceVisitor, state);
  166. }
  167. });
  168. }
  169. path = wrapClass(path);
  170. path.insertBefore([...privateNamesNodes, ...keysNodes]);
  171. if (staticNodes.length > 0) {
  172. path.insertAfter(staticNodes);
  173. }
  174. if (pureStaticNodes.length > 0) {
  175. path.find(parent => parent.isStatement() || parent.isDeclaration()).insertAfter(pureStaticNodes);
  176. }
  177. },
  178. PrivateName(path) {
  179. if (this.file.get(versionKey) !== version || path.parentPath.isPrivate({
  180. key: path.node
  181. })) {
  182. return;
  183. }
  184. throw path.buildCodeFrameError(`Unknown PrivateName "${path}"`);
  185. },
  186. ExportDefaultDeclaration(path) {
  187. if (this.file.get(versionKey) !== version) return;
  188. const decl = path.get("declaration");
  189. if (decl.isClassDeclaration() && (0, _decorators.hasDecorators)(decl.node)) {
  190. if (decl.node.id) {
  191. (0, _helperSplitExportDeclaration.default)(path);
  192. } else {
  193. decl.node.type = "ClassExpression";
  194. }
  195. }
  196. }
  197. }
  198. };
  199. }