Skip to content

Commit

Permalink
address feedbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
ychi committed Mar 7, 2021
1 parent a8d013a commit 141b722
Showing 1 changed file with 31 additions and 29 deletions.
60 changes: 31 additions & 29 deletions packages/jest-transform/src/ScriptTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ import {
} from 'jest-util';
import handlePotentialSyntaxError from './enhanceUnexpectedTokenMessage';
import shouldInstrument from './shouldInstrument';
// eslint somehow complains "There should be no empty line between import groups"
// some what similar to this: https://github.com/benmosher/eslint-plugin-import/issues/1853
//eslint-disable-next-line import/order
import type {
AsyncTransformer,
Options,
Expand All @@ -39,7 +36,6 @@ import type {
TransformedSource,
Transformer,
} from './types';

// Use `require` to avoid TS rootDir
const {version: VERSION} = require('../package.json');

Expand Down Expand Up @@ -77,6 +73,7 @@ export default class ScriptTransformer {
Config.Path,
{transformer: Transformer; transformerConfig: unknown}
>;
private readonly _transformCacheMutex: Map<Config.Path, Promise<void>>;
private readonly _transformConfigCache: Map<Config.Path, unknown>;

constructor(
Expand All @@ -86,6 +83,7 @@ export default class ScriptTransformer {
this._config = config;
this._cacheFS = cacheFS;
this._transformCache = new Map();
this._transformCacheMutex = new Map();
this._transformConfigCache = new Map();

const configString = stableStringify(this._config);
Expand Down Expand Up @@ -138,7 +136,7 @@ export default class ScriptTransformer {
this._getTransformer(filename) || {};
let transformerCacheKey = undefined;

if (transformer && typeof transformer.getCacheKey === 'function') {
if (typeof transformer?.getCacheKey === 'function') {
transformerCacheKey = transformer.getCacheKey(fileData, filename, {
...options,
cacheFS: this._cacheFS,
Expand Down Expand Up @@ -166,26 +164,19 @@ export default class ScriptTransformer {
(await this._getTransformerAsync(filename)) || {};
let transformerCacheKey = undefined;

if (transformer && typeof transformer.getCacheKeyAsync === 'function') {
transformerCacheKey = await transformer.getCacheKeyAsync(
fileData,
filename,
{
if (transformer) {
const getCacheKey =
transformer.getCacheKeyAsync || transformer.getCacheKey;

if (typeof getCacheKey === 'function') {
transformerCacheKey = await getCacheKey(fileData, filename, {
...options,
cacheFS: this._cacheFS,
config: this._config,
configString,
transformerConfig,
},
);
} else if (transformer && typeof transformer.getCacheKey === 'function') {
transformerCacheKey = transformer.getCacheKey(fileData, filename, {
...options,
cacheFS: this._cacheFS,
config: this._config,
configString,
transformerConfig,
});
});
}
}

return this._buildCacheKeyFromFileInfo(
Expand Down Expand Up @@ -259,7 +250,7 @@ export default class ScriptTransformer {
}

private async _getTransformerAsync(filename: Config.Path) {
if (!this._config.transform || !this._config.transform.length) {
if (this._config.transform.length === 0) {
return null;
}

Expand All @@ -269,12 +260,24 @@ export default class ScriptTransformer {
return null;
}

if (this._transformCacheMutex.has(transformPath)) {
await this._transformCacheMutex.get(transformPath);
}

const cached = this._transformCache.get(transformPath);
if (cached) {
return cached;
}

let transformer: AsyncTransformer = await import(transformPath);
let importResolve: () => void = () => {};
this._transformCacheMutex.set(
transformPath,
new Promise<void>(resolve => {
importResolve = resolve;
}),
);

let transformer: AsyncTransformer = (await import(transformPath)).default;

if (!transformer) {
throw new TypeError('Jest: a transform must export something.');
Expand All @@ -293,12 +296,13 @@ export default class ScriptTransformer {
}
const res = {transformer, transformerConfig};
this._transformCache.set(transformPath, res);
importResolve();

return res;
}

private _getTransformer(filename: Config.Path) {
if (!this._config.transform || !this._config.transform.length) {
if (this._config.transform.length === 0) {
return null;
}

Expand Down Expand Up @@ -409,7 +413,7 @@ export default class ScriptTransformer {
throw new TypeError(
"Jest: a transform's `process` function must return a string, " +
'or an object with `code` key containing this string. ' +
"It's `processAsync` function must return that in a promise.",
"It's `processAsync` function must return a Promise resolving to it.",
);
}
}
Expand Down Expand Up @@ -496,7 +500,7 @@ export default class ScriptTransformer {
const {transformer, transformerConfig = {}} =
this._getTransformer(filename) || {};
const cacheFilePath = this._getFileCachePath(filename, content, options);
const sourceMapPath: Config.Path | null = cacheFilePath + '.map';
const sourceMapPath: Config.Path = cacheFilePath + '.map';
// Ignore cache if `config.cache` is set (--no-cache)
const code = this._config.cache ? readCodeCacheFile(cacheFilePath) : null;

Expand Down Expand Up @@ -557,7 +561,7 @@ export default class ScriptTransformer {
content,
options,
);
const sourceMapPath: Config.Path | null = cacheFilePath + '.map';
const sourceMapPath: Config.Path = cacheFilePath + '.map';
// Ignore cache if `config.cache` is set (--no-cache)
const code = this._config.cache ? readCodeCacheFile(cacheFilePath) : null;

Expand Down Expand Up @@ -860,9 +864,7 @@ export default class ScriptTransformer {
const ignoreRegexp = this._cache.ignorePatternsRegExp;
const isIgnored = ignoreRegexp ? ignoreRegexp.test(filename) : false;

return (
!!this._config.transform && !!this._config.transform.length && !isIgnored
);
return this._config.transform.length !== 0 && !isIgnored;
}
}

Expand Down

0 comments on commit 141b722

Please sign in to comment.