Skip to content

Commit 629ebf2

Browse files
committed
incorporate feedbacks
1 parent af888f1 commit 629ebf2

File tree

3 files changed

+34
-26
lines changed

3 files changed

+34
-26
lines changed

babel.config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ module.exports = {
4040
},
4141
],
4242
],
43-
test: 'packages/jest-config/src/readConfigFileAndSetRootDir.ts',
43+
test: [
44+
'packages/jest-config/src/readConfigFileAndSetRootDir.ts',
45+
'packages/jest-transform/src/ScriptTransformer.ts'
46+
],
4447
},
4548
],
4649
plugins: [

packages/jest-transform/src/ScriptTransformer.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import type {
2929
TransformResult,
3030
TransformedSource,
3131
Transformer,
32+
SyncTransformer,
33+
AsyncTransformer
3234
} from './types';
3335
import shouldInstrument from './shouldInstrument';
3436
import handlePotentialSyntaxError from './enhanceUnexpectedTokenMessage';
@@ -114,7 +116,7 @@ export default class ScriptTransformer {
114116
}
115117

116118
private _getCacheKey(
117-
fileData: string,
119+
content: string,
118120
filename: Config.Path,
119121
instrument: boolean,
120122
supportsDynamicImport: boolean,
@@ -126,7 +128,7 @@ export default class ScriptTransformer {
126128

127129
if (transformer && typeof transformer.getCacheKey === 'function') {
128130
transformerCacheKey = transformer.getCacheKey(
129-
fileData,
131+
content,
130132
filename,
131133
configString,
132134
{
@@ -139,7 +141,7 @@ export default class ScriptTransformer {
139141
);
140142
}
141143
return this._buildCacheKeyFromFileInfo(
142-
fileData,
144+
content,
143145
filename,
144146
instrument,
145147
configString,
@@ -148,7 +150,7 @@ export default class ScriptTransformer {
148150
}
149151

150152
private async _getCacheKeyAsync(
151-
fileData: string,
153+
content: string,
152154
filename: Config.Path,
153155
instrument: boolean,
154156
supportsDynamicImport: boolean,
@@ -160,7 +162,7 @@ export default class ScriptTransformer {
160162

161163
if (transformer && typeof transformer.getCacheKeyAsync === 'function') {
162164
transformerCacheKey = await transformer.getCacheKeyAsync(
163-
fileData,
165+
content,
164166
filename,
165167
configString,
166168
{
@@ -173,7 +175,7 @@ export default class ScriptTransformer {
173175
);
174176
}
175177
return this._buildCacheKeyFromFileInfo(
176-
fileData,
178+
content,
177179
filename,
178180
instrument,
179181
configString,
@@ -259,7 +261,7 @@ export default class ScriptTransformer {
259261
}
260262

261263
private async _getTransformerAsync(filename: Config.Path) {
262-
let transform: Transformer | null = null;
264+
let transform: AsyncTransformer | null = null;
263265
if (!this._config.transform || !this._config.transform.length) {
264266
return null;
265267
}
@@ -294,7 +296,7 @@ export default class ScriptTransformer {
294296
}
295297

296298
private _getTransformer(filename: Config.Path) {
297-
let transform: Transformer | null = null;
299+
let transform: SyncTransformer | null = null;
298300
if (!this._config.transform || !this._config.transform.length) {
299301
return null;
300302
}
@@ -592,17 +594,20 @@ export default class ScriptTransformer {
592594
let processed: TransformedSource | null = null;
593595

594596
if (transform && shouldCallTransform) {
595-
processed = transform.processAsync
596-
? await transform.processAsync(content, filename, this._config, {
597+
598+
if(transform.processAsync) {
599+
processed = await transform.processAsync(content, filename, this._config, {
597600
instrument,
598601
supportsDynamicImport,
599602
supportsStaticESM,
600603
})
601-
: transform.process(content, filename, this._config, {
602-
instrument,
603-
supportsDynamicImport,
604-
supportsStaticESM,
605-
});
604+
} else if (transform.process) {
605+
processed = transform.process(content, filename, this._config, {
606+
instrument,
607+
supportsDynamicImport,
608+
supportsStaticESM,
609+
});
610+
}
606611

607612
if (
608613
processed == null ||

packages/jest-transform/src/types.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,19 @@ export interface CacheKeyOptions extends TransformOptions {
5252
rootDir: string;
5353
}
5454

55-
interface SyncTransFormer {
55+
export interface SyncTransformer {
5656
canInstrument?: boolean;
57-
createTransformer?: (options?: any) => SyncTransFormer;
57+
createTransformer?: (options?: any) => SyncTransformer;
5858

5959
getCacheKey?: (
60-
fileDate: string,
60+
content: string,
6161
filePath: Config.Path,
6262
configStr: string,
6363
options: CacheKeyOptions,
6464
) => string;
6565

6666
getCacheKeyAsync?: (
67-
fileDate: string,
67+
content: string,
6868
filePath: Config.Path,
6969
configStr: string,
7070
options: CacheKeyOptions,
@@ -85,37 +85,37 @@ interface SyncTransFormer {
8585
) => Promise<TransformedSource>;
8686
}
8787

88-
interface AsyncTransformer {
88+
export interface AsyncTransformer {
8989
canInstrument?: boolean;
9090
createTransformer?: (options?: any) => AsyncTransformer;
9191

9292
getCacheKey?: (
93-
fileDate: string,
93+
content: string,
9494
filePath: Config.Path,
9595
configStr: string,
9696
options: CacheKeyOptions,
9797
) => string;
9898

9999
getCacheKeyAsync?: (
100-
fileDate: string,
100+
content: string,
101101
filePath: Config.Path,
102102
configStr: string,
103103
options: CacheKeyOptions,
104104
) => Promise<string>;
105105

106-
process: (
106+
process?: (
107107
sourceText: string,
108108
sourcePath: Config.Path,
109109
config: Config.ProjectConfig,
110110
options?: TransformOptions,
111111
) => TransformedSource;
112112

113-
processAsync?: (
113+
processAsync: (
114114
sourceText: string,
115115
sourcePath: Config.Path,
116116
config: Config.ProjectConfig,
117117
options?: TransformOptions,
118118
) => Promise<TransformedSource>;
119119
}
120120

121-
export type Transformer = SyncTransFormer | AsyncTransformer;
121+
export type Transformer = SyncTransformer | AsyncTransformer;

0 commit comments

Comments
 (0)