Skip to content

Commit 11b581a

Browse files
committed
add the type guided by TS
1 parent 1885ce7 commit 11b581a

File tree

5 files changed

+56
-41
lines changed

5 files changed

+56
-41
lines changed

packages/jest-reporters/src/coverage_worker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export function worker({
3737
}: CoverageWorkerData): CoverageWorkerResult | null {
3838
return generateEmptyCoverage(
3939
fs.readFileSync(path, 'utf8'),
40-
path,
40+
{id: path, path},
4141
globalConfig,
4242
config,
4343
options && options.changedFiles && new Set(options.changedFiles),

packages/jest-reporters/src/generateEmptyCoverage.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export type CoverageWorkerResult =
2727

2828
export default function(
2929
source: string,
30-
filename: Config.Path,
30+
filename: Config.RPth,
3131
globalConfig: Config.GlobalConfig,
3232
config: Config.ProjectConfig,
3333
changedFiles?: Set<Config.Path>,
@@ -40,9 +40,9 @@ export default function(
4040
coverageProvider: globalConfig.coverageProvider,
4141
};
4242
let coverageWorkerResult: CoverageWorkerResult | null = null;
43-
if (shouldInstrument(filename, coverageOptions, config)) {
43+
if (shouldInstrument(filename.path, coverageOptions, config)) {
4444
if (coverageOptions.coverageProvider === 'v8') {
45-
const stat = fs.statSync(filename);
45+
const stat = fs.statSync(filename.path);
4646
return {
4747
kind: 'V8Coverage',
4848
result: {
@@ -60,7 +60,7 @@ export default function(
6060
},
6161
],
6262
scriptId: '0',
63-
url: filename,
63+
url: filename.path,
6464
},
6565
};
6666
}

packages/jest-runtime/src/index.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ type InternalModuleOptions = {
5555
};
5656

5757
type InitialModule = Partial<Module> &
58-
Pick<Module, 'children' | 'exports' | 'filename' | 'id' | 'loaded'>;
58+
Pick<Module, 'children' | 'exports' | 'filename' | 'id' | 'loaded'> & {
59+
rpth: Config.RPth;
60+
};
5961
type ModuleRegistry = Map<string, InitialModule | Module>;
6062
type ResolveOptions = Parameters<typeof require.resolve>[1];
6163

@@ -355,6 +357,7 @@ class Runtime {
355357
filename: modulePath,
356358
id: modulePath,
357359
loaded: false,
360+
rpth: {id: 'InitialModule', path: modulePath},
358361
};
359362
moduleRegistry.set(modulePath, localModule);
360363

@@ -445,6 +448,7 @@ class Runtime {
445448
filename: modulePath,
446449
id: modulePath,
447450
loaded: false,
451+
rpth: {id: 'InitialModule', path: modulePath},
448452
};
449453

450454
this._loadModule(
@@ -742,7 +746,7 @@ class Runtime {
742746
localModule: InitialModule,
743747
options: InternalModuleOptions | undefined,
744748
moduleRegistry: ModuleRegistry,
745-
from: Config.Path | null,
749+
from: Config.Path | null, // RPth?
746750
) {
747751
// If the environment was disposed, prevent this module from being executed.
748752
if (!this._environment.global) {
@@ -771,7 +775,7 @@ class Runtime {
771775
value: this._createRequireImplementation(localModule, options),
772776
});
773777
const transformedFile = this._scriptTransformer.transform(
774-
filename,
778+
localModule.rpth,
775779
this._getFullTransformationOptions(options),
776780
this._cacheFS[filename],
777781
);
@@ -916,6 +920,7 @@ class Runtime {
916920
filename,
917921
id: filename,
918922
loaded: false,
923+
rpth: {id: filename, path: filename},
919924
});
920925
};
921926

packages/jest-transform/src/ScriptTransformer.ts

+41-31
Original file line numberDiff line numberDiff line change
@@ -88,36 +88,44 @@ export default class ScriptTransformer {
8888

8989
private _getCacheKey(
9090
fileData: string,
91-
filename: Config.Path,
91+
filename: Config.RPth,
9292
instrument: boolean,
9393
): string {
9494
const configString = this._cache.configString;
95-
const transformer = this._getTransformer(filename);
95+
const transformer = this._getTransformer(filename.path);
9696

9797
if (transformer && typeof transformer.getCacheKey === 'function') {
98-
return createHash('md5')
99-
.update(
100-
transformer.getCacheKey(fileData, filename, configString, {
101-
config: this._config,
102-
instrument,
103-
rootDir: this._config.rootDir,
104-
}),
105-
)
106-
.update(CACHE_VERSION)
107-
.digest('hex');
98+
return (
99+
createHash('md5')
100+
.update(
101+
transformer.getCacheKey(fileData, filename.path, configString, {
102+
config: this._config,
103+
instrument,
104+
rootDir: this._config.rootDir,
105+
}),
106+
)
107+
.update(CACHE_VERSION)
108+
// required for the query string support
109+
.update(filename.id)
110+
.digest('hex')
111+
);
108112
} else {
109-
return createHash('md5')
110-
.update(fileData)
111-
.update(configString)
112-
.update(instrument ? 'instrument' : '')
113-
.update(filename)
114-
.update(CACHE_VERSION)
115-
.digest('hex');
113+
return (
114+
createHash('md5')
115+
.update(fileData)
116+
.update(configString)
117+
.update(instrument ? 'instrument' : '')
118+
.update(filename.path)
119+
// required for the query string support
120+
.update(filename.id)
121+
.update(CACHE_VERSION)
122+
.digest('hex')
123+
);
116124
}
117125
}
118126

119127
private _getFileCachePath(
120-
filename: Config.Path,
128+
filename: Config.RPth,
121129
content: string,
122130
instrument: boolean,
123131
): Config.Path {
@@ -131,7 +139,7 @@ export default class ScriptTransformer {
131139
// directory with many files.
132140
const cacheDir = path.join(baseCacheDir, cacheKey[0] + cacheKey[1]);
133141
const cacheFilenamePrefix = path
134-
.basename(filename, path.extname(filename))
142+
.basename(filename.path, path.extname(filename.path))
135143
.replace(/\W/g, '');
136144
const cachePath = slash(
137145
path.join(cacheDir, cacheFilenamePrefix + '_' + cacheKey),
@@ -251,13 +259,13 @@ export default class ScriptTransformer {
251259
}
252260

253261
transformSource(
254-
filepath: Config.Path,
262+
filepath: Config.RPth,
255263
content: string,
256264
instrument: boolean,
257265
): TransformResult {
258-
const filename = this._getRealPath(filepath);
266+
const filename = this._getRealPath(filepath.path);
259267
const transform = this._getTransformer(filename);
260-
const cacheFilePath = this._getFileCachePath(filename, content, instrument);
268+
const cacheFilePath = this._getFileCachePath(filepath, content, instrument);
261269
let sourceMapPath: Config.Path | null = cacheFilePath + '.map';
262270
// Ignore cache if `config.cache` is set (--no-cache)
263271
let code = this._config.cache ? readCodeCacheFile(cacheFilePath) : null;
@@ -373,15 +381,15 @@ export default class ScriptTransformer {
373381
}
374382

375383
private _transformAndBuildScript(
376-
filename: Config.Path,
384+
filename: Config.RPth,
377385
options: Options | null,
378386
instrument: boolean,
379387
fileSource?: string,
380388
): TransformResult {
381389
const isInternalModule = !!(options && options.isInternalModule);
382390
const isCoreModule = !!(options && options.isCoreModule);
383391
const content = stripShebang(
384-
fileSource || fs.readFileSync(filename, 'utf8'),
392+
fileSource || fs.readFileSync(filename.path, 'utf8'),
385393
);
386394

387395
let code = content;
@@ -391,7 +399,7 @@ export default class ScriptTransformer {
391399
const willTransform =
392400
!isInternalModule &&
393401
!isCoreModule &&
394-
(this.shouldTransform(filename) || instrument);
402+
(this.shouldTransform(filename.path) || instrument);
395403

396404
try {
397405
if (willTransform) {
@@ -418,7 +426,7 @@ export default class ScriptTransformer {
418426
}
419427

420428
transform(
421-
filename: Config.Path,
429+
filename: Config.RPth,
422430
options: Options,
423431
fileSource?: string,
424432
): TransformResult {
@@ -428,7 +436,7 @@ export default class ScriptTransformer {
428436
if (!options.isCoreModule) {
429437
instrument =
430438
options.coverageProvider === 'babel' &&
431-
shouldInstrument(filename, options, this._config);
439+
shouldInstrument(filename.path, options, this._config);
432440
scriptCacheKey = getScriptCacheKey(filename, instrument);
433441
const result = this._cache.transformedFiles.get(scriptCacheKey);
434442
if (result) {
@@ -462,6 +470,7 @@ export default class ScriptTransformer {
462470

463471
if (willTransform) {
464472
const {code: transformedJsonSource} = this.transformSource(
473+
// @ts-ignore
465474
filename,
466475
fileSource,
467476
false,
@@ -493,6 +502,7 @@ export default class ScriptTransformer {
493502
(code, filename) => {
494503
try {
495504
transforming = true;
505+
// @ts-ignore
496506
return this.transformSource(filename, code, false).code || code;
497507
} finally {
498508
transforming = false;
@@ -689,8 +699,8 @@ const readCacheFile = (cachePath: Config.Path): string | null => {
689699
return fileData;
690700
};
691701

692-
const getScriptCacheKey = (filename: Config.Path, instrument: boolean) => {
693-
const mtime = fs.statSync(filename).mtime;
702+
const getScriptCacheKey = (filename: Config.RPth, instrument: boolean) => {
703+
const mtime = fs.statSync(filename.path).mtime;
694704
return filename + '_' + mtime.getTime() + (instrument ? '_instrumented' : '');
695705
};
696706

packages/jest-types/src/Config.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ export type RPth = {
2222
// 2. filename - just the file name w/o the containing directies
2323
// 3. extname - just the extension (to speed up a bit)
2424
// 4. dirname - just the dir name (to speed up a bit)
25-
readonly path: string
25+
readonly path: string;
2626
// prob. the real path + the query string
27-
readonly id: PathID,
27+
readonly id: PathID;
2828
};
2929

3030
export type Glob = string;

0 commit comments

Comments
 (0)