index.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. }
  96. if (!props.length && !isDecorated) return;
  97. let ref;
  98. if (path.isClassExpression() || !path.node.id) {
  99. (0, _helperFunctionName.default)(path);
  100. ref = path.scope.generateUidIdentifier("class");
  101. } else {
  102. ref = _core.types.cloneNode(path.node.id);
  103. }
  104. const privateNamesMap = (0, _fields.buildPrivateNamesMap)(props);
  105. const privateNamesNodes = (0, _fields.buildPrivateNamesNodes)(privateNamesMap, loose, state);
  106. (0, _fields.transformPrivateNamesUsage)(ref, path, privateNamesMap, loose, state);
  107. let keysNodes, staticNodes, instanceNodes, wrapClass;
  108. if (isDecorated) {
  109. staticNodes = keysNodes = [];
  110. ({
  111. instanceNodes,
  112. wrapClass
  113. } = (0, _decorators.buildDecoratedClass)(ref, path, elements, this.file));
  114. } else {
  115. keysNodes = (0, _misc.extractComputedKeys)(ref, path, computedPaths, this.file);
  116. ({
  117. staticNodes,
  118. instanceNodes,
  119. wrapClass
  120. } = (0, _fields.buildFieldsInitNodes)(ref, path.node.superClass, props, privateNamesMap, state, loose));
  121. }
  122. if (instanceNodes.length > 0) {
  123. (0, _misc.injectInitialization)(path, constructor, instanceNodes, (referenceVisitor, state) => {
  124. if (isDecorated) return;
  125. for (const prop of props) {
  126. if (prop.node.static) continue;
  127. prop.traverse(referenceVisitor, state);
  128. }
  129. });
  130. }
  131. path = wrapClass(path);
  132. path.insertBefore([...privateNamesNodes, ...keysNodes]);
  133. path.insertAfter(staticNodes);
  134. },
  135. PrivateName(path) {
  136. if (this.file.get(versionKey) !== version) return;
  137. throw path.buildCodeFrameError(`Unknown PrivateName "${path}"`);
  138. },
  139. ExportDefaultDeclaration(path) {
  140. if (this.file.get(versionKey) !== version) return;
  141. const decl = path.get("declaration");
  142. if (decl.isClassDeclaration() && (0, _decorators.hasDecorators)(decl.node)) {
  143. if (decl.node.id) {
  144. (0, _helperSplitExportDeclaration.default)(path);
  145. } else {
  146. decl.node.type = "ClassExpression";
  147. }
  148. }
  149. }
  150. }
  151. };
  152. }