| 1 |
import type { InputTargets, Targets } from "@babel/helper-compilation-targets"; |
| 2 |
|
| 3 |
import type { ConfigItem } from "../item"; |
| 4 |
import type Plugin from "../plugin"; |
| 5 |
|
| 6 |
import removed from "./removed"; |
| 7 |
import { |
| 8 |
msg, |
| 9 |
access, |
| 10 |
assertString, |
| 11 |
assertBoolean, |
| 12 |
assertObject, |
| 13 |
assertArray, |
| 14 |
assertCallerMetadata, |
| 15 |
assertInputSourceMap, |
| 16 |
assertIgnoreList, |
| 17 |
assertPluginList, |
| 18 |
assertConfigApplicableTest, |
| 19 |
assertConfigFileSearch, |
| 20 |
assertBabelrcSearch, |
| 21 |
assertFunction, |
| 22 |
assertRootMode, |
| 23 |
assertSourceMaps, |
| 24 |
assertCompact, |
| 25 |
assertSourceType, |
| 26 |
assertTargets, |
| 27 |
assertAssumptions, |
| 28 |
} from "./option-assertions"; |
| 29 |
import type { ValidatorSet, Validator, OptionPath } from "./option-assertions"; |
| 30 |
import type { UnloadedDescriptor } from "../config-descriptors"; |
| 31 |
import type { ParserOptions } from "@babel/parser"; |
| 32 |
import type { GeneratorOptions } from "@babel/generator"; |
| 33 |
import ConfigError from "../../errors/config-error"; |
| 34 |
|
| 35 |
const ROOT_VALIDATORS: ValidatorSet = { |
| 36 |
cwd: assertString as Validator<ValidatedOptions["cwd"]>, |
| 37 |
root: assertString as Validator<ValidatedOptions["root"]>, |
| 38 |
rootMode: assertRootMode as Validator<ValidatedOptions["rootMode"]>, |
| 39 |
configFile: assertConfigFileSearch as Validator< |
| 40 |
ValidatedOptions["configFile"] |
| 41 |
>, |
| 42 |
|
| 43 |
caller: assertCallerMetadata as Validator<ValidatedOptions["caller"]>, |
| 44 |
filename: assertString as Validator<ValidatedOptions["filename"]>, |
| 45 |
filenameRelative: assertString as Validator< |
| 46 |
ValidatedOptions["filenameRelative"] |
| 47 |
>, |
| 48 |
code: assertBoolean as Validator<ValidatedOptions["code"]>, |
| 49 |
ast: assertBoolean as Validator<ValidatedOptions["ast"]>, |
| 50 |
|
| 51 |
cloneInputAst: assertBoolean as Validator<ValidatedOptions["cloneInputAst"]>, |
| 52 |
|
| 53 |
envName: assertString as Validator<ValidatedOptions["envName"]>, |
| 54 |
}; |
| 55 |
|
| 56 |
const BABELRC_VALIDATORS: ValidatorSet = { |
| 57 |
babelrc: assertBoolean as Validator<ValidatedOptions["babelrc"]>, |
| 58 |
babelrcRoots: assertBabelrcSearch as Validator< |
| 59 |
ValidatedOptions["babelrcRoots"] |
| 60 |
>, |
| 61 |
}; |
| 62 |
|
| 63 |
const NONPRESET_VALIDATORS: ValidatorSet = { |
| 64 |
extends: assertString as Validator<ValidatedOptions["extends"]>, |
| 65 |
ignore: assertIgnoreList as Validator<ValidatedOptions["ignore"]>, |
| 66 |
only: assertIgnoreList as Validator<ValidatedOptions["only"]>, |
| 67 |
|
| 68 |
targets: assertTargets as Validator<ValidatedOptions["targets"]>, |
| 69 |
browserslistConfigFile: assertConfigFileSearch as Validator< |
| 70 |
ValidatedOptions["browserslistConfigFile"] |
| 71 |
>, |
| 72 |
browserslistEnv: assertString as Validator< |
| 73 |
ValidatedOptions["browserslistEnv"] |
| 74 |
>, |
| 75 |
}; |
| 76 |
|
| 77 |
const COMMON_VALIDATORS: ValidatorSet = { |
| 78 |
// TODO: Should 'inputSourceMap' be moved to be a root-only option? |
| 79 |
// We may want a boolean-only version to be a common option, with the |
| 80 |
// object only allowed as a root config argument. |
| 81 |
inputSourceMap: assertInputSourceMap as Validator< |
| 82 |
ValidatedOptions["inputSourceMap"] |
| 83 |
>, |
| 84 |
presets: assertPluginList as Validator<ValidatedOptions["presets"]>, |
| 85 |
plugins: assertPluginList as Validator<ValidatedOptions["plugins"]>, |
| 86 |
passPerPreset: assertBoolean as Validator<ValidatedOptions["passPerPreset"]>, |
| 87 |
assumptions: assertAssumptions as Validator<ValidatedOptions["assumptions"]>, |
| 88 |
|
| 89 |
env: assertEnvSet as Validator<ValidatedOptions["env"]>, |
| 90 |
overrides: assertOverridesList as Validator<ValidatedOptions["overrides"]>, |
| 91 |
|
| 92 |
// We could limit these to 'overrides' blocks, but it's not clear why we'd |
| 93 |
// bother, when the ability to limit a config to a specific set of files |
| 94 |
// is a fairly general useful feature. |
| 95 |
test: assertConfigApplicableTest as Validator<ValidatedOptions["test"]>, |
| 96 |
include: assertConfigApplicableTest as Validator<ValidatedOptions["include"]>, |
| 97 |
exclude: assertConfigApplicableTest as Validator<ValidatedOptions["exclude"]>, |
| 98 |
|
| 99 |
retainLines: assertBoolean as Validator<ValidatedOptions["retainLines"]>, |
| 100 |
comments: assertBoolean as Validator<ValidatedOptions["comments"]>, |
| 101 |
shouldPrintComment: assertFunction as Validator< |
| 102 |
ValidatedOptions["shouldPrintComment"] |
| 103 |
>, |
| 104 |
compact: assertCompact as Validator<ValidatedOptions["compact"]>, |
| 105 |
minified: assertBoolean as Validator<ValidatedOptions["minified"]>, |
| 106 |
auxiliaryCommentBefore: assertString as Validator< |
| 107 |
ValidatedOptions["auxiliaryCommentBefore"] |
| 108 |
>, |
| 109 |
auxiliaryCommentAfter: assertString as Validator< |
| 110 |
ValidatedOptions["auxiliaryCommentAfter"] |
| 111 |
>, |
| 112 |
sourceType: assertSourceType as Validator<ValidatedOptions["sourceType"]>, |
| 113 |
wrapPluginVisitorMethod: assertFunction as Validator< |
| 114 |
ValidatedOptions["wrapPluginVisitorMethod"] |
| 115 |
>, |
| 116 |
highlightCode: assertBoolean as Validator<ValidatedOptions["highlightCode"]>, |
| 117 |
sourceMaps: assertSourceMaps as Validator<ValidatedOptions["sourceMaps"]>, |
| 118 |
sourceMap: assertSourceMaps as Validator<ValidatedOptions["sourceMap"]>, |
| 119 |
sourceFileName: assertString as Validator<ValidatedOptions["sourceFileName"]>, |
| 120 |
sourceRoot: assertString as Validator<ValidatedOptions["sourceRoot"]>, |
| 121 |
parserOpts: assertObject as Validator<ValidatedOptions["parserOpts"]>, |
| 122 |
generatorOpts: assertObject as Validator<ValidatedOptions["generatorOpts"]>, |
| 123 |
}; |
| 124 |
if (!process.env.BABEL_8_BREAKING) { |
| 125 |
Object.assign(COMMON_VALIDATORS, { |
| 126 |
getModuleId: assertFunction, |
| 127 |
moduleRoot: assertString, |
| 128 |
moduleIds: assertBoolean, |
| 129 |
moduleId: assertString, |
| 130 |
}); |
| 131 |
} |
| 132 |
|
| 133 |
export type InputOptions = ValidatedOptions; |
| 134 |
|
| 135 |
export type ValidatedOptions = { |
| 136 |
cwd?: string; |
| 137 |
filename?: string; |
| 138 |
filenameRelative?: string; |
| 139 |
babelrc?: boolean; |
| 140 |
babelrcRoots?: BabelrcSearch; |
| 141 |
configFile?: ConfigFileSearch; |
| 142 |
root?: string; |
| 143 |
rootMode?: RootMode; |
| 144 |
code?: boolean; |
| 145 |
ast?: boolean; |
| 146 |
cloneInputAst?: boolean; |
| 147 |
inputSourceMap?: RootInputSourceMapOption; |
| 148 |
envName?: string; |
| 149 |
caller?: CallerMetadata; |
| 150 |
extends?: string; |
| 151 |
env?: EnvSet<ValidatedOptions>; |
| 152 |
ignore?: IgnoreList; |
| 153 |
only?: IgnoreList; |
| 154 |
overrides?: OverridesList; |
| 155 |
// Generally verify if a given config object should be applied to the given file. |
| 156 |
test?: ConfigApplicableTest; |
| 157 |
include?: ConfigApplicableTest; |
| 158 |
exclude?: ConfigApplicableTest; |
| 159 |
presets?: PluginList; |
| 160 |
plugins?: PluginList; |
| 161 |
passPerPreset?: boolean; |
| 162 |
assumptions?: { |
| 163 |
[name: string]: boolean; |
| 164 |
}; |
| 165 |
// browserslists-related options |
| 166 |
targets?: TargetsListOrObject; |
| 167 |
browserslistConfigFile?: ConfigFileSearch; |
| 168 |
browserslistEnv?: string; |
| 169 |
// Options for @babel/generator |
| 170 |
retainLines?: boolean; |
| 171 |
comments?: boolean; |
| 172 |
shouldPrintComment?: Function; |
| 173 |
compact?: CompactOption; |
| 174 |
minified?: boolean; |
| 175 |
auxiliaryCommentBefore?: string; |
| 176 |
auxiliaryCommentAfter?: string; |
| 177 |
// Parser |
| 178 |
sourceType?: SourceTypeOption; |
| 179 |
wrapPluginVisitorMethod?: Function; |
| 180 |
highlightCode?: boolean; |
| 181 |
// Sourcemap generation options. |
| 182 |
sourceMaps?: SourceMapsOption; |
| 183 |
sourceMap?: SourceMapsOption; |
| 184 |
sourceFileName?: string; |
| 185 |
sourceRoot?: string; |
| 186 |
// Deprecate top level parserOpts |
| 187 |
parserOpts?: ParserOptions; |
| 188 |
// Deprecate top level generatorOpts |
| 189 |
generatorOpts?: GeneratorOptions; |
| 190 |
}; |
| 191 |
|
| 192 |
export type NormalizedOptions = { |
| 193 |
readonly targets: Targets; |
| 194 |
} & Omit<ValidatedOptions, "targets">; |
| 195 |
|
| 196 |
export type CallerMetadata = { |
| 197 |
// If 'caller' is specified, require that the name is given for debugging |
| 198 |
// messages. |
| 199 |
name: string; |
| 200 |
}; |
| 201 |
export type EnvSet<T> = { |
| 202 |
[x: string]: T; |
| 203 |
}; |
| 204 |
export type IgnoreItem = |
| 205 |
| string |
| 206 |
| RegExp |
| 207 |
| (( |
| 208 |
path: string | undefined, |
| 209 |
context: { dirname: string; caller: CallerMetadata; envName: string }, |
| 210 |
) => unknown); |
| 211 |
export type IgnoreList = ReadonlyArray<IgnoreItem>; |
| 212 |
|
| 213 |
export type PluginOptions = object | void | false; |
| 214 |
export type PluginTarget = string | object | Function; |
| 215 |
export type PluginItem = |
| 216 |
| ConfigItem |
| 217 |
| Plugin |
| 218 |
| PluginTarget |
| 219 |
| [PluginTarget, PluginOptions] |
| 220 |
| [PluginTarget, PluginOptions, string | void]; |
| 221 |
export type PluginList = ReadonlyArray<PluginItem>; |
| 222 |
|
| 223 |
export type OverridesList = Array<ValidatedOptions>; |
| 224 |
export type ConfigApplicableTest = IgnoreItem | Array<IgnoreItem>; |
| 225 |
|
| 226 |
export type ConfigFileSearch = string | boolean; |
| 227 |
export type BabelrcSearch = boolean | IgnoreItem | IgnoreList; |
| 228 |
export type SourceMapsOption = boolean | "inline" | "both"; |
| 229 |
export type SourceTypeOption = "module" | "script" | "unambiguous"; |
| 230 |
export type CompactOption = boolean | "auto"; |
| 231 |
export type RootInputSourceMapOption = {} | boolean; |
| 232 |
export type RootMode = "root" | "upward" | "upward-optional"; |
| 233 |
|
| 234 |
export type TargetsListOrObject = |
| 235 |
| Targets |
| 236 |
| InputTargets |
| 237 |
| InputTargets["browsers"]; |
| 238 |
|
| 239 |
export type OptionsSource = |
| 240 |
| "arguments" |
| 241 |
| "configfile" |
| 242 |
| "babelrcfile" |
| 243 |
| "extendsfile" |
| 244 |
| "preset" |
| 245 |
| "plugin"; |
| 246 |
|
| 247 |
export type RootPath = Readonly<{ |
| 248 |
type: "root"; |
| 249 |
source: OptionsSource; |
| 250 |
}>; |
| 251 |
|
| 252 |
type OverridesPath = Readonly<{ |
| 253 |
type: "overrides"; |
| 254 |
index: number; |
| 255 |
parent: RootPath; |
| 256 |
}>; |
| 257 |
|
| 258 |
type EnvPath = Readonly<{ |
| 259 |
type: "env"; |
| 260 |
name: string; |
| 261 |
parent: RootPath | OverridesPath; |
| 262 |
}>; |
| 263 |
|
| 264 |
export type NestingPath = RootPath | OverridesPath | EnvPath; |
| 265 |
|
| 266 |
const knownAssumptions = [ |
| 267 |
"arrayLikeIsIterable", |
| 268 |
"constantReexports", |
| 269 |
"constantSuper", |
| 270 |
"enumerableModuleMeta", |
| 271 |
"ignoreFunctionLength", |
| 272 |
"ignoreToPrimitiveHint", |
| 273 |
"iterableIsArray", |
| 274 |
"mutableTemplateObject", |
| 275 |
"noClassCalls", |
| 276 |
"noDocumentAll", |
| 277 |
"noIncompleteNsImportDetection", |
| 278 |
"noNewArrows", |
| 279 |
"objectRestNoSymbols", |
| 280 |
"privateFieldsAsProperties", |
| 281 |
"pureGetters", |
| 282 |
"setClassMethods", |
| 283 |
"setComputedProperties", |
| 284 |
"setPublicClassFields", |
| 285 |
"setSpreadProperties", |
| 286 |
"skipForOfIteratorClosing", |
| 287 |
"superIsCallableConstructor", |
| 288 |
] as const; |
| •289 |
export type AssumptionName = (typeof knownAssumptions)[number]; |
| 290 |
export const assumptionsNames = new Set(knownAssumptions); |
| 291 |
|
| 292 |
function getSource(loc: NestingPath): OptionsSource { |
| 293 |
return loc.type === "root" ? loc.source : getSource(loc.parent); |
| 294 |
} |
| 295 |
|
| 296 |
export function validate( |
| 297 |
type: OptionsSource, |
| 298 |
opts: {}, |
| 299 |
filename?: string, |
| 300 |
): ValidatedOptions { |
| 301 |
try { |
| 302 |
return validateNested( |
| 303 |
{ |
| 304 |
type: "root", |
| 305 |
source: type, |
| 306 |
}, |
| 307 |
opts, |
| 308 |
); |
| 309 |
} catch (error) { |
| 310 |
const configError = new ConfigError(error.message, filename); |
| 311 |
// @ts-expect-error TODO: .code is not defined on ConfigError or Error |
| 312 |
if (error.code) configError.code = error.code; |
| 313 |
throw configError; |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
function validateNested(loc: NestingPath, opts: { [key: string]: unknown }) { |
| 318 |
const type = getSource(loc); |
| 319 |
|
| 320 |
assertNoDuplicateSourcemap(opts); |
| 321 |
|
| 322 |
Object.keys(opts).forEach((key: string) => { |
| 323 |
const optLoc = { |
| 324 |
type: "option", |
| 325 |
name: key, |
| 326 |
parent: loc, |
| 327 |
} as const; |
| 328 |
|
| 329 |
if (type === "preset" && NONPRESET_VALIDATORS[key]) { |
| 330 |
throw new Error(`${msg(optLoc)} is not allowed in preset options`); |
| 331 |
} |
| 332 |
if (type !== "arguments" && ROOT_VALIDATORS[key]) { |
| 333 |
throw new Error( |
| 334 |
`${msg(optLoc)} is only allowed in root programmatic options`, |
| 335 |
); |
| 336 |
} |
| 337 |
if ( |
| 338 |
type !== "arguments" && |
| 339 |
type !== "configfile" && |
| 340 |
BABELRC_VALIDATORS[key] |
| 341 |
) { |
| 342 |
if (type === "babelrcfile" || type === "extendsfile") { |
| 343 |
throw new Error( |
| 344 |
`${msg( |
| 345 |
optLoc, |
| 346 |
)} is not allowed in .babelrc or "extends"ed files, only in root programmatic options, ` + |
| 347 |
`or babel.config.js/config file options`, |
| 348 |
); |
| 349 |
} |
| 350 |
|
| 351 |
throw new Error( |
| 352 |
`${msg( |
| 353 |
optLoc, |
| 354 |
)} is only allowed in root programmatic options, or babel.config.js/config file options`, |
| 355 |
); |
| 356 |
} |
| 357 |
|
| 358 |
const validator = |
| 359 |
COMMON_VALIDATORS[key] || |
| 360 |
NONPRESET_VALIDATORS[key] || |
| 361 |
BABELRC_VALIDATORS[key] || |
| 362 |
ROOT_VALIDATORS[key] || |
| 363 |
(throwUnknownError as Validator<void>); |
| 364 |
|
| 365 |
validator(optLoc, opts[key]); |
| 366 |
}); |
| 367 |
|
| 368 |
return opts; |
| 369 |
} |
| 370 |
|
| 371 |
function throwUnknownError(loc: OptionPath) { |
| 372 |
const key = loc.name; |
| 373 |
|
| 374 |
if (removed[key]) { |
| 375 |
const { message, version = 5 } = removed[key]; |
| 376 |
|
| 377 |
throw new Error( |
| 378 |
`Using removed Babel ${version} option: ${msg(loc)} - ${message}`, |
| 379 |
); |
| 380 |
} else { |
| 381 |
// eslint-disable-next-line max-len |
| 382 |
const unknownOptErr = new Error( |
| 383 |
`Unknown option: ${msg( |
| 384 |
loc, |
| 385 |
)}. Check out https://babeljs.io/docs/en/babel-core/#options for more information about options.`, |
| 386 |
); |
| 387 |
// @ts-expect-error todo(flow->ts): consider creating something like BabelConfigError with code field in it |
| 388 |
unknownOptErr.code = "BABEL_UNKNOWN_OPTION"; |
| 389 |
|
| 390 |
throw unknownOptErr; |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
function has(obj: {}, key: string) { |
| 395 |
return Object.prototype.hasOwnProperty.call(obj, key); |
| 396 |
} |
| 397 |
|
| 398 |
function assertNoDuplicateSourcemap(opts: {}): void { |
| 399 |
if (has(opts, "sourceMap") && has(opts, "sourceMaps")) { |
| 400 |
throw new Error(".sourceMap is an alias for .sourceMaps, cannot use both"); |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
function assertEnvSet( |
| 405 |
loc: OptionPath, |
| 406 |
value: unknown, |
| 407 |
): void | EnvSet<ValidatedOptions> { |
| 408 |
if (loc.parent.type === "env") { |
| 409 |
throw new Error(`${msg(loc)} is not allowed inside of another .env block`); |
| 410 |
} |
| 411 |
const parent: RootPath | OverridesPath = loc.parent; |
| 412 |
|
| 413 |
const obj = assertObject(loc, value); |
| 414 |
if (obj) { |
| 415 |
// Validate but don't copy the .env object in order to preserve |
| 416 |
// object identity for use during config chain processing. |
| 417 |
for (const envName of Object.keys(obj)) { |
| 418 |
const env = assertObject(access(loc, envName), obj[envName]); |
| 419 |
if (!env) continue; |
| 420 |
|
| 421 |
const envLoc = { |
| 422 |
type: "env", |
| 423 |
name: envName, |
| 424 |
parent, |
| 425 |
} as const; |
| 426 |
validateNested(envLoc, env); |
| 427 |
} |
| 428 |
} |
| 429 |
return obj; |
| 430 |
} |
| 431 |
|
| 432 |
function assertOverridesList( |
| 433 |
loc: OptionPath, |
| 434 |
value: unknown[], |
| 435 |
): undefined | OverridesList { |
| 436 |
if (loc.parent.type === "env") { |
| 437 |
throw new Error(`${msg(loc)} is not allowed inside an .env block`); |
| 438 |
} |
| 439 |
if (loc.parent.type === "overrides") { |
| 440 |
throw new Error(`${msg(loc)} is not allowed inside an .overrides block`); |
| 441 |
} |
| 442 |
const parent: RootPath = loc.parent; |
| 443 |
|
| 444 |
const arr = assertArray(loc, value); |
| 445 |
if (arr) { |
| 446 |
for (const [index, item] of arr.entries()) { |
| 447 |
const objLoc = access(loc, index); |
| 448 |
const env = assertObject(objLoc, item); |
| 449 |
if (!env) throw new Error(`${msg(objLoc)} must be an object`); |
| 450 |
|
| 451 |
const overridesLoc = { |
| 452 |
type: "overrides", |
| 453 |
index, |
| 454 |
parent, |
| 455 |
} as const; |
| 456 |
validateNested(overridesLoc, env); |
| 457 |
} |
| 458 |
} |
| 459 |
return arr as OverridesList; |
| 460 |
} |
| 461 |
|
| 462 |
export function checkNoUnwrappedItemOptionPairs( |
| 463 |
items: Array<UnloadedDescriptor>, |
| 464 |
index: number, |
| 465 |
type: "plugin" | "preset", |
| 466 |
e: Error, |
| 467 |
): void { |
| 468 |
if (index === 0) return; |
| 469 |
|
| 470 |
const lastItem = items[index - 1]; |
| 471 |
const thisItem = items[index]; |
| 472 |
|
| 473 |
if ( |
| 474 |
lastItem.file && |
| 475 |
lastItem.options === undefined && |
| 476 |
typeof thisItem.value === "object" |
| 477 |
) { |
| 478 |
e.message += |
| 479 |
`\n- Maybe you meant to use\n` + |
| 480 |
`"${type}s": [\n ["${lastItem.file.request}", ${JSON.stringify( |
| 481 |
thisItem.value, |
| 482 |
undefined, |
| 483 |
2, |
| 484 |
)}]\n]\n` + |
| 485 |
`To be a valid ${type}, its name and options should be wrapped in a pair of brackets`; |
| 486 |
} |
| 487 |
} |