1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.msg = msg;
|
---|
7 | exports.access = access;
|
---|
8 | exports.assertRootMode = assertRootMode;
|
---|
9 | exports.assertSourceMaps = assertSourceMaps;
|
---|
10 | exports.assertCompact = assertCompact;
|
---|
11 | exports.assertSourceType = assertSourceType;
|
---|
12 | exports.assertCallerMetadata = assertCallerMetadata;
|
---|
13 | exports.assertInputSourceMap = assertInputSourceMap;
|
---|
14 | exports.assertString = assertString;
|
---|
15 | exports.assertFunction = assertFunction;
|
---|
16 | exports.assertBoolean = assertBoolean;
|
---|
17 | exports.assertObject = assertObject;
|
---|
18 | exports.assertArray = assertArray;
|
---|
19 | exports.assertIgnoreList = assertIgnoreList;
|
---|
20 | exports.assertConfigApplicableTest = assertConfigApplicableTest;
|
---|
21 | exports.assertConfigFileSearch = assertConfigFileSearch;
|
---|
22 | exports.assertBabelrcSearch = assertBabelrcSearch;
|
---|
23 | exports.assertPluginList = assertPluginList;
|
---|
24 | exports.assertTargets = assertTargets;
|
---|
25 | exports.assertAssumptions = assertAssumptions;
|
---|
26 |
|
---|
27 | function _helperCompilationTargets() {
|
---|
28 | const data = require("@babel/helper-compilation-targets");
|
---|
29 |
|
---|
30 | _helperCompilationTargets = function () {
|
---|
31 | return data;
|
---|
32 | };
|
---|
33 |
|
---|
34 | return data;
|
---|
35 | }
|
---|
36 |
|
---|
37 | var _options = require("./options");
|
---|
38 |
|
---|
39 | function msg(loc) {
|
---|
40 | switch (loc.type) {
|
---|
41 | case "root":
|
---|
42 | return ``;
|
---|
43 |
|
---|
44 | case "env":
|
---|
45 | return `${msg(loc.parent)}.env["${loc.name}"]`;
|
---|
46 |
|
---|
47 | case "overrides":
|
---|
48 | return `${msg(loc.parent)}.overrides[${loc.index}]`;
|
---|
49 |
|
---|
50 | case "option":
|
---|
51 | return `${msg(loc.parent)}.${loc.name}`;
|
---|
52 |
|
---|
53 | case "access":
|
---|
54 | return `${msg(loc.parent)}[${JSON.stringify(loc.name)}]`;
|
---|
55 |
|
---|
56 | default:
|
---|
57 | throw new Error(`Assertion failure: Unknown type ${loc.type}`);
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | function access(loc, name) {
|
---|
62 | return {
|
---|
63 | type: "access",
|
---|
64 | name,
|
---|
65 | parent: loc
|
---|
66 | };
|
---|
67 | }
|
---|
68 |
|
---|
69 | function assertRootMode(loc, value) {
|
---|
70 | if (value !== undefined && value !== "root" && value !== "upward" && value !== "upward-optional") {
|
---|
71 | throw new Error(`${msg(loc)} must be a "root", "upward", "upward-optional" or undefined`);
|
---|
72 | }
|
---|
73 |
|
---|
74 | return value;
|
---|
75 | }
|
---|
76 |
|
---|
77 | function assertSourceMaps(loc, value) {
|
---|
78 | if (value !== undefined && typeof value !== "boolean" && value !== "inline" && value !== "both") {
|
---|
79 | throw new Error(`${msg(loc)} must be a boolean, "inline", "both", or undefined`);
|
---|
80 | }
|
---|
81 |
|
---|
82 | return value;
|
---|
83 | }
|
---|
84 |
|
---|
85 | function assertCompact(loc, value) {
|
---|
86 | if (value !== undefined && typeof value !== "boolean" && value !== "auto") {
|
---|
87 | throw new Error(`${msg(loc)} must be a boolean, "auto", or undefined`);
|
---|
88 | }
|
---|
89 |
|
---|
90 | return value;
|
---|
91 | }
|
---|
92 |
|
---|
93 | function assertSourceType(loc, value) {
|
---|
94 | if (value !== undefined && value !== "module" && value !== "script" && value !== "unambiguous") {
|
---|
95 | throw new Error(`${msg(loc)} must be "module", "script", "unambiguous", or undefined`);
|
---|
96 | }
|
---|
97 |
|
---|
98 | return value;
|
---|
99 | }
|
---|
100 |
|
---|
101 | function assertCallerMetadata(loc, value) {
|
---|
102 | const obj = assertObject(loc, value);
|
---|
103 |
|
---|
104 | if (obj) {
|
---|
105 | if (typeof obj.name !== "string") {
|
---|
106 | throw new Error(`${msg(loc)} set but does not contain "name" property string`);
|
---|
107 | }
|
---|
108 |
|
---|
109 | for (const prop of Object.keys(obj)) {
|
---|
110 | const propLoc = access(loc, prop);
|
---|
111 | const value = obj[prop];
|
---|
112 |
|
---|
113 | if (value != null && typeof value !== "boolean" && typeof value !== "string" && typeof value !== "number") {
|
---|
114 | throw new Error(`${msg(propLoc)} must be null, undefined, a boolean, a string, or a number.`);
|
---|
115 | }
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | return value;
|
---|
120 | }
|
---|
121 |
|
---|
122 | function assertInputSourceMap(loc, value) {
|
---|
123 | if (value !== undefined && typeof value !== "boolean" && (typeof value !== "object" || !value)) {
|
---|
124 | throw new Error(`${msg(loc)} must be a boolean, object, or undefined`);
|
---|
125 | }
|
---|
126 |
|
---|
127 | return value;
|
---|
128 | }
|
---|
129 |
|
---|
130 | function assertString(loc, value) {
|
---|
131 | if (value !== undefined && typeof value !== "string") {
|
---|
132 | throw new Error(`${msg(loc)} must be a string, or undefined`);
|
---|
133 | }
|
---|
134 |
|
---|
135 | return value;
|
---|
136 | }
|
---|
137 |
|
---|
138 | function assertFunction(loc, value) {
|
---|
139 | if (value !== undefined && typeof value !== "function") {
|
---|
140 | throw new Error(`${msg(loc)} must be a function, or undefined`);
|
---|
141 | }
|
---|
142 |
|
---|
143 | return value;
|
---|
144 | }
|
---|
145 |
|
---|
146 | function assertBoolean(loc, value) {
|
---|
147 | if (value !== undefined && typeof value !== "boolean") {
|
---|
148 | throw new Error(`${msg(loc)} must be a boolean, or undefined`);
|
---|
149 | }
|
---|
150 |
|
---|
151 | return value;
|
---|
152 | }
|
---|
153 |
|
---|
154 | function assertObject(loc, value) {
|
---|
155 | if (value !== undefined && (typeof value !== "object" || Array.isArray(value) || !value)) {
|
---|
156 | throw new Error(`${msg(loc)} must be an object, or undefined`);
|
---|
157 | }
|
---|
158 |
|
---|
159 | return value;
|
---|
160 | }
|
---|
161 |
|
---|
162 | function assertArray(loc, value) {
|
---|
163 | if (value != null && !Array.isArray(value)) {
|
---|
164 | throw new Error(`${msg(loc)} must be an array, or undefined`);
|
---|
165 | }
|
---|
166 |
|
---|
167 | return value;
|
---|
168 | }
|
---|
169 |
|
---|
170 | function assertIgnoreList(loc, value) {
|
---|
171 | const arr = assertArray(loc, value);
|
---|
172 |
|
---|
173 | if (arr) {
|
---|
174 | arr.forEach((item, i) => assertIgnoreItem(access(loc, i), item));
|
---|
175 | }
|
---|
176 |
|
---|
177 | return arr;
|
---|
178 | }
|
---|
179 |
|
---|
180 | function assertIgnoreItem(loc, value) {
|
---|
181 | if (typeof value !== "string" && typeof value !== "function" && !(value instanceof RegExp)) {
|
---|
182 | throw new Error(`${msg(loc)} must be an array of string/Function/RegExp values, or undefined`);
|
---|
183 | }
|
---|
184 |
|
---|
185 | return value;
|
---|
186 | }
|
---|
187 |
|
---|
188 | function assertConfigApplicableTest(loc, value) {
|
---|
189 | if (value === undefined) return value;
|
---|
190 |
|
---|
191 | if (Array.isArray(value)) {
|
---|
192 | value.forEach((item, i) => {
|
---|
193 | if (!checkValidTest(item)) {
|
---|
194 | throw new Error(`${msg(access(loc, i))} must be a string/Function/RegExp.`);
|
---|
195 | }
|
---|
196 | });
|
---|
197 | } else if (!checkValidTest(value)) {
|
---|
198 | throw new Error(`${msg(loc)} must be a string/Function/RegExp, or an array of those`);
|
---|
199 | }
|
---|
200 |
|
---|
201 | return value;
|
---|
202 | }
|
---|
203 |
|
---|
204 | function checkValidTest(value) {
|
---|
205 | return typeof value === "string" || typeof value === "function" || value instanceof RegExp;
|
---|
206 | }
|
---|
207 |
|
---|
208 | function assertConfigFileSearch(loc, value) {
|
---|
209 | if (value !== undefined && typeof value !== "boolean" && typeof value !== "string") {
|
---|
210 | throw new Error(`${msg(loc)} must be a undefined, a boolean, a string, ` + `got ${JSON.stringify(value)}`);
|
---|
211 | }
|
---|
212 |
|
---|
213 | return value;
|
---|
214 | }
|
---|
215 |
|
---|
216 | function assertBabelrcSearch(loc, value) {
|
---|
217 | if (value === undefined || typeof value === "boolean") return value;
|
---|
218 |
|
---|
219 | if (Array.isArray(value)) {
|
---|
220 | value.forEach((item, i) => {
|
---|
221 | if (!checkValidTest(item)) {
|
---|
222 | throw new Error(`${msg(access(loc, i))} must be a string/Function/RegExp.`);
|
---|
223 | }
|
---|
224 | });
|
---|
225 | } else if (!checkValidTest(value)) {
|
---|
226 | throw new Error(`${msg(loc)} must be a undefined, a boolean, a string/Function/RegExp ` + `or an array of those, got ${JSON.stringify(value)}`);
|
---|
227 | }
|
---|
228 |
|
---|
229 | return value;
|
---|
230 | }
|
---|
231 |
|
---|
232 | function assertPluginList(loc, value) {
|
---|
233 | const arr = assertArray(loc, value);
|
---|
234 |
|
---|
235 | if (arr) {
|
---|
236 | arr.forEach((item, i) => assertPluginItem(access(loc, i), item));
|
---|
237 | }
|
---|
238 |
|
---|
239 | return arr;
|
---|
240 | }
|
---|
241 |
|
---|
242 | function assertPluginItem(loc, value) {
|
---|
243 | if (Array.isArray(value)) {
|
---|
244 | if (value.length === 0) {
|
---|
245 | throw new Error(`${msg(loc)} must include an object`);
|
---|
246 | }
|
---|
247 |
|
---|
248 | if (value.length > 3) {
|
---|
249 | throw new Error(`${msg(loc)} may only be a two-tuple or three-tuple`);
|
---|
250 | }
|
---|
251 |
|
---|
252 | assertPluginTarget(access(loc, 0), value[0]);
|
---|
253 |
|
---|
254 | if (value.length > 1) {
|
---|
255 | const opts = value[1];
|
---|
256 |
|
---|
257 | if (opts !== undefined && opts !== false && (typeof opts !== "object" || Array.isArray(opts) || opts === null)) {
|
---|
258 | throw new Error(`${msg(access(loc, 1))} must be an object, false, or undefined`);
|
---|
259 | }
|
---|
260 | }
|
---|
261 |
|
---|
262 | if (value.length === 3) {
|
---|
263 | const name = value[2];
|
---|
264 |
|
---|
265 | if (name !== undefined && typeof name !== "string") {
|
---|
266 | throw new Error(`${msg(access(loc, 2))} must be a string, or undefined`);
|
---|
267 | }
|
---|
268 | }
|
---|
269 | } else {
|
---|
270 | assertPluginTarget(loc, value);
|
---|
271 | }
|
---|
272 |
|
---|
273 | return value;
|
---|
274 | }
|
---|
275 |
|
---|
276 | function assertPluginTarget(loc, value) {
|
---|
277 | if ((typeof value !== "object" || !value) && typeof value !== "string" && typeof value !== "function") {
|
---|
278 | throw new Error(`${msg(loc)} must be a string, object, function`);
|
---|
279 | }
|
---|
280 |
|
---|
281 | return value;
|
---|
282 | }
|
---|
283 |
|
---|
284 | function assertTargets(loc, value) {
|
---|
285 | if ((0, _helperCompilationTargets().isBrowsersQueryValid)(value)) return value;
|
---|
286 |
|
---|
287 | if (typeof value !== "object" || !value || Array.isArray(value)) {
|
---|
288 | throw new Error(`${msg(loc)} must be a string, an array of strings or an object`);
|
---|
289 | }
|
---|
290 |
|
---|
291 | const browsersLoc = access(loc, "browsers");
|
---|
292 | const esmodulesLoc = access(loc, "esmodules");
|
---|
293 | assertBrowsersList(browsersLoc, value.browsers);
|
---|
294 | assertBoolean(esmodulesLoc, value.esmodules);
|
---|
295 |
|
---|
296 | for (const key of Object.keys(value)) {
|
---|
297 | const val = value[key];
|
---|
298 | const subLoc = access(loc, key);
|
---|
299 | if (key === "esmodules") assertBoolean(subLoc, val);else if (key === "browsers") assertBrowsersList(subLoc, val);else if (!Object.hasOwnProperty.call(_helperCompilationTargets().TargetNames, key)) {
|
---|
300 | const validTargets = Object.keys(_helperCompilationTargets().TargetNames).join(", ");
|
---|
301 | throw new Error(`${msg(subLoc)} is not a valid target. Supported targets are ${validTargets}`);
|
---|
302 | } else assertBrowserVersion(subLoc, val);
|
---|
303 | }
|
---|
304 |
|
---|
305 | return value;
|
---|
306 | }
|
---|
307 |
|
---|
308 | function assertBrowsersList(loc, value) {
|
---|
309 | if (value !== undefined && !(0, _helperCompilationTargets().isBrowsersQueryValid)(value)) {
|
---|
310 | throw new Error(`${msg(loc)} must be undefined, a string or an array of strings`);
|
---|
311 | }
|
---|
312 | }
|
---|
313 |
|
---|
314 | function assertBrowserVersion(loc, value) {
|
---|
315 | if (typeof value === "number" && Math.round(value) === value) return;
|
---|
316 | if (typeof value === "string") return;
|
---|
317 | throw new Error(`${msg(loc)} must be a string or an integer number`);
|
---|
318 | }
|
---|
319 |
|
---|
320 | function assertAssumptions(loc, value) {
|
---|
321 | if (value === undefined) return;
|
---|
322 |
|
---|
323 | if (typeof value !== "object" || value === null) {
|
---|
324 | throw new Error(`${msg(loc)} must be an object or undefined.`);
|
---|
325 | }
|
---|
326 |
|
---|
327 | let root = loc;
|
---|
328 |
|
---|
329 | do {
|
---|
330 | root = root.parent;
|
---|
331 | } while (root.type !== "root");
|
---|
332 |
|
---|
333 | const inPreset = root.source === "preset";
|
---|
334 |
|
---|
335 | for (const name of Object.keys(value)) {
|
---|
336 | const subLoc = access(loc, name);
|
---|
337 |
|
---|
338 | if (!_options.assumptionsNames.has(name)) {
|
---|
339 | throw new Error(`${msg(subLoc)} is not a supported assumption.`);
|
---|
340 | }
|
---|
341 |
|
---|
342 | if (typeof value[name] !== "boolean") {
|
---|
343 | throw new Error(`${msg(subLoc)} must be a boolean.`);
|
---|
344 | }
|
---|
345 |
|
---|
346 | if (inPreset && value[name] === false) {
|
---|
347 | throw new Error(`${msg(subLoc)} cannot be set to 'false' inside presets.`);
|
---|
348 | }
|
---|
349 | }
|
---|
350 |
|
---|
351 | return value;
|
---|
352 | } |
---|