Skip to content

Commit

Permalink
add the type guided by TS
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-leonov committed Mar 8, 2020
1 parent 1885ce7 commit 11b581a
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 41 deletions.
2 changes: 1 addition & 1 deletion packages/jest-reporters/src/coverage_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
8 changes: 4 additions & 4 deletions packages/jest-reporters/src/generateEmptyCoverage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Config.Path>,
Expand All @@ -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: {
Expand All @@ -60,7 +60,7 @@ export default function(
},
],
scriptId: '0',
url: filename,
url: filename.path,
},
};
}
Expand Down
11 changes: 8 additions & 3 deletions packages/jest-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ type InternalModuleOptions = {
};

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

Expand Down Expand Up @@ -355,6 +357,7 @@ class Runtime {
filename: modulePath,
id: modulePath,
loaded: false,
rpth: {id: 'InitialModule', path: modulePath},
};
moduleRegistry.set(modulePath, localModule);

Expand Down Expand Up @@ -445,6 +448,7 @@ class Runtime {
filename: modulePath,
id: modulePath,
loaded: false,
rpth: {id: 'InitialModule', path: modulePath},
};

this._loadModule(
Expand Down Expand Up @@ -742,7 +746,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) {
Expand Down Expand Up @@ -771,7 +775,7 @@ class Runtime {
value: this._createRequireImplementation(localModule, options),
});
const transformedFile = this._scriptTransformer.transform(
filename,
localModule.rpth,
this._getFullTransformationOptions(options),
this._cacheFS[filename],
);
Expand Down Expand Up @@ -916,6 +920,7 @@ class Runtime {
filename,
id: filename,
loaded: false,
rpth: {id: filename, path: filename},
});
};

Expand Down
72 changes: 41 additions & 31 deletions packages/jest-transform/src/ScriptTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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),
Expand Down Expand Up @@ -251,13 +259,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;
Expand Down Expand Up @@ -373,15 +381,15 @@ export default class ScriptTransformer {
}

private _transformAndBuildScript(
filename: Config.Path,
filename: Config.RPth,
options: Options | null,
instrument: boolean,
fileSource?: string,
): TransformResult {
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;
Expand All @@ -391,7 +399,7 @@ export default class ScriptTransformer {
const willTransform =
!isInternalModule &&
!isCoreModule &&
(this.shouldTransform(filename) || instrument);
(this.shouldTransform(filename.path) || instrument);

try {
if (willTransform) {
Expand All @@ -418,7 +426,7 @@ export default class ScriptTransformer {
}

transform(
filename: Config.Path,
filename: Config.RPth,
options: Options,
fileSource?: string,
): TransformResult {
Expand All @@ -428,7 +436,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) {
Expand Down Expand Up @@ -462,6 +470,7 @@ export default class ScriptTransformer {

if (willTransform) {
const {code: transformedJsonSource} = this.transformSource(
// @ts-ignore
filename,
fileSource,
false,
Expand Down Expand Up @@ -493,6 +502,7 @@ export default class ScriptTransformer {
(code, filename) => {
try {
transforming = true;
// @ts-ignore
return this.transformSource(filename, code, false).code || code;
} finally {
transforming = false;
Expand Down Expand Up @@ -689,8 +699,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' : '');
};

Expand Down
4 changes: 2 additions & 2 deletions packages/jest-types/src/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 11b581a

Please sign in to comment.