Expose optional caller metadata to Babel ignore/only/test/include/exclude functions.

This commit is contained in:
Logan Smyth
2018-05-07 11:16:05 -07:00
parent 2c3c12fdf7
commit 8f4bae8ea4
5 changed files with 112 additions and 5 deletions

View File

@@ -8,6 +8,7 @@ import {
type IgnoreList,
type ConfigApplicableTest,
type BabelrcSearch,
type CallerMetadata,
} from "./validation/options";
import pathPatternToRegex from "./pattern-to-regex";
@@ -50,6 +51,7 @@ export type ConfigContext = {
cwd: string,
root: string,
envName: string,
caller: CallerMetadata | void,
};
/**
@@ -248,7 +250,7 @@ function babelrcLoadEnabled(
if (typeof pat === "string") pat = pathPatternToRegex(pat, context.cwd);
return pkgData.directories.some(directory => {
return matchPattern(pat, context.cwd, directory);
return matchPattern(pat, context.cwd, directory, context);
});
});
}
@@ -643,12 +645,23 @@ function matchesPatterns(
dirname: string,
): boolean {
return patterns.some(pattern =>
matchPattern(pattern, dirname, context.filename),
matchPattern(pattern, dirname, context.filename, context),
);
}
function matchPattern(pattern, dirname, pathToTest): boolean {
if (typeof pattern === "function") return !!pattern(pathToTest);
function matchPattern(
pattern,
dirname,
pathToTest,
context: ConfigContext,
): boolean {
if (typeof pattern === "function") {
return !!pattern(pathToTest, {
dirname,
envName: context.envName,
caller: context.caller,
});
}
if (typeof pathToTest !== "string") {
throw new Error(

View File

@@ -28,7 +28,7 @@ export default function loadPrivatePartialConfig(
const args = inputOpts ? validate("arguments", inputOpts) : {};
const { envName = getEnv(), cwd = ".", root: rootDir = "." } = args;
const { envName = getEnv(), cwd = ".", root: rootDir = ".", caller } = args;
const absoluteCwd = path.resolve(cwd);
const absoluteRootDir = path.resolve(absoluteCwd, rootDir);
@@ -40,6 +40,7 @@ export default function loadPrivatePartialConfig(
cwd: absoluteCwd,
root: absoluteRootDir,
envName,
caller,
};
const configChain = buildRootChain(args, context);

View File

@@ -14,6 +14,7 @@ import type {
CompactOption,
RootInputSourceMapOption,
NestingPath,
CallerMetadata,
} from "./options";
export type ValidatorSet = {
@@ -103,6 +104,41 @@ export function assertSourceType(
return value;
}
export function assertCallerMetadata(
loc: OptionPath,
value: mixed,
): CallerMetadata | void {
const obj = assertObject(loc, value);
if (obj) {
if (typeof obj[("name": string)] !== "string") {
throw new Error(
`${msg(loc)} set but does not contain "name" property string`,
);
}
for (const prop of Object.keys(obj)) {
const propLoc = access(loc, prop);
const value = obj[prop];
if (
value != null &&
typeof value !== "boolean" &&
typeof value !== "string" &&
typeof value !== "number"
) {
// NOTE(logan): I'm limiting the type here so that we can guarantee that
// the "caller" value will serialize to JSON nicely. We can always
// allow more complex structures later though.
throw new Error(
`${msg(
propLoc,
)} must be null, undefined, a boolean, a string, or a number.`,
);
}
}
}
return (value: any);
}
export function assertInputSourceMap(
loc: OptionPath,
value: mixed,

View File

@@ -11,6 +11,7 @@ import {
assertBoolean,
assertObject,
assertArray,
assertCallerMetadata,
assertInputSourceMap,
assertIgnoreList,
assertPluginList,
@@ -33,6 +34,9 @@ const ROOT_VALIDATORS: ValidatorSet = {
$PropertyType<ValidatedOptions, "configFile">,
>),
caller: (assertCallerMetadata: Validator<
$PropertyType<ValidatedOptions, "caller">,
>),
filename: (assertString: Validator<
$PropertyType<ValidatedOptions, "filename">,
>),
@@ -176,6 +180,7 @@ export type ValidatedOptions = {
ast?: boolean,
inputSourceMap?: RootInputSourceMapOption,
envName?: string,
caller?: CallerMetadata,
extends?: string,
env?: EnvSet<ValidatedOptions>,
@@ -225,6 +230,11 @@ export type ValidatedOptions = {
generatorOpts?: {},
};
export type CallerMetadata = {
// If 'caller' is specified, require that the name is given for debugging
// messages.
name: string,
};
export type EnvSet<T> = {
[string]: ?T,
};