features.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. });
  16. exports.FEATURES = FEATURES;
  17. const featuresSameLoose = new Map([[FEATURES.fields, "@babel/plugin-proposal-class-properties"], [FEATURES.privateMethods, "@babel/plugin-proposal-private-methods"], [FEATURES.privateIn, "@babel/plugin-proposal-private-private-property-in-object"]]);
  18. const featuresKey = "@babel/plugin-class-features/featuresKey";
  19. const looseKey = "@babel/plugin-class-features/looseKey";
  20. const looseLowPriorityKey = "@babel/plugin-class-features/looseLowPriorityKey/#__internal__@babel/preset-env__please-overwrite-loose-instead-of-throwing";
  21. function enableFeature(file, feature, loose) {
  22. if (!hasFeature(file, feature) || canIgnoreLoose(file, feature)) {
  23. file.set(featuresKey, file.get(featuresKey) | feature);
  24. if (loose === "#__internal__@babel/preset-env__prefer-true-but-false-is-ok-if-it-prevents-an-error") {
  25. setLoose(file, feature, true);
  26. file.set(looseLowPriorityKey, file.get(looseLowPriorityKey) | feature);
  27. } else if (loose === "#__internal__@babel/preset-env__prefer-false-but-true-is-ok-if-it-prevents-an-error") {
  28. setLoose(file, feature, false);
  29. file.set(looseLowPriorityKey, file.get(looseLowPriorityKey) | feature);
  30. } else {
  31. setLoose(file, feature, loose);
  32. }
  33. }
  34. let resolvedLoose;
  35. let higherPriorityPluginName;
  36. for (const [mask, name] of featuresSameLoose) {
  37. if (!hasFeature(file, mask)) continue;
  38. const loose = isLoose(file, mask);
  39. if (canIgnoreLoose(file, mask)) {
  40. continue;
  41. } else if (resolvedLoose === !loose) {
  42. 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).");
  43. } else {
  44. resolvedLoose = loose;
  45. higherPriorityPluginName = name;
  46. }
  47. }
  48. if (resolvedLoose !== undefined) {
  49. for (const [mask, name] of featuresSameLoose) {
  50. if (hasFeature(file, mask) && isLoose(file, mask) !== resolvedLoose) {
  51. setLoose(file, mask, resolvedLoose);
  52. 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.`);
  53. }
  54. }
  55. }
  56. }
  57. function hasFeature(file, feature) {
  58. return !!(file.get(featuresKey) & feature);
  59. }
  60. function isLoose(file, feature) {
  61. return !!(file.get(looseKey) & feature);
  62. }
  63. function setLoose(file, feature, loose) {
  64. if (loose) file.set(looseKey, file.get(looseKey) | feature);else file.set(looseKey, file.get(looseKey) & ~feature);
  65. file.set(looseLowPriorityKey, file.get(looseLowPriorityKey) & ~feature);
  66. }
  67. function canIgnoreLoose(file, feature) {
  68. return !!(file.get(looseLowPriorityKey) & feature);
  69. }
  70. function verifyUsedFeatures(path, file) {
  71. if ((0, _decorators.hasOwnDecorators)(path.node)) {
  72. if (!hasFeature(file, FEATURES.decorators)) {
  73. 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 }]');
  74. }
  75. if (path.isPrivate()) {
  76. throw path.buildCodeFrameError(`Private ${path.isClassMethod() ? "methods" : "fields"} in decorated classes are not supported yet.`);
  77. }
  78. }
  79. if (path.isPrivate() && path.isMethod()) {
  80. if (!hasFeature(file, FEATURES.privateMethods)) {
  81. throw path.buildCodeFrameError("Class private methods are not enabled.");
  82. }
  83. }
  84. if (path.isPrivateName() && path.parentPath.isBinaryExpression({
  85. operator: "in",
  86. left: path.node
  87. })) {
  88. if (!hasFeature(file, FEATURES.privateIn)) {
  89. throw path.buildCodeFrameError("Private property in checks are not enabled.");
  90. }
  91. }
  92. if (path.isProperty()) {
  93. if (!hasFeature(file, FEATURES.fields)) {
  94. throw path.buildCodeFrameError("Class fields are not enabled.");
  95. }
  96. }
  97. }