features.js 6.2 KB

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