index.js 6.4 KB

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