index.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. function _helperFunctionName() {
  19. const data = _interopRequireDefault(require("@babel/helper-function-name"));
  20. _helperFunctionName = function () {
  21. return data;
  22. };
  23. return data;
  24. }
  25. function _helperSplitExportDeclaration() {
  26. const data = _interopRequireDefault(require("@babel/helper-split-export-declaration"));
  27. _helperSplitExportDeclaration = function () {
  28. return data;
  29. };
  30. return data;
  31. }
  32. var _fields = require("./fields");
  33. var _decorators = require("./decorators");
  34. var _misc = require("./misc");
  35. var _features = require("./features");
  36. var _package = _interopRequireDefault(require("../package.json"));
  37. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  38. const version = _package.default.version.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. }) {
  46. return {
  47. name,
  48. manipulateOptions,
  49. pre() {
  50. (0, _features.enableFeature)(this.file, feature, loose);
  51. if (!this.file.get(versionKey) || this.file.get(versionKey) < version) {
  52. this.file.set(versionKey, version);
  53. }
  54. },
  55. visitor: {
  56. Class(path, state) {
  57. if (this.file.get(versionKey) !== version) return;
  58. (0, _features.verifyUsedFeatures)(path, this.file);
  59. const loose = (0, _features.isLoose)(this.file, feature);
  60. let constructor;
  61. let isDecorated = (0, _decorators.hasOwnDecorators)(path.node);
  62. const props = [];
  63. const elements = [];
  64. const computedPaths = [];
  65. const privateNames = new Set();
  66. const body = path.get("body");
  67. for (const path of body.get("body")) {
  68. (0, _features.verifyUsedFeatures)(path, this.file);
  69. if (path.node.computed) {
  70. computedPaths.push(path);
  71. }
  72. if (path.isPrivate()) {
  73. const {
  74. name
  75. } = path.node.key.id;
  76. const getName = `get ${name}`;
  77. const setName = `set ${name}`;
  78. if (path.node.kind === "get") {
  79. if (privateNames.has(getName) || privateNames.has(name) && !privateNames.has(setName)) {
  80. throw path.buildCodeFrameError("Duplicate private field");
  81. }
  82. privateNames.add(getName).add(name);
  83. } else if (path.node.kind === "set") {
  84. if (privateNames.has(setName) || privateNames.has(name) && !privateNames.has(getName)) {
  85. throw path.buildCodeFrameError("Duplicate private field");
  86. }
  87. privateNames.add(setName).add(name);
  88. } else {
  89. if (privateNames.has(name) && !privateNames.has(getName) && !privateNames.has(setName) || privateNames.has(name) && (privateNames.has(getName) || privateNames.has(setName))) {
  90. throw path.buildCodeFrameError("Duplicate private field");
  91. }
  92. privateNames.add(name);
  93. }
  94. }
  95. if (path.isClassMethod({
  96. kind: "constructor"
  97. })) {
  98. constructor = path;
  99. } else {
  100. elements.push(path);
  101. if (path.isProperty() || path.isPrivate()) {
  102. props.push(path);
  103. }
  104. }
  105. if (!isDecorated) isDecorated = (0, _decorators.hasOwnDecorators)(path.node);
  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 = 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(keysNodes);
  144. path.insertAfter([...privateNamesNodes, ...staticNodes]);
  145. },
  146. PrivateName(path) {
  147. if (this.file.get(versionKey) !== version) return;
  148. throw path.buildCodeFrameError(`Unknown PrivateName "${path}"`);
  149. },
  150. ExportDefaultDeclaration(path) {
  151. if (this.file.get(versionKey) !== version) return;
  152. const decl = path.get("declaration");
  153. if (decl.isClassDeclaration() && (0, _decorators.hasDecorators)(decl.node)) {
  154. if (decl.node.id) {
  155. (0, _helperSplitExportDeclaration().default)(path);
  156. } else {
  157. decl.node.type = "ClassExpression";
  158. }
  159. }
  160. }
  161. }
  162. };
  163. }