Skip to content

Commit

Permalink
chore: transformers must return objects, not strings
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Nov 3, 2020
1 parent e0304f9 commit 5fff2c0
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion packages/babel-jest/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ const createTransformer = (
}
}

return src;
return {code: src};
},
};
};
Expand Down
5 changes: 1 addition & 4 deletions packages/jest-repl/src/cli/repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
23 changes: 9 additions & 14 deletions packages/jest-transform/src/ScriptTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -232,15 +229,15 @@ export default class ScriptTransformer {
cwd: this._config.rootDir,
exclude: [],
extension: false,
inputSourceMap: inputMap,
inputSourceMap: input.map,
useInlineSourceMaps: false,
},
],
],
sourceMaps: canMapToInput ? 'both' : false,
});

if (result && result.code) {
if (result?.code) {
return result as TransformResult;
}

Expand Down Expand Up @@ -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.',
);
}
}
Expand Down Expand Up @@ -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;
}
Expand Down
8 changes: 4 additions & 4 deletions packages/jest-transform/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ interface FixedRawSourceMap extends Omit<RawSourceMap, 'version'> {
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;

Expand Down

0 comments on commit 5fff2c0

Please sign in to comment.