index.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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.11");
  87. return;
  88. }
  89. }
  90. if (!file.get(versionKey) || _semver.lt(file.get(versionKey), "7.22.11")) {
  91. file.set(versionKey, "7.22.11");
  92. }
  93. },
  94. visitor: {
  95. Class(path, {
  96. file
  97. }) {
  98. var _ref;
  99. if (file.get(versionKey) !== "7.22.11") return;
  100. if (!(0, _features.shouldTransform)(path, file)) return;
  101. const pathIsClassDeclaration = path.isClassDeclaration();
  102. if (pathIsClassDeclaration) (0, _typescript.assertFieldTransformed)(path);
  103. const loose = (0, _features.isLoose)(file, feature);
  104. let constructor;
  105. const isDecorated = (0, _decorators.hasDecorators)(path.node);
  106. const props = [];
  107. const elements = [];
  108. const computedPaths = [];
  109. const privateNames = new Set();
  110. const body = path.get("body");
  111. for (const path of body.get("body")) {
  112. if ((path.isClassProperty() || path.isClassMethod()) && path.node.computed) {
  113. computedPaths.push(path);
  114. }
  115. if (path.isPrivate()) {
  116. const {
  117. name
  118. } = path.node.key.id;
  119. const getName = `get ${name}`;
  120. const setName = `set ${name}`;
  121. if (path.isClassPrivateMethod()) {
  122. if (path.node.kind === "get") {
  123. if (privateNames.has(getName) || privateNames.has(name) && !privateNames.has(setName)) {
  124. throw path.buildCodeFrameError("Duplicate private field");
  125. }
  126. privateNames.add(getName).add(name);
  127. } else if (path.node.kind === "set") {
  128. if (privateNames.has(setName) || privateNames.has(name) && !privateNames.has(getName)) {
  129. throw path.buildCodeFrameError("Duplicate private field");
  130. }
  131. privateNames.add(setName).add(name);
  132. }
  133. } else {
  134. if (privateNames.has(name) && !privateNames.has(getName) && !privateNames.has(setName) || privateNames.has(name) && (privateNames.has(getName) || privateNames.has(setName))) {
  135. throw path.buildCodeFrameError("Duplicate private field");
  136. }
  137. privateNames.add(name);
  138. }
  139. }
  140. if (path.isClassMethod({
  141. kind: "constructor"
  142. })) {
  143. constructor = path;
  144. } else {
  145. elements.push(path);
  146. if (path.isProperty() || path.isPrivate() || path.isStaticBlock != null && path.isStaticBlock()) {
  147. props.push(path);
  148. }
  149. }
  150. }
  151. {
  152. if (!props.length && !isDecorated) return;
  153. }
  154. const innerBinding = path.node.id;
  155. let ref;
  156. if (!innerBinding || !pathIsClassDeclaration) {
  157. (0, _helperFunctionName.default)(path);
  158. ref = path.scope.generateUidIdentifier("class");
  159. }
  160. const classRefForDefine = (_ref = ref) != null ? _ref : _core.types.cloneNode(innerBinding);
  161. const privateNamesMap = (0, _fields.buildPrivateNamesMap)(props);
  162. const privateNamesNodes = (0, _fields.buildPrivateNamesNodes)(privateNamesMap, privateFieldsAsProperties != null ? privateFieldsAsProperties : loose, privateFieldsAsSymbols != null ? privateFieldsAsSymbols : false, file);
  163. (0, _fields.transformPrivateNamesUsage)(classRefForDefine, path, privateNamesMap, {
  164. privateFieldsAsProperties: privateFieldsAsSymbolsOrProperties != null ? privateFieldsAsSymbolsOrProperties : loose,
  165. noDocumentAll,
  166. innerBinding
  167. }, file);
  168. let keysNodes, staticNodes, instanceNodes, pureStaticNodes, classBindingNode, wrapClass;
  169. {
  170. if (isDecorated) {
  171. staticNodes = pureStaticNodes = keysNodes = [];
  172. ({
  173. instanceNodes,
  174. wrapClass
  175. } = (0, _decorators.buildDecoratedClass)(classRefForDefine, path, elements, file));
  176. } else {
  177. keysNodes = (0, _misc.extractComputedKeys)(path, computedPaths, file);
  178. ({
  179. staticNodes,
  180. pureStaticNodes,
  181. instanceNodes,
  182. classBindingNode,
  183. wrapClass
  184. } = (0, _fields.buildFieldsInitNodes)(ref, path.node.superClass, props, privateNamesMap, file, setPublicClassFields != null ? setPublicClassFields : loose, privateFieldsAsSymbolsOrProperties != null ? privateFieldsAsSymbolsOrProperties : loose, constantSuper != null ? constantSuper : loose, innerBinding));
  185. }
  186. }
  187. if (instanceNodes.length > 0) {
  188. (0, _misc.injectInitialization)(path, constructor, instanceNodes, (referenceVisitor, state) => {
  189. {
  190. if (isDecorated) return;
  191. }
  192. for (const prop of props) {
  193. if (_core.types.isStaticBlock != null && _core.types.isStaticBlock(prop.node) || prop.node.static) continue;
  194. prop.traverse(referenceVisitor, state);
  195. }
  196. });
  197. }
  198. const wrappedPath = wrapClass(path);
  199. wrappedPath.insertBefore([...privateNamesNodes, ...keysNodes]);
  200. if (staticNodes.length > 0) {
  201. wrappedPath.insertAfter(staticNodes);
  202. }
  203. if (pureStaticNodes.length > 0) {
  204. wrappedPath.find(parent => parent.isStatement() || parent.isDeclaration()).insertAfter(pureStaticNodes);
  205. }
  206. if (classBindingNode != null && pathIsClassDeclaration) {
  207. wrappedPath.insertAfter(classBindingNode);
  208. }
  209. },
  210. ExportDefaultDeclaration(path, {
  211. file
  212. }) {
  213. {
  214. if (file.get(versionKey) !== "7.22.11") return;
  215. const decl = path.get("declaration");
  216. if (decl.isClassDeclaration() && (0, _decorators.hasDecorators)(decl.node)) {
  217. if (decl.node.id) {
  218. (0, _helperSplitExportDeclaration.default)(path);
  219. } else {
  220. decl.node.type = "ClassExpression";
  221. }
  222. }
  223. }
  224. }
  225. }
  226. };
  227. }
  228. //# sourceMappingURL=index.js.map