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