[6a3a178] | 1 | "use strict";
|
---|
| 2 |
|
---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
---|
| 4 | value: true
|
---|
| 5 | });
|
---|
| 6 | exports.enableFeature = enableFeature;
|
---|
| 7 | exports.isLoose = isLoose;
|
---|
| 8 | exports.verifyUsedFeatures = verifyUsedFeatures;
|
---|
| 9 | exports.FEATURES = void 0;
|
---|
| 10 |
|
---|
| 11 | var _decorators = require("./decorators");
|
---|
| 12 |
|
---|
| 13 | const FEATURES = Object.freeze({
|
---|
| 14 | fields: 1 << 1,
|
---|
| 15 | privateMethods: 1 << 2,
|
---|
| 16 | decorators: 1 << 3,
|
---|
| 17 | privateIn: 1 << 4,
|
---|
| 18 | staticBlocks: 1 << 5
|
---|
| 19 | });
|
---|
| 20 | exports.FEATURES = FEATURES;
|
---|
| 21 | const featuresSameLoose = new Map([[FEATURES.fields, "@babel/plugin-proposal-class-properties"], [FEATURES.privateMethods, "@babel/plugin-proposal-private-methods"], [FEATURES.privateIn, "@babel/plugin-proposal-private-property-in-object"]]);
|
---|
| 22 | const featuresKey = "@babel/plugin-class-features/featuresKey";
|
---|
| 23 | const looseKey = "@babel/plugin-class-features/looseKey";
|
---|
| 24 | const looseLowPriorityKey = "@babel/plugin-class-features/looseLowPriorityKey/#__internal__@babel/preset-env__please-overwrite-loose-instead-of-throwing";
|
---|
| 25 |
|
---|
| 26 | function enableFeature(file, feature, loose) {
|
---|
| 27 | if (!hasFeature(file, feature) || canIgnoreLoose(file, feature)) {
|
---|
| 28 | file.set(featuresKey, file.get(featuresKey) | feature);
|
---|
| 29 |
|
---|
| 30 | if (loose === "#__internal__@babel/preset-env__prefer-true-but-false-is-ok-if-it-prevents-an-error") {
|
---|
| 31 | setLoose(file, feature, true);
|
---|
| 32 | file.set(looseLowPriorityKey, file.get(looseLowPriorityKey) | feature);
|
---|
| 33 | } else if (loose === "#__internal__@babel/preset-env__prefer-false-but-true-is-ok-if-it-prevents-an-error") {
|
---|
| 34 | setLoose(file, feature, false);
|
---|
| 35 | file.set(looseLowPriorityKey, file.get(looseLowPriorityKey) | feature);
|
---|
| 36 | } else {
|
---|
| 37 | setLoose(file, feature, loose);
|
---|
| 38 | }
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | let resolvedLoose;
|
---|
| 42 | let higherPriorityPluginName;
|
---|
| 43 |
|
---|
| 44 | for (const [mask, name] of featuresSameLoose) {
|
---|
| 45 | if (!hasFeature(file, mask)) continue;
|
---|
| 46 | const loose = isLoose(file, mask);
|
---|
| 47 |
|
---|
| 48 | if (canIgnoreLoose(file, mask)) {
|
---|
| 49 | continue;
|
---|
| 50 | } else if (resolvedLoose === !loose) {
|
---|
| 51 | 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).");
|
---|
| 52 | } else {
|
---|
| 53 | resolvedLoose = loose;
|
---|
| 54 | higherPriorityPluginName = name;
|
---|
| 55 | }
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | if (resolvedLoose !== undefined) {
|
---|
| 59 | for (const [mask, name] of featuresSameLoose) {
|
---|
| 60 | if (hasFeature(file, mask) && isLoose(file, mask) !== resolvedLoose) {
|
---|
| 61 | setLoose(file, mask, resolvedLoose);
|
---|
| 62 | 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.`);
|
---|
| 63 | }
|
---|
| 64 | }
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | function hasFeature(file, feature) {
|
---|
| 69 | return !!(file.get(featuresKey) & feature);
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | function isLoose(file, feature) {
|
---|
| 73 | return !!(file.get(looseKey) & feature);
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | function setLoose(file, feature, loose) {
|
---|
| 77 | if (loose) file.set(looseKey, file.get(looseKey) | feature);else file.set(looseKey, file.get(looseKey) & ~feature);
|
---|
| 78 | file.set(looseLowPriorityKey, file.get(looseLowPriorityKey) & ~feature);
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | function canIgnoreLoose(file, feature) {
|
---|
| 82 | return !!(file.get(looseLowPriorityKey) & feature);
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | function verifyUsedFeatures(path, file) {
|
---|
| 86 | if ((0, _decorators.hasOwnDecorators)(path.node)) {
|
---|
| 87 | if (!hasFeature(file, FEATURES.decorators)) {
|
---|
| 88 | 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 }]');
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | if (path.isPrivate()) {
|
---|
| 92 | throw path.buildCodeFrameError(`Private ${path.isClassMethod() ? "methods" : "fields"} in decorated classes are not supported yet.`);
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | if (path.isClassPrivateMethod != null && path.isClassPrivateMethod()) {
|
---|
| 97 | if (!hasFeature(file, FEATURES.privateMethods)) {
|
---|
| 98 | throw path.buildCodeFrameError("Class private methods are not enabled.");
|
---|
| 99 | }
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | if (path.isPrivateName() && path.parentPath.isBinaryExpression({
|
---|
| 103 | operator: "in",
|
---|
| 104 | left: path.node
|
---|
| 105 | })) {
|
---|
| 106 | if (!hasFeature(file, FEATURES.privateIn)) {
|
---|
| 107 | throw path.buildCodeFrameError("Private property in checks are not enabled.");
|
---|
| 108 | }
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | if (path.isProperty()) {
|
---|
| 112 | if (!hasFeature(file, FEATURES.fields)) {
|
---|
| 113 | throw path.buildCodeFrameError("Class fields are not enabled.");
|
---|
| 114 | }
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | if (path.isStaticBlock != null && path.isStaticBlock()) {
|
---|
| 118 | if (!hasFeature(file, FEATURES.staticBlocks)) {
|
---|
| 119 | throw path.buildCodeFrameError("Static class blocks are not enabled. " + "Please add `@babel/plugin-proposal-class-static-block` to your configuration.");
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
| 122 | } |
---|