features.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.FEATURES = void 0;
  6. exports.enableFeature = enableFeature;
  7. exports.isLoose = isLoose;
  8. exports.shouldTransform = shouldTransform;
  9. var _decorators = require("./decorators");
  10. const FEATURES = Object.freeze({
  11. fields: 1 << 1,
  12. privateMethods: 1 << 2,
  13. decorators: 1 << 3,
  14. privateIn: 1 << 4,
  15. staticBlocks: 1 << 5
  16. });
  17. exports.FEATURES = FEATURES;
  18. const featuresSameLoose = new Map([[FEATURES.fields, "@babel/plugin-proposal-class-properties"], [FEATURES.privateMethods, "@babel/plugin-proposal-private-methods"], [FEATURES.privateIn, "@babel/plugin-proposal-private-property-in-object"]]);
  19. const featuresKey = "@babel/plugin-class-features/featuresKey";
  20. const looseKey = "@babel/plugin-class-features/looseKey";
  21. const looseLowPriorityKey = "@babel/plugin-class-features/looseLowPriorityKey/#__internal__@babel/preset-env__please-overwrite-loose-instead-of-throwing";
  22. function enableFeature(file, feature, loose) {
  23. if (!hasFeature(file, feature) || canIgnoreLoose(file, feature)) {
  24. file.set(featuresKey, file.get(featuresKey) | feature);
  25. if (
  26. loose === "#__internal__@babel/preset-env__prefer-true-but-false-is-ok-if-it-prevents-an-error") {
  27. setLoose(file, feature, true);
  28. file.set(looseLowPriorityKey, file.get(looseLowPriorityKey) | feature);
  29. } else if (
  30. loose === "#__internal__@babel/preset-env__prefer-false-but-true-is-ok-if-it-prevents-an-error") {
  31. setLoose(file, feature, false);
  32. file.set(looseLowPriorityKey, file.get(looseLowPriorityKey) | feature);
  33. } else {
  34. setLoose(file, feature, loose);
  35. }
  36. }
  37. let resolvedLoose;
  38. let higherPriorityPluginName;
  39. for (const [mask, name] of featuresSameLoose) {
  40. if (!hasFeature(file, mask)) continue;
  41. const loose = isLoose(file, mask);
  42. if (canIgnoreLoose(file, mask)) {
  43. continue;
  44. } else if (resolvedLoose === !loose) {
  45. throw new Error("'loose' mode configuration must be the same for @babel/plugin-proposal-class-properties, " + "@babel/plugin-proposal-private-methods and " + "@babel/plugin-proposal-private-property-in-object (when they are enabled).");
  46. } else {
  47. resolvedLoose = loose;
  48. higherPriorityPluginName = name;
  49. }
  50. }
  51. if (resolvedLoose !== undefined) {
  52. for (const [mask, name] of featuresSameLoose) {
  53. if (hasFeature(file, mask) && isLoose(file, mask) !== resolvedLoose) {
  54. setLoose(file, mask, resolvedLoose);
  55. console.warn(`Though the "loose" option was set to "${!resolvedLoose}" in your @babel/preset-env ` + `config, it will not be used for ${name} since the "loose" mode option was set to ` + `"${resolvedLoose}" for ${higherPriorityPluginName}.\nThe "loose" option must be the ` + `same for @babel/plugin-proposal-class-properties, @babel/plugin-proposal-private-methods ` + `and @babel/plugin-proposal-private-property-in-object (when they are enabled): you can ` + `silence this warning by explicitly adding\n` + `\t["${name}", { "loose": ${resolvedLoose} }]\n` + `to the "plugins" section of your Babel config.`);
  56. }
  57. }
  58. }
  59. }
  60. function hasFeature(file, feature) {
  61. return !!(file.get(featuresKey) & feature);
  62. }
  63. function isLoose(file, feature) {
  64. return !!(file.get(looseKey) & feature);
  65. }
  66. function setLoose(file, feature, loose) {
  67. if (loose) file.set(looseKey, file.get(looseKey) | feature);else file.set(looseKey, file.get(looseKey) & ~feature);
  68. file.set(looseLowPriorityKey, file.get(looseLowPriorityKey) & ~feature);
  69. }
  70. function canIgnoreLoose(file, feature) {
  71. return !!(file.get(looseLowPriorityKey) & feature);
  72. }
  73. function shouldTransform(path, file) {
  74. let decoratorPath = null;
  75. let publicFieldPath = null;
  76. let privateFieldPath = null;
  77. let privateMethodPath = null;
  78. let staticBlockPath = null;
  79. if ((0, _decorators.hasOwnDecorators)(path.node)) {
  80. decoratorPath = path.get("decorators.0");
  81. }
  82. for (const el of path.get("body.body")) {
  83. if (!decoratorPath && (0, _decorators.hasOwnDecorators)(el.node)) {
  84. decoratorPath = el.get("decorators.0");
  85. }
  86. if (!publicFieldPath && el.isClassProperty()) {
  87. publicFieldPath = el;
  88. }
  89. if (!privateFieldPath && el.isClassPrivateProperty()) {
  90. privateFieldPath = el;
  91. }
  92. if (!privateMethodPath && el.isClassPrivateMethod != null && el.isClassPrivateMethod()) {
  93. privateMethodPath = el;
  94. }
  95. if (!staticBlockPath && el.isStaticBlock != null && el.isStaticBlock()) {
  96. staticBlockPath = el;
  97. }
  98. }
  99. if (decoratorPath && privateFieldPath) {
  100. throw privateFieldPath.buildCodeFrameError("Private fields in decorated classes are not supported yet.");
  101. }
  102. if (decoratorPath && privateMethodPath) {
  103. throw privateMethodPath.buildCodeFrameError("Private methods in decorated classes are not supported yet.");
  104. }
  105. if (decoratorPath && !hasFeature(file, FEATURES.decorators)) {
  106. throw path.buildCodeFrameError("Decorators are not enabled." + "\nIf you are using " + '["@babel/plugin-proposal-decorators", { "version": "legacy" }], ' + 'make sure it comes *before* "@babel/plugin-proposal-class-properties" ' + "and enable loose mode, like so:\n" + '\t["@babel/plugin-proposal-decorators", { "version": "legacy" }]\n' + '\t["@babel/plugin-proposal-class-properties", { "loose": true }]');
  107. }
  108. if (privateMethodPath && !hasFeature(file, FEATURES.privateMethods)) {
  109. throw privateMethodPath.buildCodeFrameError("Class private methods are not enabled. " + "Please add `@babel/plugin-proposal-private-methods` to your configuration.");
  110. }
  111. if ((publicFieldPath || privateFieldPath) && !hasFeature(file, FEATURES.fields) &&
  112. !hasFeature(file, FEATURES.privateMethods)) {
  113. throw path.buildCodeFrameError("Class fields are not enabled. " + "Please add `@babel/plugin-proposal-class-properties` to your configuration.");
  114. }
  115. if (staticBlockPath && !hasFeature(file, FEATURES.staticBlocks)) {
  116. throw path.buildCodeFrameError("Static class blocks are not enabled. " + "Please add `@babel/plugin-proposal-class-static-block` to your configuration.");
  117. }
  118. if (decoratorPath || privateMethodPath || staticBlockPath) {
  119. return true;
  120. }
  121. if ((publicFieldPath || privateFieldPath) && hasFeature(file, FEATURES.fields)) {
  122. return true;
  123. }
  124. return false;
  125. }
  126. //# sourceMappingURL=features.js.map