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 |
|
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 shouldTransform(path, file) {
|
86 | let decoratorPath = null;
|
87 | let publicFieldPath = null;
|
88 | let privateFieldPath = null;
|
89 | let privateMethodPath = null;
|
90 | let staticBlockPath = null;
|
91 |
|
92 | if ((0, _decorators.hasOwnDecorators)(path.node)) {
|
93 | decoratorPath = path.get("decorators.0");
|
94 | }
|
95 |
|
96 | for (const el of path.get("body.body")) {
|
97 | if (!decoratorPath && (0, _decorators.hasOwnDecorators)(el.node)) {
|
98 | decoratorPath = el.get("decorators.0");
|
99 | }
|
100 |
|
101 | if (!publicFieldPath && el.isClassProperty()) {
|
102 | publicFieldPath = el;
|
103 | }
|
104 |
|
105 | if (!privateFieldPath && el.isClassPrivateProperty()) {
|
106 | privateFieldPath = el;
|
107 | }
|
108 |
|
109 | if (!privateMethodPath && el.isClassPrivateMethod != null && el.isClassPrivateMethod()) {
|
110 | privateMethodPath = el;
|
111 | }
|
112 |
|
113 | if (!staticBlockPath && el.isStaticBlock != null && el.isStaticBlock()) {
|
114 | staticBlockPath = el;
|
115 | }
|
116 | }
|
117 |
|
118 | if (decoratorPath && privateFieldPath) {
|
119 | throw privateFieldPath.buildCodeFrameError("Private fields in decorated classes are not supported yet.");
|
120 | }
|
121 |
|
122 | if (decoratorPath && privateMethodPath) {
|
123 | throw privateMethodPath.buildCodeFrameError("Private methods in decorated classes are not supported yet.");
|
124 | }
|
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-proposal-class-properties" ' + "and enable loose mode, like so:\n" + '\t["@babel/plugin-proposal-decorators", { "version": "legacy" }]\n' + '\t["@babel/plugin-proposal-class-properties", { "loose": true }]');
|
128 | }
|
129 |
|
130 | if (privateMethodPath && !hasFeature(file, FEATURES.privateMethods)) {
|
131 | throw privateMethodPath.buildCodeFrameError("Class private methods are not enabled. " + "Please add `@babel/plugin-proposal-private-methods` to your configuration.");
|
132 | }
|
133 |
|
134 | if ((publicFieldPath || privateFieldPath) && !hasFeature(file, FEATURES.fields) && !hasFeature(file, FEATURES.privateMethods)) {
|
135 | throw path.buildCodeFrameError("Class fields are not enabled. " + "Please add `@babel/plugin-proposal-class-properties` to your configuration.");
|
136 | }
|
137 |
|
138 | if (staticBlockPath && !hasFeature(file, FEATURES.staticBlocks)) {
|
139 | throw path.buildCodeFrameError("Static class blocks are not enabled. " + "Please add `@babel/plugin-proposal-class-static-block` to your configuration.");
|
140 | }
|
141 |
|
142 | if (decoratorPath || privateMethodPath || staticBlockPath) {
|
143 | return true;
|
144 | }
|
145 |
|
146 | if ((publicFieldPath || privateFieldPath) && hasFeature(file, FEATURES.fields)) {
|
147 | return true;
|
148 | }
|
149 |
|
150 | return false;
|
151 | } |
\ | No newline at end of file |