index.js 8.0 KB

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