Skip to content

Commit

Permalink
chore(jest-config): util types (#8437)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshRosenstein authored and SimenB committed May 11, 2019
1 parent db5e472 commit 15e2cc6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 29 deletions.
21 changes: 13 additions & 8 deletions packages/jest-config/src/normalize.ts
Expand Up @@ -659,14 +659,19 @@ export default function normalize(
? _replaceRootDirTags(options.rootDir, project)
: project,
)
.reduce((projects, project) => {
// Project can be specified as globs. If a glob matches any files,
// We expand it to these paths. If not, we keep the original path
// for the future resolution.
const globMatches =
typeof project === 'string' ? glob.sync(project) : [];
return projects.concat(globMatches.length ? globMatches : project);
}, []);
.reduce(
(projects, project) => {
// Project can be specified as globs. If a glob matches any files,
// We expand it to these paths. If not, we keep the original path
// for the future resolution.
const globMatches =
typeof project === 'string' ? glob.sync(project) : [];
return projects.concat(
globMatches.length ? globMatches : project,
);
},
[] as Array<string>,
);
break;
case 'moduleDirectories':
case 'testMatch':
Expand Down
55 changes: 34 additions & 21 deletions packages/jest-config/src/utils.ts
Expand Up @@ -48,8 +48,8 @@ export const resolve = (
${chalk.bold('<rootDir>')} is: ${rootDir}`,
);
}

return module;
/// can cast as string since nulls will be thrown
return module as string;
};

export const escapeGlobCharacters = (path: Config.Path): Config.Glob =>
Expand All @@ -69,37 +69,50 @@ export const replaceRootDirInPath = (
);
};

// TODO: Type as returning same type as input
const _replaceRootDirInObject = (
const _replaceRootDirInObject = <T extends ReplaceRootDirConfigObj>(
rootDir: Config.Path,
config: any,
): {[key: string]: unknown} => {
if (config !== null) {
const newConfig: {[key: string]: unknown} = {};
for (const configKey in config) {
newConfig[configKey] =
configKey === 'rootDir'
? config[configKey]
: _replaceRootDirTags(rootDir, config[configKey]);
}
return newConfig;
config: T,
): T => {
const newConfig = {} as T;
for (const configKey in config) {
newConfig[configKey] =
configKey === 'rootDir'
? config[configKey]
: _replaceRootDirTags(rootDir, config[configKey]);
}
return config;
return newConfig;
};

// TODO: Type as returning same type as input
export const _replaceRootDirTags = (rootDir: Config.Path, config: any): any => {
type OrArray<T> = T | Array<T>;
type ReplaceRootDirConfigObj = {[key: string]: Config.Path};
type ReplaceRootDirConfigValues =
| OrArray<ReplaceRootDirConfigObj>
| OrArray<RegExp>
| OrArray<Config.Path>;

export const _replaceRootDirTags = <T extends ReplaceRootDirConfigValues>(
rootDir: Config.Path,
config: T,
): T => {
if (config == null) {
return config;
}
switch (typeof config) {
case 'object':
if (Array.isArray(config)) {
return config.map(item => _replaceRootDirTags(rootDir, item));
/// can be string[] or {}[]
return config.map(item => _replaceRootDirTags(rootDir, item)) as T;
}
if (config instanceof RegExp) {
return config;
}
return _replaceRootDirInObject(rootDir, config);

return _replaceRootDirInObject(
rootDir,
config as ReplaceRootDirConfigObj,
) as T;
case 'string':
return replaceRootDirInPath(rootDir, config);
return replaceRootDirInPath(rootDir, config) as T;
}
return config;
};
Expand Down

0 comments on commit 15e2cc6

Please sign in to comment.