| 1 |
import { |
| 2 |
isBrowsersQueryValid, |
| 3 |
TargetNames, |
| 4 |
} from "@babel/helper-compilation-targets"; |
| 5 |
|
| 6 |
import type { |
| 7 |
ConfigFileSearch, |
| 8 |
BabelrcSearch, |
| 9 |
IgnoreList, |
| 10 |
IgnoreItem, |
| 11 |
PluginList, |
| 12 |
PluginItem, |
| 13 |
PluginTarget, |
| 14 |
ConfigApplicableTest, |
| 15 |
SourceMapsOption, |
| 16 |
SourceTypeOption, |
| 17 |
CompactOption, |
| 18 |
RootInputSourceMapOption, |
| 19 |
NestingPath, |
| 20 |
CallerMetadata, |
| 21 |
RootMode, |
| 22 |
TargetsListOrObject, |
| 23 |
AssumptionName, |
| 24 |
} from "./options"; |
| 25 |
|
| 26 |
import { assumptionsNames } from "./options"; |
| 27 |
|
| 28 |
export type { RootPath } from "./options"; |
| 29 |
|
| 30 |
export type ValidatorSet = { |
| 31 |
[name: string]: Validator<any>; |
| 32 |
}; |
| 33 |
|
| 34 |
export type Validator<T> = (loc: OptionPath, value: unknown) => T; |
| 35 |
|
| 36 |
export function msg(loc: NestingPath | GeneralPath): string { |
| 37 |
switch (loc.type) { |
| 38 |
case "root": |
| 39 |
return ``; |
| 40 |
case "env": |
| 41 |
return `${msg(loc.parent)}.env["${loc.name}"]`; |
| 42 |
case "overrides": |
| 43 |
return `${msg(loc.parent)}.overrides[${loc.index}]`; |
| 44 |
case "option": |
| 45 |
return `${msg(loc.parent)}.${loc.name}`; |
| 46 |
case "access": |
| 47 |
return `${msg(loc.parent)}[${JSON.stringify(loc.name)}]`; |
| 48 |
default: |
| 49 |
// @ts-expect-error should not happen when code is type checked |
| 50 |
throw new Error(`Assertion failure: Unknown type ${loc.type}`); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
export function access(loc: GeneralPath, name: string | number): AccessPath { |
| 55 |
return { |
| 56 |
type: "access", |
| 57 |
name, |
| 58 |
parent: loc, |
| 59 |
}; |
| 60 |
} |
| 61 |
|
| 62 |
export type OptionPath = Readonly<{ |
| 63 |
type: "option"; |
| 64 |
name: string; |
| 65 |
parent: NestingPath; |
| 66 |
}>; |
| 67 |
type AccessPath = Readonly<{ |
| 68 |
type: "access"; |
| 69 |
name: string | number; |
| 70 |
parent: GeneralPath; |
| 71 |
}>; |
| 72 |
type GeneralPath = OptionPath | AccessPath; |
| 73 |
|
| 74 |
export function assertRootMode( |
| 75 |
loc: OptionPath, |
| 76 |
value: unknown, |
| 77 |
): RootMode | void { |
| 78 |
if ( |
| 79 |
value !== undefined && |
| 80 |
value !== "root" && |
| 81 |
value !== "upward" && |
| 82 |
value !== "upward-optional" |
| 83 |
) { |
| 84 |
throw new Error( |
| 85 |
`${msg(loc)} must be a "root", "upward", "upward-optional" or undefined`, |
| 86 |
); |
| 87 |
} |
| 88 |
// @ts-expect-error: TS can only narrow down the type when "strictNullCheck" is true |
| 89 |
return value; |
| 90 |
} |
| 91 |
|
| 92 |
export function assertSourceMaps( |
| 93 |
loc: OptionPath, |
| 94 |
value: unknown, |
| 95 |
): SourceMapsOption | void { |
| 96 |
if ( |
| 97 |
value !== undefined && |
| 98 |
typeof value !== "boolean" && |
| 99 |
value !== "inline" && |
| 100 |
value !== "both" |
| 101 |
) { |
| 102 |
throw new Error( |
| 103 |
`${msg(loc)} must be a boolean, "inline", "both", or undefined`, |
| 104 |
); |
| 105 |
} |
| 106 |
// @ts-expect-error: TS can only narrow down the type when "strictNullCheck" is true |
| 107 |
return value; |
| 108 |
} |
| 109 |
|
| 110 |
export function assertCompact( |
| 111 |
loc: OptionPath, |
| 112 |
value: unknown, |
| 113 |
): CompactOption | void { |
| 114 |
if (value !== undefined && typeof value !== "boolean" && value !== "auto") { |
| 115 |
throw new Error(`${msg(loc)} must be a boolean, "auto", or undefined`); |
| 116 |
} |
| 117 |
// @ts-expect-error: TS can only narrow down the type when "strictNullCheck" is true |
| 118 |
return value; |
| 119 |
} |
| 120 |
|
| 121 |
export function assertSourceType( |
| 122 |
loc: OptionPath, |
| 123 |
value: unknown, |
| 124 |
): SourceTypeOption | void { |
| 125 |
if ( |
| 126 |
value !== undefined && |
| 127 |
value !== "module" && |
| 128 |
value !== "script" && |
| 129 |
value !== "unambiguous" |
| 130 |
) { |
| 131 |
throw new Error( |
| 132 |
`${msg(loc)} must be "module", "script", "unambiguous", or undefined`, |
| 133 |
); |
| 134 |
} |
| 135 |
// @ts-expect-error: TS can only narrow down the type when "strictNullCheck" is true |
| 136 |
return value; |
| 137 |
} |
| 138 |
|
| 139 |
export function assertCallerMetadata( |
| 140 |
loc: OptionPath, |
| 141 |
value: unknown, |
| 142 |
): CallerMetadata | undefined { |
| 143 |
const obj = assertObject(loc, value); |
| 144 |
if (obj) { |
| 145 |
if (typeof obj.name !== "string") { |
| 146 |
throw new Error( |
| 147 |
`${msg(loc)} set but does not contain "name" property string`, |
| 148 |
); |
| 149 |
} |
| 150 |
|
| 151 |
for (const prop of Object.keys(obj)) { |
| 152 |
const propLoc = access(loc, prop); |
| 153 |
const value = obj[prop]; |
| 154 |
if ( |
| 155 |
value != null && |
| 156 |
typeof value !== "boolean" && |
| 157 |
typeof value !== "string" && |
| 158 |
typeof value !== "number" |
| 159 |
) { |
| 160 |
// NOTE(logan): I'm limiting the type here so that we can guarantee that |
| 161 |
// the "caller" value will serialize to JSON nicely. We can always |
| 162 |
// allow more complex structures later though. |
| 163 |
throw new Error( |
| 164 |
`${msg( |
| 165 |
propLoc, |
| 166 |
)} must be null, undefined, a boolean, a string, or a number.`, |
| 167 |
); |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
// @ts-expect-error todo(flow->ts) |
| 172 |
return value; |
| 173 |
} |
| 174 |
|
| 175 |
export function assertInputSourceMap( |
| 176 |
loc: OptionPath, |
| 177 |
value: unknown, |
| 178 |
): RootInputSourceMapOption | void { |
| 179 |
if ( |
| 180 |
value !== undefined && |
| 181 |
typeof value !== "boolean" && |
| 182 |
(typeof value !== "object" || !value) |
| 183 |
) { |
| 184 |
throw new Error(`${msg(loc)} must be a boolean, object, or undefined`); |
| 185 |
} |
| 186 |
return value; |
| 187 |
} |
| 188 |
|
| 189 |
export function assertString(loc: GeneralPath, value: unknown): string | void { |
| 190 |
if (value !== undefined && typeof value !== "string") { |
| 191 |
throw new Error(`${msg(loc)} must be a string, or undefined`); |
| 192 |
} |
| 193 |
// @ts-expect-error: TS can only narrow down the type when "strictNullCheck" is true |
| 194 |
return value; |
| 195 |
} |
| 196 |
|
| 197 |
export function assertFunction( |
| 198 |
loc: GeneralPath, |
| 199 |
value: unknown, |
| 200 |
): Function | void { |
| 201 |
if (value !== undefined && typeof value !== "function") { |
| 202 |
throw new Error(`${msg(loc)} must be a function, or undefined`); |
| 203 |
} |
| 204 |
// @ts-expect-error: TS can only narrow down the type when "strictNullCheck" is true |
| 205 |
return value; |
| 206 |
} |
| 207 |
|
| 208 |
export function assertBoolean( |
| 209 |
loc: GeneralPath, |
| 210 |
value: unknown, |
| 211 |
): boolean | void { |
| 212 |
if (value !== undefined && typeof value !== "boolean") { |
| 213 |
throw new Error(`${msg(loc)} must be a boolean, or undefined`); |
| 214 |
} |
| 215 |
// @ts-expect-error: TS can only narrow down the type when "strictNullCheck" is true |
| 216 |
return value; |
| 217 |
} |
| 218 |
|
| 219 |
export function assertObject( |
| 220 |
loc: GeneralPath, |
| 221 |
value: unknown, |
| 222 |
): { readonly [key: string]: unknown } | void { |
| 223 |
if ( |
| 224 |
value !== undefined && |
| 225 |
(typeof value !== "object" || Array.isArray(value) || !value) |
| 226 |
) { |
| 227 |
throw new Error(`${msg(loc)} must be an object, or undefined`); |
| 228 |
} |
| 229 |
// @ts-expect-error todo(flow->ts) value is still typed as unknown, also assert function typically should not return a value |
| 230 |
return value; |
| 231 |
} |
| 232 |
|
| 233 |
export function assertArray<T>( |
| 234 |
loc: GeneralPath, |
| 235 |
value: Array<T> | undefined | null, |
| 236 |
): ReadonlyArray<T> | undefined | null { |
| 237 |
if (value != null && !Array.isArray(value)) { |
| 238 |
throw new Error(`${msg(loc)} must be an array, or undefined`); |
| 239 |
} |
| 240 |
return value; |
| 241 |
} |
| 242 |
|
| 243 |
export function assertIgnoreList( |
| 244 |
loc: OptionPath, |
| 245 |
value: unknown[] | undefined, |
| 246 |
): IgnoreList | void { |
| 247 |
const arr = assertArray(loc, value); |
| 248 |
if (arr) { |
| 249 |
arr.forEach((item, i) => assertIgnoreItem(access(loc, i), item)); |
| 250 |
} |
| 251 |
// @ts-expect-error todo(flow->ts) |
| 252 |
return arr; |
| 253 |
} |
| 254 |
function assertIgnoreItem(loc: GeneralPath, value: unknown): IgnoreItem { |
| 255 |
if ( |
| 256 |
typeof value !== "string" && |
| 257 |
typeof value !== "function" && |
| 258 |
!(value instanceof RegExp) |
| 259 |
) { |
| 260 |
throw new Error( |
| 261 |
`${msg( |
| 262 |
loc, |
| 263 |
)} must be an array of string/Function/RegExp values, or undefined`, |
| 264 |
); |
| 265 |
} |
| 266 |
return value as IgnoreItem; |
| 267 |
} |
| 268 |
|
| 269 |
export function assertConfigApplicableTest( |
| 270 |
loc: OptionPath, |
| 271 |
value: unknown, |
| 272 |
): ConfigApplicableTest | void { |
| 273 |
if (value === undefined) { |
| 274 |
// @ts-expect-error: TS can only narrow down the type when "strictNullCheck" is true |
| 275 |
return value; |
| 276 |
} |
| 277 |
|
| 278 |
if (Array.isArray(value)) { |
| 279 |
value.forEach((item, i) => { |
| 280 |
if (!checkValidTest(item)) { |
| 281 |
throw new Error( |
| 282 |
`${msg(access(loc, i))} must be a string/Function/RegExp.`, |
| 283 |
); |
| 284 |
} |
| 285 |
}); |
| 286 |
} else if (!checkValidTest(value)) { |
| 287 |
throw new Error( |
| 288 |
`${msg(loc)} must be a string/Function/RegExp, or an array of those`, |
| 289 |
); |
| 290 |
} |
| 291 |
return value as ConfigApplicableTest; |
| 292 |
} |
| 293 |
|
| 294 |
function checkValidTest(value: unknown): value is string | Function | RegExp { |
| 295 |
return ( |
| 296 |
typeof value === "string" || |
| 297 |
typeof value === "function" || |
| 298 |
value instanceof RegExp |
| 299 |
); |
| 300 |
} |
| 301 |
|
| 302 |
export function assertConfigFileSearch( |
| 303 |
loc: OptionPath, |
| 304 |
value: unknown, |
| 305 |
): ConfigFileSearch | void { |
| 306 |
if ( |
| 307 |
value !== undefined && |
| 308 |
typeof value !== "boolean" && |
| 309 |
typeof value !== "string" |
| 310 |
) { |
| 311 |
throw new Error( |
| 312 |
`${msg(loc)} must be a undefined, a boolean, a string, ` + |
| 313 |
`got ${JSON.stringify(value)}`, |
| 314 |
); |
| 315 |
} |
| 316 |
// @ts-expect-error: TS can only narrow down the type when "strictNullCheck" is true |
| 317 |
return value; |
| 318 |
} |
| 319 |
|
| 320 |
export function assertBabelrcSearch( |
| 321 |
loc: OptionPath, |
| 322 |
value: unknown, |
| 323 |
): BabelrcSearch | void { |
| 324 |
if (value === undefined || typeof value === "boolean") { |
| 325 |
// @ts-expect-error: TS can only narrow down the type when "strictNullCheck" is true |
| 326 |
return value; |
| 327 |
} |
| 328 |
|
| 329 |
if (Array.isArray(value)) { |
| 330 |
value.forEach((item, i) => { |
| 331 |
if (!checkValidTest(item)) { |
| 332 |
throw new Error( |
| 333 |
`${msg(access(loc, i))} must be a string/Function/RegExp.`, |
| 334 |
); |
| 335 |
} |
| 336 |
}); |
| 337 |
} else if (!checkValidTest(value)) { |
| 338 |
throw new Error( |
| 339 |
`${msg(loc)} must be a undefined, a boolean, a string/Function/RegExp ` + |
| 340 |
`or an array of those, got ${JSON.stringify(value as any)}`, |
| 341 |
); |
| 342 |
} |
| 343 |
return value as BabelrcSearch; |
| 344 |
} |
| 345 |
|
| 346 |
export function assertPluginList( |
| 347 |
loc: OptionPath, |
| 348 |
value: unknown[] | null | undefined, |
| 349 |
): PluginList | void { |
| 350 |
const arr = assertArray(loc, value); |
| 351 |
if (arr) { |
| 352 |
// Loop instead of using `.map` in order to preserve object identity |
| 353 |
// for plugin array for use during config chain processing. |
| 354 |
arr.forEach((item, i) => assertPluginItem(access(loc, i), item)); |
| 355 |
} |
| 356 |
return arr as any; |
| 357 |
} |
| 358 |
function assertPluginItem(loc: GeneralPath, value: unknown): PluginItem { |
| 359 |
if (Array.isArray(value)) { |
| 360 |
if (value.length === 0) { |
| 361 |
throw new Error(`${msg(loc)} must include an object`); |
| 362 |
} |
| 363 |
|
| 364 |
if (value.length > 3) { |
| 365 |
throw new Error(`${msg(loc)} may only be a two-tuple or three-tuple`); |
| 366 |
} |
| 367 |
|
| 368 |
assertPluginTarget(access(loc, 0), value[0]); |
| 369 |
|
| 370 |
if (value.length > 1) { |
| 371 |
const opts = value[1]; |
| 372 |
if ( |
| 373 |
opts !== undefined && |
| 374 |
opts !== false && |
| 375 |
(typeof opts !== "object" || Array.isArray(opts) || opts === null) |
| 376 |
) { |
| 377 |
throw new Error( |
| 378 |
`${msg(access(loc, 1))} must be an object, false, or undefined`, |
| 379 |
); |
| 380 |
} |
| 381 |
} |
| 382 |
if (value.length === 3) { |
| 383 |
const name = value[2]; |
| 384 |
if (name !== undefined && typeof name !== "string") { |
| 385 |
throw new Error( |
| 386 |
`${msg(access(loc, 2))} must be a string, or undefined`, |
| 387 |
); |
| 388 |
} |
| 389 |
} |
| 390 |
} else { |
| 391 |
assertPluginTarget(loc, value); |
| 392 |
} |
| 393 |
|
| 394 |
// @ts-expect-error todo(flow->ts) |
| 395 |
return value; |
| 396 |
} |
| 397 |
function assertPluginTarget(loc: GeneralPath, value: unknown): PluginTarget { |
| 398 |
if ( |
| 399 |
(typeof value !== "object" || !value) && |
| 400 |
typeof value !== "string" && |
| 401 |
typeof value !== "function" |
| 402 |
) { |
| 403 |
throw new Error(`${msg(loc)} must be a string, object, function`); |
| 404 |
} |
| 405 |
return value; |
| 406 |
} |
| 407 |
|
| 408 |
export function assertTargets( |
| 409 |
loc: GeneralPath, |
| 410 |
value: any, |
| 411 |
): TargetsListOrObject { |
| 412 |
if (isBrowsersQueryValid(value)) return value; |
| 413 |
|
| 414 |
if (typeof value !== "object" || !value || Array.isArray(value)) { |
| 415 |
throw new Error( |
| 416 |
`${msg(loc)} must be a string, an array of strings or an object`, |
| 417 |
); |
| 418 |
} |
| 419 |
|
| 420 |
const browsersLoc = access(loc, "browsers"); |
| 421 |
const esmodulesLoc = access(loc, "esmodules"); |
| 422 |
|
| 423 |
assertBrowsersList(browsersLoc, value.browsers); |
| 424 |
assertBoolean(esmodulesLoc, value.esmodules); |
| 425 |
|
| 426 |
for (const key of Object.keys(value)) { |
| 427 |
const val = value[key]; |
| 428 |
const subLoc = access(loc, key); |
| 429 |
|
| 430 |
if (key === "esmodules") assertBoolean(subLoc, val); |
| 431 |
else if (key === "browsers") assertBrowsersList(subLoc, val); |
| 432 |
else if (!Object.hasOwnProperty.call(TargetNames, key)) { |
| 433 |
const validTargets = Object.keys(TargetNames).join(", "); |
| 434 |
throw new Error( |
| 435 |
`${msg( |
| 436 |
subLoc, |
| 437 |
)} is not a valid target. Supported targets are ${validTargets}`, |
| 438 |
); |
| 439 |
} else assertBrowserVersion(subLoc, val); |
| 440 |
} |
| 441 |
|
| 442 |
return value; |
| 443 |
} |
| 444 |
|
| 445 |
function assertBrowsersList(loc: GeneralPath, value: unknown) { |
| 446 |
if (value !== undefined && !isBrowsersQueryValid(value)) { |
| 447 |
throw new Error( |
| 448 |
`${msg(loc)} must be undefined, a string or an array of strings`, |
| 449 |
); |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
function assertBrowserVersion(loc: GeneralPath, value: unknown) { |
| 454 |
if (typeof value === "number" && Math.round(value) === value) return; |
| 455 |
if (typeof value === "string") return; |
| 456 |
|
| 457 |
throw new Error(`${msg(loc)} must be a string or an integer number`); |
| 458 |
} |
| 459 |
|
| 460 |
export function assertAssumptions( |
| 461 |
loc: GeneralPath, |
| 462 |
value: { [key: string]: unknown }, |
| 463 |
): { [name: string]: boolean } | void { |
| 464 |
if (value === undefined) return; |
| 465 |
|
| 466 |
if (typeof value !== "object" || value === null) { |
| 467 |
throw new Error(`${msg(loc)} must be an object or undefined.`); |
| 468 |
} |
| 469 |
|
| 470 |
// todo(flow->ts): remove any |
| 471 |
let root: any = loc; |
| 472 |
do { |
| 473 |
root = root.parent; |
| 474 |
} while (root.type !== "root"); |
| 475 |
const inPreset = root.source === "preset"; |
| 476 |
|
| 477 |
for (const name of Object.keys(value)) { |
| 478 |
const subLoc = access(loc, name); |
| 479 |
if (!assumptionsNames.has(name as AssumptionName)) { |
| 480 |
throw new Error(`${msg(subLoc)} is not a supported assumption.`); |
| 481 |
} |
| 482 |
if (typeof value[name] !== "boolean") { |
| 483 |
throw new Error(`${msg(subLoc)} must be a boolean.`); |
| 484 |
} |
| 485 |
if (inPreset && value[name] === false) { |
| 486 |
throw new Error( |
| 487 |
`${msg(subLoc)} cannot be set to 'false' inside presets.`, |
| 488 |
); |
| 489 |
} |
| 490 |
} |
| 491 |
|
| 492 |
// @ts-expect-error todo(flow->ts) |
| 493 |
return value; |
| 494 |
} |