Skip to content

Commit

Permalink
Move type casts to config validation
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Aug 25, 2022
1 parent ce9f2d2 commit 89542ad
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
4 changes: 2 additions & 2 deletions packages/babel-core/src/config/config-chain.ts
Expand Up @@ -54,7 +54,7 @@ export type PresetInstance = {
};

export type ConfigContext = {
filename: string | void;
filename: string | undefined;
cwd: string;
root: string;
envName: string;
Expand Down Expand Up @@ -899,7 +899,7 @@ function matchesPatterns(
function matchPattern(
pattern: IgnoreItem,
dirname: string,
pathToTest: unknown,
pathToTest: string | undefined,
context: ConfigContext,
configName?: string,
): boolean {
Expand Down
Expand Up @@ -256,7 +256,7 @@ function assertIgnoreItem(loc: GeneralPath, value: unknown): IgnoreItem {
)} must be an array of string/Function/RegExp values, or undefined`,
);
}
return value;
return value as IgnoreItem;
}

export function assertConfigApplicableTest(
Expand All @@ -278,7 +278,7 @@ export function assertConfigApplicableTest(
`${msg(loc)} must be a string/Function/RegExp, or an array of those`,
);
}
return value;
return value as ConfigApplicableTest;
}

function checkValidTest(value: unknown): value is string | Function | RegExp {
Expand Down Expand Up @@ -327,7 +327,7 @@ export function assertBabelrcSearch(
`or an array of those, got ${JSON.stringify(value as any)}`,
);
}
return value;
return value as BabelrcSearch;
}

export function assertPluginList(
Expand Down
8 changes: 7 additions & 1 deletion packages/babel-core/src/config/validation/options.ts
Expand Up @@ -201,7 +201,13 @@ export type CallerMetadata = {
export type EnvSet<T> = {
[x: string]: T;
};
export type IgnoreItem = string | Function | RegExp;
export type IgnoreItem =
| string
| RegExp
| ((
path: string | undefined,
context: { dirname: string; caller: CallerMetadata; envName: string },
) => unknown);
export type IgnoreList = ReadonlyArray<IgnoreItem>;

export type PluginOptions = object | void | false;
Expand Down
20 changes: 12 additions & 8 deletions packages/babel-core/src/errors/rewrite-stack-trace.ts
Expand Up @@ -94,26 +94,30 @@ export function expectedError(error: Error) {
return error;
}

export function beginHiddenCallStack<Fn extends Function>(fn: Fn): Fn {
export function beginHiddenCallStack<A extends unknown[], R>(
fn: (...args: A) => R,
) {
if (!SUPPORTED) return fn;

return Object.defineProperty(
function (this: any) {
function (...args: A) {
setupPrepareStackTrace();
return fn.apply(this, arguments);
} as any as Fn,
return fn(...args);
},
"name",
{ value: STOP_HIDNG },
);
}

export function endHiddenCallStack<Fn extends Function>(fn: Fn): Fn {
export function endHiddenCallStack<A extends unknown[], R>(
fn: (...args: A) => R,
) {
if (!SUPPORTED) return fn;

return Object.defineProperty(
function (this: any) {
return fn.apply(this, arguments);
} as any as Fn,
function (...args: A) {
return fn(...args);
},
"name",
{ value: START_HIDNG },
);
Expand Down

0 comments on commit 89542ad

Please sign in to comment.