Skip to content

Commit

Permalink
Remove 'createTransformer' from Transformer types
Browse files Browse the repository at this point in the history
  • Loading branch information
fatso83 committed Feb 16, 2022
1 parent 199f981 commit 47062c7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
15 changes: 11 additions & 4 deletions packages/jest-transform/src/ScriptTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import type {
TransformResult,
TransformedSource,
Transformer,
TransformerFactory,
} from './types';
// Use `require` to avoid TS rootDir
const {version: VERSION} = require('../package.json');
Expand Down Expand Up @@ -72,6 +73,13 @@ async function waitForPromiseWithCleanup(
}
}

// type predicate
function isTransformerFactory(
t: Transformer | TransformerFactory,
): t is TransformerFactory {
return typeof (t as TransformerFactory).createTransformer === 'function';
}

class ScriptTransformer {
private readonly _cache: ProjectCache;
private readonly _transformCache = new Map<
Expand Down Expand Up @@ -259,14 +267,13 @@ class ScriptTransformer {
await Promise.all(
this._config.transform.map(
async ([, transformPath, transformerConfig]) => {
let transformer: Transformer = await requireOrImportModule(
transformPath,
);
let transformer: Transformer | TransformerFactory =
await requireOrImportModule(transformPath);

if (!transformer) {
throw new Error(makeInvalidTransformerError(transformPath));
}
if (typeof transformer.createTransformer === 'function') {
if (isTransformerFactory(transformer)) {
transformer = transformer.createTransformer(transformerConfig);
}
if (
Expand Down
8 changes: 6 additions & 2 deletions packages/jest-transform/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ export interface SyncTransformer<OptionType = unknown> {
* If V8 coverage is _not_ active, and this is `false`. Jest will instrument the code returned by this transformer using Babel.
*/
canInstrument?: boolean;
createTransformer?: (options?: OptionType) => SyncTransformer<OptionType>;

getCacheKey?: (
sourceText: string,
Expand Down Expand Up @@ -111,7 +110,6 @@ export interface AsyncTransformer<OptionType = unknown> {
* If V8 coverage is _not_ active, and this is `false`. Jest will instrument the code returned by this transformer using Babel.
*/
canInstrument?: boolean;
createTransformer?: (options?: OptionType) => AsyncTransformer<OptionType>;

getCacheKey?: (
sourceText: string,
Expand Down Expand Up @@ -141,3 +139,9 @@ export interface AsyncTransformer<OptionType = unknown> {
export type Transformer<OptionType = unknown> =
| SyncTransformer<OptionType>
| AsyncTransformer<OptionType>;

type TransformerCreator<OptionType = unknown> = (
options?: OptionType,
) => Transformer<OptionType>;

export type TransformerFactory = {createTransformer: TransformerCreator};

0 comments on commit 47062c7

Please sign in to comment.