Skip to content

Commit

Permalink
incorporate feedbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
ychi committed May 3, 2020
1 parent ee2c7eb commit 44ccac4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 26 deletions.
5 changes: 4 additions & 1 deletion babel.config.js
Expand Up @@ -40,7 +40,10 @@ module.exports = {
},
],
],
test: 'packages/jest-config/src/readConfigFileAndSetRootDir.ts',
test: [
'packages/jest-config/src/readConfigFileAndSetRootDir.ts',
'packages/jest-transform/src/ScriptTransformer.ts'
],
},
],
plugins: [
Expand Down
35 changes: 20 additions & 15 deletions packages/jest-transform/src/ScriptTransformer.ts
Expand Up @@ -29,6 +29,8 @@ import type {
TransformResult,
TransformedSource,
Transformer,
SyncTransformer,
AsyncTransformer
} from './types';
import shouldInstrument from './shouldInstrument';
import handlePotentialSyntaxError from './enhanceUnexpectedTokenMessage';
Expand Down Expand Up @@ -114,7 +116,7 @@ export default class ScriptTransformer {
}

private _getCacheKey(
fileData: string,
content: string,
filename: Config.Path,
instrument: boolean,
supportsDynamicImport: boolean,
Expand All @@ -126,7 +128,7 @@ export default class ScriptTransformer {

if (transformer && typeof transformer.getCacheKey === 'function') {
transformerCacheKey = transformer.getCacheKey(
fileData,
content,
filename,
configString,
{
Expand All @@ -139,7 +141,7 @@ export default class ScriptTransformer {
);
}
return this._buildCacheKeyFromFileInfo(
fileData,
content,
filename,
instrument,
configString,
Expand All @@ -148,7 +150,7 @@ export default class ScriptTransformer {
}

private async _getCacheKeyAsync(
fileData: string,
content: string,
filename: Config.Path,
instrument: boolean,
supportsDynamicImport: boolean,
Expand All @@ -160,7 +162,7 @@ export default class ScriptTransformer {

if (transformer && typeof transformer.getCacheKeyAsync === 'function') {
transformerCacheKey = await transformer.getCacheKeyAsync(
fileData,
content,
filename,
configString,
{
Expand All @@ -173,7 +175,7 @@ export default class ScriptTransformer {
);
}
return this._buildCacheKeyFromFileInfo(
fileData,
content,
filename,
instrument,
configString,
Expand Down Expand Up @@ -259,7 +261,7 @@ export default class ScriptTransformer {
}

private async _getTransformerAsync(filename: Config.Path) {
let transform: Transformer | null = null;
let transform: AsyncTransformer | null = null;
if (!this._config.transform || !this._config.transform.length) {
return null;
}
Expand Down Expand Up @@ -294,7 +296,7 @@ export default class ScriptTransformer {
}

private _getTransformer(filename: Config.Path) {
let transform: Transformer | null = null;
let transform: SyncTransformer | null = null;
if (!this._config.transform || !this._config.transform.length) {
return null;
}
Expand Down Expand Up @@ -592,17 +594,20 @@ export default class ScriptTransformer {
let processed: TransformedSource | null = null;

if (transform && shouldCallTransform) {
processed = transform.processAsync
? await transform.processAsync(content, filename, this._config, {

if(transform.processAsync) {
processed = await transform.processAsync(content, filename, this._config, {
instrument,
supportsDynamicImport,
supportsStaticESM,
})
: transform.process(content, filename, this._config, {
instrument,
supportsDynamicImport,
supportsStaticESM,
});
} else if (transform.process) {
processed = transform.process(content, filename, this._config, {
instrument,
supportsDynamicImport,
supportsStaticESM,
});
}

if (
processed == null ||
Expand Down
20 changes: 10 additions & 10 deletions packages/jest-transform/src/types.ts
Expand Up @@ -52,19 +52,19 @@ export interface CacheKeyOptions extends TransformOptions {
rootDir: string;
}

interface SyncTransFormer {
export interface SyncTransformer {
canInstrument?: boolean;
createTransformer?: (options?: any) => SyncTransFormer;
createTransformer?: (options?: any) => SyncTransformer;

getCacheKey?: (
fileDate: string,
content: string,
filePath: Config.Path,
configStr: string,
options: CacheKeyOptions,
) => string;

getCacheKeyAsync?: (
fileDate: string,
content: string,
filePath: Config.Path,
configStr: string,
options: CacheKeyOptions,
Expand All @@ -85,37 +85,37 @@ interface SyncTransFormer {
) => Promise<TransformedSource>;
}

interface AsyncTransformer {
export interface AsyncTransformer {
canInstrument?: boolean;
createTransformer?: (options?: any) => AsyncTransformer;

getCacheKey?: (
fileDate: string,
content: string,
filePath: Config.Path,
configStr: string,
options: CacheKeyOptions,
) => string;

getCacheKeyAsync?: (
fileDate: string,
content: string,
filePath: Config.Path,
configStr: string,
options: CacheKeyOptions,
) => Promise<string>;

process: (
process?: (
sourceText: string,
sourcePath: Config.Path,
config: Config.ProjectConfig,
options?: TransformOptions,
) => TransformedSource;

processAsync?: (
processAsync: (
sourceText: string,
sourcePath: Config.Path,
config: Config.ProjectConfig,
options?: TransformOptions,
) => Promise<TransformedSource>;
}

export type Transformer = SyncTransFormer | AsyncTransformer;
export type Transformer = SyncTransformer | AsyncTransformer;

0 comments on commit 44ccac4

Please sign in to comment.