Skip to content

Commit 15e2cc6

Browse files
JoshRosensteinSimenB
authored andcommitted
chore(jest-config): util types (#8437)
1 parent db5e472 commit 15e2cc6

File tree

2 files changed

+47
-29
lines changed

2 files changed

+47
-29
lines changed

packages/jest-config/src/normalize.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -659,14 +659,19 @@ export default function normalize(
659659
? _replaceRootDirTags(options.rootDir, project)
660660
: project,
661661
)
662-
.reduce((projects, project) => {
663-
// Project can be specified as globs. If a glob matches any files,
664-
// We expand it to these paths. If not, we keep the original path
665-
// for the future resolution.
666-
const globMatches =
667-
typeof project === 'string' ? glob.sync(project) : [];
668-
return projects.concat(globMatches.length ? globMatches : project);
669-
}, []);
662+
.reduce(
663+
(projects, project) => {
664+
// Project can be specified as globs. If a glob matches any files,
665+
// We expand it to these paths. If not, we keep the original path
666+
// for the future resolution.
667+
const globMatches =
668+
typeof project === 'string' ? glob.sync(project) : [];
669+
return projects.concat(
670+
globMatches.length ? globMatches : project,
671+
);
672+
},
673+
[] as Array<string>,
674+
);
670675
break;
671676
case 'moduleDirectories':
672677
case 'testMatch':

packages/jest-config/src/utils.ts

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ export const resolve = (
4848
${chalk.bold('<rootDir>')} is: ${rootDir}`,
4949
);
5050
}
51-
52-
return module;
51+
/// can cast as string since nulls will be thrown
52+
return module as string;
5353
};
5454

5555
export const escapeGlobCharacters = (path: Config.Path): Config.Glob =>
@@ -69,37 +69,50 @@ export const replaceRootDirInPath = (
6969
);
7070
};
7171

72-
// TODO: Type as returning same type as input
73-
const _replaceRootDirInObject = (
72+
const _replaceRootDirInObject = <T extends ReplaceRootDirConfigObj>(
7473
rootDir: Config.Path,
75-
config: any,
76-
): {[key: string]: unknown} => {
77-
if (config !== null) {
78-
const newConfig: {[key: string]: unknown} = {};
79-
for (const configKey in config) {
80-
newConfig[configKey] =
81-
configKey === 'rootDir'
82-
? config[configKey]
83-
: _replaceRootDirTags(rootDir, config[configKey]);
84-
}
85-
return newConfig;
74+
config: T,
75+
): T => {
76+
const newConfig = {} as T;
77+
for (const configKey in config) {
78+
newConfig[configKey] =
79+
configKey === 'rootDir'
80+
? config[configKey]
81+
: _replaceRootDirTags(rootDir, config[configKey]);
8682
}
87-
return config;
83+
return newConfig;
8884
};
8985

90-
// TODO: Type as returning same type as input
91-
export const _replaceRootDirTags = (rootDir: Config.Path, config: any): any => {
86+
type OrArray<T> = T | Array<T>;
87+
type ReplaceRootDirConfigObj = {[key: string]: Config.Path};
88+
type ReplaceRootDirConfigValues =
89+
| OrArray<ReplaceRootDirConfigObj>
90+
| OrArray<RegExp>
91+
| OrArray<Config.Path>;
92+
93+
export const _replaceRootDirTags = <T extends ReplaceRootDirConfigValues>(
94+
rootDir: Config.Path,
95+
config: T,
96+
): T => {
97+
if (config == null) {
98+
return config;
99+
}
92100
switch (typeof config) {
93101
case 'object':
94102
if (Array.isArray(config)) {
95-
return config.map(item => _replaceRootDirTags(rootDir, item));
103+
/// can be string[] or {}[]
104+
return config.map(item => _replaceRootDirTags(rootDir, item)) as T;
96105
}
97106
if (config instanceof RegExp) {
98107
return config;
99108
}
100-
return _replaceRootDirInObject(rootDir, config);
109+
110+
return _replaceRootDirInObject(
111+
rootDir,
112+
config as ReplaceRootDirConfigObj,
113+
) as T;
101114
case 'string':
102-
return replaceRootDirInPath(rootDir, config);
115+
return replaceRootDirInPath(rootDir, config) as T;
103116
}
104117
return config;
105118
};

0 commit comments

Comments
 (0)