From f85e570e1ed120e1c4fdb71ebf9332bc65bf878e Mon Sep 17 00:00:00 2001 From: Yen-Chi Chen Date: Sun, 3 May 2020 11:57:36 +0200 Subject: [PATCH] lint --- .../jest-transform/src/ScriptTransformer.ts | 135 +++++++++--------- packages/jest-transform/src/types.ts | 2 +- 2 files changed, 68 insertions(+), 69 deletions(-) diff --git a/packages/jest-transform/src/ScriptTransformer.ts b/packages/jest-transform/src/ScriptTransformer.ts index 57f573d9a601..ef298011f276 100644 --- a/packages/jest-transform/src/ScriptTransformer.ts +++ b/packages/jest-transform/src/ScriptTransformer.ts @@ -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( @@ -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, ); } @@ -205,7 +211,6 @@ export default class ScriptTransformer { supportsDynamicImport: boolean, supportsStaticESM: boolean, ): Config.Path { - const cacheKey = this._getCacheKey( content, filename, @@ -214,10 +219,7 @@ export default class ScriptTransformer { supportsStaticESM, ); - return this._createFolderFromCacheKey( - filename, - cacheKey - ); + return this._createFolderFromCacheKey(filename, cacheKey); } private async _getFileCachePathAsync( @@ -227,7 +229,6 @@ export default class ScriptTransformer { supportsDynamicImport: boolean, supportsStaticESM: boolean, ): Promise { - const cacheKey = await this._getCacheKeyAsync( content, filename, @@ -236,9 +237,7 @@ export default class ScriptTransformer { supportsStaticESM, ); - return this._createFolderFromCacheKey( - filename, - cacheKey); + return this._createFolderFromCacheKey(filename, cacheKey); } private _getTransformPath(filename: Config.Path) { @@ -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) { @@ -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, @@ -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') { @@ -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; @@ -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 @@ -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.', ); } @@ -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.', @@ -550,7 +550,7 @@ export default class ScriptTransformer { supportsDynamicImport, supportsStaticESM, processed, - sourceMapPath + sourceMapPath, ); } @@ -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); @@ -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.", ); } @@ -625,9 +626,8 @@ export default class ScriptTransformer { supportsDynamicImport, supportsStaticESM, processed, - sourceMapPath + sourceMapPath, ); - } private async _transformAndBuildScriptAsync( @@ -726,7 +726,6 @@ export default class ScriptTransformer { } } - async transformAsync( filename: Config.Path, options: Options, diff --git a/packages/jest-transform/src/types.ts b/packages/jest-transform/src/types.ts index 434d2d0c9d63..01f6af70f475 100644 --- a/packages/jest-transform/src/types.ts +++ b/packages/jest-transform/src/types.ts @@ -118,4 +118,4 @@ interface AsyncTransformer { ) => Promise; } -export type Transformer = SyncTransFormer | AsyncTransformer; \ No newline at end of file +export type Transformer = SyncTransFormer | AsyncTransformer;