index.js 8.7 KB

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