From 5fff2c0413a7b6bc21e9fc47399eab02dc0099bc Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 3 Nov 2020 01:58:35 +0100 Subject: [PATCH] chore: transformers must return objects, not strings --- CHANGELOG.md | 1 + packages/babel-jest/src/index.ts | 2 +- packages/jest-repl/src/cli/repl.ts | 5 +--- .../jest-transform/src/ScriptTransformer.ts | 23 ++++++++----------- packages/jest-transform/src/types.ts | 8 +++---- 5 files changed, 16 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aabf813304c5..cced9453bb48 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ ### Chore & Maintenance - `[jest-transform]` [**BREAKING**] Refactor API to pass an options bag around rather than multiple boolean options ([#10753](https://github.com/facebook/jest/pull/10753)) +- `[jest-transform]` [**BREAKING**] All transformers must now return `{code: string, map?: SourceMap | string | null}`. Returning a string is no longer supported ([#10773](https://github.com/facebook/jest/pull/10773)) ### Performance diff --git a/packages/babel-jest/src/index.ts b/packages/babel-jest/src/index.ts index 07181095ccc7..91c55dd0965b 100644 --- a/packages/babel-jest/src/index.ts +++ b/packages/babel-jest/src/index.ts @@ -164,7 +164,7 @@ const createTransformer = ( } } - return src; + return {code: src}; }, }; }; diff --git a/packages/jest-repl/src/cli/repl.ts b/packages/jest-repl/src/cli/repl.ts index 831b2323489a..280ae085159a 100644 --- a/packages/jest-repl/src/cli/repl.ts +++ b/packages/jest-repl/src/cli/repl.ts @@ -30,10 +30,7 @@ const evalCommand: repl.REPLEval = ( jestGlobalConfig.replname || 'jest.js', jestProjectConfig, ); - cmd = - typeof transformResult === 'string' - ? transformResult - : transformResult.code; + cmd = transformResult.code; } result = runInThisContext(cmd); } catch (e) { diff --git a/packages/jest-transform/src/ScriptTransformer.ts b/packages/jest-transform/src/ScriptTransformer.ts index 2af7af8c27e9..1b030e5d8563 100644 --- a/packages/jest-transform/src/ScriptTransformer.ts +++ b/packages/jest-transform/src/ScriptTransformer.ts @@ -208,10 +208,7 @@ export default class ScriptTransformer { canMapToInput: boolean, options: TransformOptions, ): TransformedSource { - const inputCode = typeof input === 'string' ? input : input.code; - const inputMap = typeof input === 'string' ? null : input.map; - - const result = babelTransform(inputCode, { + const result = babelTransform(input.code, { auxiliaryCommentBefore: ' istanbul ignore next ', babelrc: false, caller: { @@ -232,7 +229,7 @@ export default class ScriptTransformer { cwd: this._config.rootDir, exclude: [], extension: false, - inputSourceMap: inputMap, + inputSourceMap: input.map, useInlineSourceMaps: false, }, ], @@ -240,7 +237,7 @@ export default class ScriptTransformer { sourceMaps: canMapToInput ? 'both' : false, }); - if (result && result.code) { + if (result?.code) { return result as TransformResult; } @@ -297,14 +294,13 @@ export default class ScriptTransformer { options, ); - if (typeof processed === 'string') { - transformed.code = processed; - } else if (processed != null && typeof processed.code === 'string') { + if (processed != null && typeof processed.code === 'string') { transformed = processed; } else { throw new TypeError( - "Jest: a transform's `process` function must return a string, " + - 'or an object with `code` key containing this string.', + "Jest: a transform's `process` function must return an object with " + + '`code` key containing a string and an optional `map` key with ' + + 'the source map.', ); } } @@ -349,9 +345,8 @@ export default class ScriptTransformer { options, ); - code = - typeof instrumented === 'string' ? instrumented : instrumented.code; - map = typeof instrumented === 'string' ? null : instrumented.map; + code = instrumented.code; + map = instrumented.map; } else { code = transformed.code; } diff --git a/packages/jest-transform/src/types.ts b/packages/jest-transform/src/types.ts index 979c16c8ad42..521e66ef0bca 100644 --- a/packages/jest-transform/src/types.ts +++ b/packages/jest-transform/src/types.ts @@ -31,10 +31,10 @@ interface FixedRawSourceMap extends Omit { version: number; } -// TODO: For Jest 26 normalize this (always structured data, never a string) -export type TransformedSource = - | {code: string; map?: FixedRawSourceMap | string | null} - | string; +export type TransformedSource = { + code: string; + map?: FixedRawSourceMap | string | null; +}; export type TransformResult = TransformTypes.TransformResult;