features.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.enableFeature = enableFeature;
  6. exports.isLoose = isLoose;
  7. exports.verifyUsedFeatures = verifyUsedFeatures;
  8. exports.FEATURES = void 0;
  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 verifyUsedFeatures(path, file) {
  72. if ((0, _decorators.hasOwnDecorators)(path.node)) {
  73. if (!hasFeature(file, FEATURES.decorators)) {
  74. throw path.buildCodeFrameError("Decorators are not enabled." + "\nIf you are using " + '["@babel/plugin-proposal-decorators", { "legacy": true }], ' + 'make sure it comes *before* "@babel/plugin-proposal-class-properties" ' + "and enable loose mode, like so:\n" + '\t["@babel/plugin-proposal-decorators", { "legacy": true }]\n' + '\t["@babel/plugin-proposal-class-properties", { "loose": true }]');
  75. }
  76. if (path.isPrivate()) {
  77. throw path.buildCodeFrameError(`Private ${path.isClassMethod() ? "methods" : "fields"} in decorated classes are not supported yet.`);
  78. }
  79. }
  80. if (path.isPrivateMethod != null && path.isPrivateMethod()) {
  81. if (!hasFeature(file, FEATURES.privateMethods)) {
  82. throw path.buildCodeFrameError("Class private methods are not enabled.");
  83. }
  84. }
  85. if (path.isPrivateName() && path.parentPath.isBinaryExpression({
  86. operator: "in",
  87. left: path.node
  88. })) {
  89. if (!hasFeature(file, FEATURES.privateIn)) {
  90. throw path.buildCodeFrameError("Private property in checks are not enabled.");
  91. }
  92. }
  93. if (path.isProperty()) {
  94. if (!hasFeature(file, FEATURES.fields)) {
  95. throw path.buildCodeFrameError("Class fields are not enabled.");
  96. }
  97. }
  98. if (path.isStaticBlock != null && path.isStaticBlock()) {
  99. if (!hasFeature(file, FEATURES.staticBlocks)) {
  100. throw path.buildCodeFrameError("Static class blocks are not enabled. " + "Please add `@babel/plugin-proposal-class-static-block` to your configuration.");
  101. }
  102. }
  103. }