index.js 8.5 KB

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