features.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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.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. var looseLowPriorityKey = "@babel/plugin-class-features/looseLowPriorityKey/#__internal__@babel/preset-env__please-overwrite-loose-instead-of-throwing";
  21. var canIgnoreLoose = function (file, feature) {
  22. return !!(file.get(looseLowPriorityKey) & feature);
  23. };
  24. function enableFeature(file, feature, loose) {
  25. if (!hasFeature(file, feature) || canIgnoreLoose(file, feature)) {
  26. file.set(featuresKey, file.get(featuresKey) | feature);
  27. if (loose === "#__internal__@babel/preset-env__prefer-true-but-false-is-ok-if-it-prevents-an-error") {
  28. setLoose(file, feature, true);
  29. file.set(looseLowPriorityKey, file.get(looseLowPriorityKey) | feature);
  30. } else if (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. for (const [mask, name] of featuresSameLoose) {
  39. if (!hasFeature(file, mask)) continue;
  40. if (canIgnoreLoose(file, mask)) continue;
  41. const loose = isLoose(file, mask);
  42. if (resolvedLoose === !loose) {
  43. 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)." + "\n\n" + getBabelShowConfigForHint(file));
  44. } else {
  45. resolvedLoose = loose;
  46. var 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-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.` + "\n\n" + getBabelShowConfigForHint(file));
  54. }
  55. }
  56. }
  57. }
  58. function getBabelShowConfigForHint(file) {
  59. let {
  60. filename
  61. } = file.opts;
  62. if (!filename || filename === "unknown") {
  63. filename = "[name of the input file]";
  64. }
  65. return `\
  66. If you already set the same 'loose' mode for these plugins in your config, it's possible that they \
  67. are enabled multiple times with different options.
  68. You can re-run Babel with the BABEL_SHOW_CONFIG_FOR environment variable to show the loaded \
  69. configuration:
  70. \tnpx cross-env BABEL_SHOW_CONFIG_FOR=${filename} <your build command>
  71. See https://babeljs.io/docs/configuration#print-effective-configs for more info.`;
  72. }
  73. function hasFeature(file, feature) {
  74. return !!(file.get(featuresKey) & feature);
  75. }
  76. function isLoose(file, feature) {
  77. return !!(file.get(looseKey) & feature);
  78. }
  79. function setLoose(file, feature, loose) {
  80. if (loose) file.set(looseKey, file.get(looseKey) | feature);else file.set(looseKey, file.get(looseKey) & ~feature);
  81. file.set(looseLowPriorityKey, file.get(looseLowPriorityKey) & ~feature);
  82. }
  83. function shouldTransform(path, file) {
  84. let decoratorPath = null;
  85. let publicFieldPath = null;
  86. let privateFieldPath = null;
  87. let privateMethodPath = null;
  88. let staticBlockPath = null;
  89. if ((0, _decorators.hasOwnDecorators)(path.node)) {
  90. decoratorPath = path.get("decorators.0");
  91. }
  92. for (const el of path.get("body.body")) {
  93. if (!decoratorPath && (0, _decorators.hasOwnDecorators)(el.node)) {
  94. decoratorPath = el.get("decorators.0");
  95. }
  96. if (!publicFieldPath && el.isClassProperty()) {
  97. publicFieldPath = el;
  98. }
  99. if (!privateFieldPath && el.isClassPrivateProperty()) {
  100. privateFieldPath = el;
  101. }
  102. if (!privateMethodPath && el.isClassPrivateMethod != null && el.isClassPrivateMethod()) {
  103. privateMethodPath = el;
  104. }
  105. if (!staticBlockPath && el.isStaticBlock != null && el.isStaticBlock()) {
  106. staticBlockPath = el;
  107. }
  108. }
  109. if (decoratorPath && privateFieldPath) {
  110. throw privateFieldPath.buildCodeFrameError("Private fields in decorated classes are not supported yet.");
  111. }
  112. if (decoratorPath && privateMethodPath) {
  113. throw privateMethodPath.buildCodeFrameError("Private methods in decorated classes are not supported yet.");
  114. }
  115. if (decoratorPath && !hasFeature(file, FEATURES.decorators)) {
  116. 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 }]');
  117. }
  118. if (privateMethodPath && !hasFeature(file, FEATURES.privateMethods)) {
  119. throw privateMethodPath.buildCodeFrameError("Class private methods are not enabled. " + "Please add `@babel/plugin-transform-private-methods` to your configuration.");
  120. }
  121. if ((publicFieldPath || privateFieldPath) && !hasFeature(file, FEATURES.fields) && !hasFeature(file, FEATURES.privateMethods)) {
  122. throw path.buildCodeFrameError("Class fields are not enabled. " + "Please add `@babel/plugin-transform-class-properties` to your configuration.");
  123. }
  124. if (staticBlockPath && !hasFeature(file, FEATURES.staticBlocks)) {
  125. throw path.buildCodeFrameError("Static class blocks are not enabled. " + "Please add `@babel/plugin-transform-class-static-block` to your configuration.");
  126. }
  127. if (decoratorPath || privateMethodPath || staticBlockPath) {
  128. return true;
  129. }
  130. if ((publicFieldPath || privateFieldPath) && hasFeature(file, FEATURES.fields)) {
  131. return true;
  132. }
  133. return false;
  134. }
  135. //# sourceMappingURL=features.js.map