From 629ebf2da675a7fd3a9577efbe61b1208e3fd1da Mon Sep 17 00:00:00 2001 From: Yen-Chi Chen Date: Sun, 3 May 2020 21:48:51 +0200 Subject: [PATCH] incorporate feedbacks --- babel.config.js | 5 ++- .../jest-transform/src/ScriptTransformer.ts | 35 +++++++++++-------- packages/jest-transform/src/types.ts | 20 +++++------ 3 files changed, 34 insertions(+), 26 deletions(-) diff --git a/babel.config.js b/babel.config.js index d2ff271d1ea6..129f3490ba30 100644 --- a/babel.config.js +++ b/babel.config.js @@ -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: [ diff --git a/packages/jest-transform/src/ScriptTransformer.ts b/packages/jest-transform/src/ScriptTransformer.ts index ef298011f276..a99c521d1732 100644 --- a/packages/jest-transform/src/ScriptTransformer.ts +++ b/packages/jest-transform/src/ScriptTransformer.ts @@ -29,6 +29,8 @@ import type { TransformResult, TransformedSource, Transformer, + SyncTransformer, + AsyncTransformer } from './types'; import shouldInstrument from './shouldInstrument'; import handlePotentialSyntaxError from './enhanceUnexpectedTokenMessage'; @@ -114,7 +116,7 @@ export default class ScriptTransformer { } private _getCacheKey( - fileData: string, + content: string, filename: Config.Path, instrument: boolean, supportsDynamicImport: boolean, @@ -126,7 +128,7 @@ export default class ScriptTransformer { if (transformer && typeof transformer.getCacheKey === 'function') { transformerCacheKey = transformer.getCacheKey( - fileData, + content, filename, configString, { @@ -139,7 +141,7 @@ export default class ScriptTransformer { ); } return this._buildCacheKeyFromFileInfo( - fileData, + content, filename, instrument, configString, @@ -148,7 +150,7 @@ export default class ScriptTransformer { } private async _getCacheKeyAsync( - fileData: string, + content: string, filename: Config.Path, instrument: boolean, supportsDynamicImport: boolean, @@ -160,7 +162,7 @@ export default class ScriptTransformer { if (transformer && typeof transformer.getCacheKeyAsync === 'function') { transformerCacheKey = await transformer.getCacheKeyAsync( - fileData, + content, filename, configString, { @@ -173,7 +175,7 @@ export default class ScriptTransformer { ); } return this._buildCacheKeyFromFileInfo( - fileData, + content, filename, instrument, configString, @@ -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; } @@ -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; } @@ -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 || diff --git a/packages/jest-transform/src/types.ts b/packages/jest-transform/src/types.ts index 01f6af70f475..a305a2d2353e 100644 --- a/packages/jest-transform/src/types.ts +++ b/packages/jest-transform/src/types.ts @@ -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, @@ -85,32 +85,32 @@ interface SyncTransFormer { ) => Promise; } -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; - process: ( + process?: ( sourceText: string, sourcePath: Config.Path, config: Config.ProjectConfig, options?: TransformOptions, ) => TransformedSource; - processAsync?: ( + processAsync: ( sourceText: string, sourcePath: Config.Path, config: Config.ProjectConfig, @@ -118,4 +118,4 @@ interface AsyncTransformer { ) => Promise; } -export type Transformer = SyncTransFormer | AsyncTransformer; +export type Transformer = SyncTransformer | AsyncTransformer;