features.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 (loose === "#__internal__@babel/preset-env__prefer-true-but-false-is-ok-if-it-prevents-an-error") {
  26. setLoose(file, feature, true);
  27. file.set(looseLowPriorityKey, file.get(looseLowPriorityKey) | feature);
  28. } else if (loose === "#__internal__@babel/preset-env__prefer-false-but-true-is-ok-if-it-prevents-an-error") {
  29. setLoose(file, feature, false);
  30. file.set(looseLowPriorityKey, file.get(looseLowPriorityKey) | feature);
  31. } else {
  32. setLoose(file, feature, loose);
  33. }
  34. }
  35. let resolvedLoose;
  36. let higherPriorityPluginName;
  37. for (const [mask, name] of featuresSameLoose) {
  38. if (!hasFeature(file, mask)) continue;
  39. const loose = isLoose(file, mask);
  40. if (canIgnoreLoose(file, mask)) {
  41. continue;
  42. } else if (resolvedLoose === !loose) {
  43. 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).");
  44. } else {
  45. resolvedLoose = loose;
  46. higherPriorityPluginName = name;
  47. }
  48. }
  49. if (resolvedLoose !== undefined) {
  50. for (const [mask, name] of featuresSameLoose) {
  51. if (hasFeature(file, mask) && isLoose(file, mask) !== resolvedLoose) {
  52. setLoose(file, mask, resolvedLoose);
  53. 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.`);
  54. }
  55. }
  56. }
  57. }
  58. function hasFeature(file, feature) {
  59. return !!(file.get(featuresKey) & feature);
  60. }
  61. function isLoose(file, feature) {
  62. return !!(file.get(looseKey) & feature);
  63. }
  64. function setLoose(file, feature, loose) {
  65. if (loose) file.set(looseKey, file.get(looseKey) | feature);else file.set(looseKey, file.get(looseKey) & ~feature);
  66. file.set(looseLowPriorityKey, file.get(looseLowPriorityKey) & ~feature);
  67. }
  68. function canIgnoreLoose(file, feature) {
  69. return !!(file.get(looseLowPriorityKey) & feature);
  70. }
  71. function shouldTransform(path, file) {
  72. let decoratorPath = null;
  73. let publicFieldPath = null;
  74. let privateFieldPath = null;
  75. let privateMethodPath = null;
  76. let staticBlockPath = null;
  77. if ((0, _decorators.hasOwnDecorators)(path.node)) {
  78. decoratorPath = path.get("decorators.0");
  79. }
  80. for (const el of path.get("body.body")) {
  81. if (!decoratorPath && (0, _decorators.hasOwnDecorators)(el.node)) {
  82. decoratorPath = el.get("decorators.0");
  83. }
  84. if (!publicFieldPath && el.isClassProperty()) {
  85. publicFieldPath = el;
  86. }
  87. if (!privateFieldPath && el.isClassPrivateProperty()) {
  88. privateFieldPath = el;
  89. }
  90. if (!privateMethodPath && el.isClassPrivateMethod != null && el.isClassPrivateMethod()) {
  91. privateMethodPath = el;
  92. }
  93. if (!staticBlockPath && el.isStaticBlock != null && el.isStaticBlock()) {
  94. staticBlockPath = el;
  95. }
  96. }
  97. if (decoratorPath && privateFieldPath) {
  98. throw privateFieldPath.buildCodeFrameError("Private fields in decorated classes are not supported yet.");
  99. }
  100. if (decoratorPath && privateMethodPath) {
  101. throw privateMethodPath.buildCodeFrameError("Private methods in decorated classes are not supported yet.");
  102. }
  103. if (decoratorPath && !hasFeature(file, FEATURES.decorators)) {
  104. 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 }]');
  105. }
  106. if (privateMethodPath && !hasFeature(file, FEATURES.privateMethods)) {
  107. throw privateMethodPath.buildCodeFrameError("Class private methods are not enabled. " + "Please add `@babel/plugin-proposal-private-methods` to your configuration.");
  108. }
  109. if ((publicFieldPath || privateFieldPath) && !hasFeature(file, FEATURES.fields) && !hasFeature(file, FEATURES.privateMethods)) {
  110. throw path.buildCodeFrameError("Class fields are not enabled. " + "Please add `@babel/plugin-proposal-class-properties` to your configuration.");
  111. }
  112. if (staticBlockPath && !hasFeature(file, FEATURES.staticBlocks)) {
  113. throw path.buildCodeFrameError("Static class blocks are not enabled. " + "Please add `@babel/plugin-proposal-class-static-block` to your configuration.");
  114. }
  115. if (decoratorPath || privateMethodPath || staticBlockPath) {
  116. return true;
  117. }
  118. if ((publicFieldPath || privateFieldPath) && hasFeature(file, FEATURES.fields)) {
  119. return true;
  120. }
  121. return false;
  122. }