features.js 6.9 KB

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