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