Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
ychi committed May 3, 2020
1 parent 0e2b7c4 commit f85e570
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 69 deletions.
135 changes: 67 additions & 68 deletions packages/jest-transform/src/ScriptTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,23 +126,25 @@ export default class ScriptTransformer {

if (transformer && typeof transformer.getCacheKey === 'function') {
transformerCacheKey = transformer.getCacheKey(
fileData,
filename,
configString,
fileData,
filename,
configString,
{
config: this._config,
instrument,
rootDir: this._config.rootDir,
supportsDynamicImport,
supportsStaticESM,
});
}
return this._buildCacheKeyFromFileInfo(
fileData,
filename,
instrument,
configString,
transformerCacheKey);
},
);
}
return this._buildCacheKeyFromFileInfo(
fileData,
filename,
instrument,
configString,
transformerCacheKey,
);
}

private async _getCacheKeyAsync(
Expand All @@ -156,22 +158,26 @@ export default class ScriptTransformer {
const transformer = await this._getTransformerAsync(filename);
let transformerCacheKey = undefined;

if (transformer && typeof transformer.getCacheKeyAsync === 'function') {
transformerCacheKey =
await transformer.getCacheKeyAsync(fileData, filename, configString, {
config: this._config,
instrument,
rootDir: this._config.rootDir,
supportsDynamicImport,
supportsStaticESM,
});
if (transformer && typeof transformer.getCacheKeyAsync === 'function') {
transformerCacheKey = await transformer.getCacheKeyAsync(
fileData,
filename,
configString,
{
config: this._config,
instrument,
rootDir: this._config.rootDir,
supportsDynamicImport,
supportsStaticESM,
},
);
}
return this._buildCacheKeyFromFileInfo(
fileData,
filename,
instrument,
configString,
transformerCacheKey
transformerCacheKey,
);
}

Expand Down Expand Up @@ -205,7 +211,6 @@ export default class ScriptTransformer {
supportsDynamicImport: boolean,
supportsStaticESM: boolean,
): Config.Path {

const cacheKey = this._getCacheKey(
content,
filename,
Expand All @@ -214,10 +219,7 @@ export default class ScriptTransformer {
supportsStaticESM,
);

return this._createFolderFromCacheKey(
filename,
cacheKey
);
return this._createFolderFromCacheKey(filename, cacheKey);
}

private async _getFileCachePathAsync(
Expand All @@ -227,7 +229,6 @@ export default class ScriptTransformer {
supportsDynamicImport: boolean,
supportsStaticESM: boolean,
): Promise<Config.Path> {

const cacheKey = await this._getCacheKeyAsync(
content,
filename,
Expand All @@ -236,9 +237,7 @@ export default class ScriptTransformer {
supportsStaticESM,
);

return this._createFolderFromCacheKey(
filename,
cacheKey);
return this._createFolderFromCacheKey(filename, cacheKey);
}

private _getTransformPath(filename: Config.Path) {
Expand Down Expand Up @@ -282,15 +281,16 @@ export default class ScriptTransformer {
transform = transform.createTransformer(transformerConfig);
}
if (
typeof transform.process !== 'function' &&
typeof transform.processAsync !== 'function') {
typeof transform.process !== 'function' &&
typeof transform.processAsync !== 'function'
) {
throw new TypeError(
'Jest: a transform must export a `process` or `processAsync` function.',
);
}
this._transformCache.set(transformPath, transform);
}
return transform;
return transform;
}

private _getTransformer(filename: Config.Path) {
Expand Down Expand Up @@ -386,7 +386,7 @@ export default class ScriptTransformer {
supportsStaticESM: boolean,
processed: TransformedSource | null,
sourceMapPath: Config.Path | null,
):TransformResult {
): TransformResult {
let transformed: TransformedSource = {
code: content,
map: null,
Expand All @@ -397,7 +397,7 @@ export default class ScriptTransformer {
const transformWillInstrument =
shouldCallTransform && transform && transform.canInstrument;

if (transform && shouldCallTransform) {
if (transform && shouldCallTransform) {
if (typeof processed === 'string') {
transformed.code = processed;
} else if (processed != null && typeof processed.code === 'string') {
Expand Down Expand Up @@ -462,9 +462,9 @@ export default class ScriptTransformer {
if (map) {
const sourceMapContent =
typeof map === 'string' ? map : JSON.stringify(map);
invariant(sourceMapPath, "We should always have default sourceMapPath")

invariant(sourceMapPath, 'We should always have default sourceMapPath');

writeCacheFile(sourceMapPath, sourceMapContent);
} else {
sourceMapPath = null;
Expand All @@ -488,22 +488,20 @@ export default class ScriptTransformer {
supportsStaticESM = false,
): TransformResult {
const filename = tryRealpath(filepath);
const transform = this._getTransformer(filename);
const cacheFilePath = this._getFileCachePath(
const transform = this._getTransformer(filename);
const cacheFilePath = this._getFileCachePath(
filename,
content,
instrument,
supportsDynamicImport,
supportsStaticESM,
);
let sourceMapPath: Config.Path | null = cacheFilePath + '.map';
const sourceMapPath: Config.Path | null = cacheFilePath + '.map';
// Ignore cache if `config.cache` is set (--no-cache)
let code = this._config.cache ? readCodeCacheFile(cacheFilePath) : null;
const code = this._config.cache ? readCodeCacheFile(cacheFilePath) : null;

const shouldCallTransform = transform && this.shouldTransform(filename);



if (code) {
// This is broken: we return the code, and a path for the source map
// directly from the cache. But, nothing ensures the source map actually
Expand All @@ -521,7 +519,7 @@ export default class ScriptTransformer {
if (transform && shouldCallTransform) {
if (typeof transform.process !== 'function') {
throw new TypeError(
"Jest: a synchronous transform mus export a `process` function."
'Jest: a synchronous transform mus export a `process` function.',
);
}

Expand All @@ -531,8 +529,10 @@ export default class ScriptTransformer {
supportsStaticESM,
});

if (processed == null ||
(typeof processed !== 'string' && typeof processed.code !== 'string')) {
if (
processed == null ||
(typeof processed !== 'string' && typeof processed.code !== 'string')
) {
throw new TypeError(
"Jest: a transform's `process` function must return a string, " +
'or an object with `code` key containing this string.',
Expand All @@ -550,7 +550,7 @@ export default class ScriptTransformer {
supportsDynamicImport,
supportsStaticESM,
processed,
sourceMapPath
sourceMapPath,
);
}

Expand All @@ -571,9 +571,9 @@ export default class ScriptTransformer {
supportsDynamicImport,
supportsStaticESM,
);
let sourceMapPath: Config.Path | null = cacheFilePath + '.map';
const sourceMapPath: Config.Path | null = cacheFilePath + '.map';
// Ignore cache if `config.cache` is set (--no-cache)
let code = this._config.cache ? readCodeCacheFile(cacheFilePath) : null;
const code = this._config.cache ? readCodeCacheFile(cacheFilePath) : null;

const shouldCallTransform = transform && this.shouldTransform(filename);

Expand All @@ -592,24 +592,25 @@ export default class ScriptTransformer {
let processed: TransformedSource | null = null;

if (transform && shouldCallTransform) {
processed = transform.processAsync ? await transform.processAsync(
content, filename, this._config, {
instrument,
supportsDynamicImport,
supportsStaticESM,
}
) :
transform.process(content, filename, this._config, {
instrument,
supportsDynamicImport,
supportsStaticESM,
});
processed = transform.processAsync
? await transform.processAsync(content, filename, this._config, {
instrument,
supportsDynamicImport,
supportsStaticESM,
})
: transform.process(content, filename, this._config, {
instrument,
supportsDynamicImport,
supportsStaticESM,
});

if (processed == null ||
(typeof processed !== 'string' && typeof processed.code !== 'string')) {
if (
processed == null ||
(typeof processed !== 'string' && typeof processed.code !== 'string')
) {
throw new TypeError(
"Jest: a transform's `process` function must return a string, " +
"or an object with `code` key containing this string. " +
'or an object with `code` key containing this string. ' +
"It's `processAsync` function must return that in a promise.",
);
}
Expand All @@ -625,9 +626,8 @@ export default class ScriptTransformer {
supportsDynamicImport,
supportsStaticESM,
processed,
sourceMapPath
sourceMapPath,
);

}

private async _transformAndBuildScriptAsync(
Expand Down Expand Up @@ -726,7 +726,6 @@ export default class ScriptTransformer {
}
}


async transformAsync(
filename: Config.Path,
options: Options,
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-transform/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,4 @@ interface AsyncTransformer {
) => Promise<TransformedSource>;
}

export type Transformer = SyncTransFormer | AsyncTransformer;
export type Transformer = SyncTransFormer | AsyncTransformer;

0 comments on commit f85e570

Please sign in to comment.