From 501ed692c0aad4baacf219c336bffdfb69b56bff Mon Sep 17 00:00:00 2001 From: Peter Leonov Date: Mon, 10 Feb 2020 23:24:27 +0100 Subject: [PATCH] add the type guided by TS --- .../jest-reporters/src/coverage_worker.ts | 2 +- .../src/generateEmptyCoverage.ts | 8 +-- packages/jest-runtime/src/index.ts | 11 ++- .../jest-transform/src/ScriptTransformer.ts | 72 +++++++++++-------- packages/jest-types/src/Config.ts | 4 +- 5 files changed, 56 insertions(+), 41 deletions(-) diff --git a/packages/jest-reporters/src/coverage_worker.ts b/packages/jest-reporters/src/coverage_worker.ts index eb0d056d2644..360488da68a1 100644 --- a/packages/jest-reporters/src/coverage_worker.ts +++ b/packages/jest-reporters/src/coverage_worker.ts @@ -37,7 +37,7 @@ export function worker({ }: CoverageWorkerData): CoverageWorkerResult | null { return generateEmptyCoverage( fs.readFileSync(path, 'utf8'), - path, + {id: path, path}, globalConfig, config, options && options.changedFiles && new Set(options.changedFiles), diff --git a/packages/jest-reporters/src/generateEmptyCoverage.ts b/packages/jest-reporters/src/generateEmptyCoverage.ts index b026e9861b3e..3583b69d8b51 100644 --- a/packages/jest-reporters/src/generateEmptyCoverage.ts +++ b/packages/jest-reporters/src/generateEmptyCoverage.ts @@ -27,7 +27,7 @@ export type CoverageWorkerResult = export default function( source: string, - filename: Config.Path, + filename: Config.RPth, globalConfig: Config.GlobalConfig, config: Config.ProjectConfig, changedFiles?: Set, @@ -40,9 +40,9 @@ export default function( coverageProvider: globalConfig.coverageProvider, }; let coverageWorkerResult: CoverageWorkerResult | null = null; - if (shouldInstrument(filename, coverageOptions, config)) { + if (shouldInstrument(filename.path, coverageOptions, config)) { if (coverageOptions.coverageProvider === 'v8') { - const stat = fs.statSync(filename); + const stat = fs.statSync(filename.path); return { kind: 'V8Coverage', result: { @@ -60,7 +60,7 @@ export default function( }, ], scriptId: '0', - url: filename, + url: filename.path, }, }; } diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index c96b2912e5e6..3c79af3c23bd 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -55,7 +55,9 @@ type InternalModuleOptions = { }; type InitialModule = Partial & - Pick; + Pick & { + rpth: Config.RPth; + }; type ModuleRegistry = Map; type ResolveOptions = Parameters[1]; @@ -349,6 +351,7 @@ class Runtime { filename: modulePath, id: modulePath, loaded: false, + rpth: {id: 'InitialModule', path: modulePath}, }; moduleRegistry.set(modulePath, localModule); @@ -439,6 +442,7 @@ class Runtime { filename: modulePath, id: modulePath, loaded: false, + rpth: {id: 'InitialModule', path: modulePath}, }; this._loadModule( @@ -736,7 +740,7 @@ class Runtime { localModule: InitialModule, options: InternalModuleOptions | undefined, moduleRegistry: ModuleRegistry, - from: Config.Path | null, + from: Config.Path | null, // RPth? ) { // If the environment was disposed, prevent this module from being executed. if (!this._environment.global) { @@ -765,7 +769,7 @@ class Runtime { value: this._createRequireImplementation(localModule, options), }); const transformedFile = this._scriptTransformer.transform( - filename, + localModule.rpth, this._getFullTransformationOptions(options), this._cacheFS[filename], ); @@ -910,6 +914,7 @@ class Runtime { filename, id: filename, loaded: false, + rpth: {id: filename, path: filename}, }); }; diff --git a/packages/jest-transform/src/ScriptTransformer.ts b/packages/jest-transform/src/ScriptTransformer.ts index 18b3b46a07f0..5eec2bd97a3e 100644 --- a/packages/jest-transform/src/ScriptTransformer.ts +++ b/packages/jest-transform/src/ScriptTransformer.ts @@ -88,36 +88,44 @@ export default class ScriptTransformer { private _getCacheKey( fileData: string, - filename: Config.Path, + filename: Config.RPth, instrument: boolean, ): string { const configString = this._cache.configString; - const transformer = this._getTransformer(filename); + const transformer = this._getTransformer(filename.path); if (transformer && typeof transformer.getCacheKey === 'function') { - return createHash('md5') - .update( - transformer.getCacheKey(fileData, filename, configString, { - config: this._config, - instrument, - rootDir: this._config.rootDir, - }), - ) - .update(CACHE_VERSION) - .digest('hex'); + return ( + createHash('md5') + .update( + transformer.getCacheKey(fileData, filename.path, configString, { + config: this._config, + instrument, + rootDir: this._config.rootDir, + }), + ) + .update(CACHE_VERSION) + // required for the query string support + .update(filename.id) + .digest('hex') + ); } else { - return createHash('md5') - .update(fileData) - .update(configString) - .update(instrument ? 'instrument' : '') - .update(filename) - .update(CACHE_VERSION) - .digest('hex'); + return ( + createHash('md5') + .update(fileData) + .update(configString) + .update(instrument ? 'instrument' : '') + .update(filename.path) + // required for the query string support + .update(filename.id) + .update(CACHE_VERSION) + .digest('hex') + ); } } private _getFileCachePath( - filename: Config.Path, + filename: Config.RPth, content: string, instrument: boolean, ): Config.Path { @@ -131,7 +139,7 @@ export default class ScriptTransformer { // directory with many files. const cacheDir = path.join(baseCacheDir, cacheKey[0] + cacheKey[1]); const cacheFilenamePrefix = path - .basename(filename, path.extname(filename)) + .basename(filename.path, path.extname(filename.path)) .replace(/\W/g, ''); const cachePath = slash( path.join(cacheDir, cacheFilenamePrefix + '_' + cacheKey), @@ -252,13 +260,13 @@ export default class ScriptTransformer { } transformSource( - filepath: Config.Path, + filepath: Config.RPth, content: string, instrument: boolean, ): TransformResult { - const filename = this._getRealPath(filepath); + const filename = this._getRealPath(filepath.path); const transform = this._getTransformer(filename); - const cacheFilePath = this._getFileCachePath(filename, content, instrument); + const cacheFilePath = this._getFileCachePath(filepath, content, instrument); let sourceMapPath: Config.Path | null = cacheFilePath + '.map'; // Ignore cache if `config.cache` is set (--no-cache) let code = this._config.cache ? readCodeCacheFile(cacheFilePath) : null; @@ -374,7 +382,7 @@ export default class ScriptTransformer { } private _transformAndBuildScript( - filename: Config.Path, + filename: Config.RPth, options: Options | null, instrument: boolean, fileSource?: string, @@ -382,7 +390,7 @@ export default class ScriptTransformer { const isInternalModule = !!(options && options.isInternalModule); const isCoreModule = !!(options && options.isCoreModule); const content = stripShebang( - fileSource || fs.readFileSync(filename, 'utf8'), + fileSource || fs.readFileSync(filename.path, 'utf8'), ); let code = content; @@ -392,7 +400,7 @@ export default class ScriptTransformer { const willTransform = !isInternalModule && !isCoreModule && - (this.shouldTransform(filename) || instrument); + (this.shouldTransform(filename.path) || instrument); try { if (willTransform) { @@ -419,7 +427,7 @@ export default class ScriptTransformer { } transform( - filename: Config.Path, + filename: Config.RPth, options: Options, fileSource?: string, ): TransformResult { @@ -429,7 +437,7 @@ export default class ScriptTransformer { if (!options.isCoreModule) { instrument = options.coverageProvider === 'babel' && - shouldInstrument(filename, options, this._config); + shouldInstrument(filename.path, options, this._config); scriptCacheKey = getScriptCacheKey(filename, instrument); const result = this._cache.transformedFiles.get(scriptCacheKey); if (result) { @@ -463,6 +471,7 @@ export default class ScriptTransformer { if (willTransform) { const {code: transformedJsonSource} = this.transformSource( + // @ts-ignore filename, fileSource, false, @@ -494,6 +503,7 @@ export default class ScriptTransformer { (code, filename) => { try { transforming = true; + // @ts-ignore return this.transformSource(filename, code, false).code || code; } finally { transforming = false; @@ -690,8 +700,8 @@ const readCacheFile = (cachePath: Config.Path): string | null => { return fileData; }; -const getScriptCacheKey = (filename: Config.Path, instrument: boolean) => { - const mtime = fs.statSync(filename).mtime; +const getScriptCacheKey = (filename: Config.RPth, instrument: boolean) => { + const mtime = fs.statSync(filename.path).mtime; return filename + '_' + mtime.getTime() + (instrument ? '_instrumented' : ''); }; diff --git a/packages/jest-types/src/Config.ts b/packages/jest-types/src/Config.ts index 5d171bf339de..8c3d0aa8c454 100644 --- a/packages/jest-types/src/Config.ts +++ b/packages/jest-types/src/Config.ts @@ -22,9 +22,9 @@ export type RPth = { // 2. filename - just the file name w/o the containing directies // 3. extname - just the extension (to speed up a bit) // 4. dirname - just the dir name (to speed up a bit) - readonly path: string + readonly path: string; // prob. the real path + the query string - readonly id: PathID, + readonly id: PathID; }; export type Glob = string;