Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

187 строки
5.8 KiB

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