Skip to content

Commit 6809772

Browse files
committed
move transform types to transform package and fix babel-jest export types
1 parent d1c361c commit 6809772

11 files changed

Lines changed: 96 additions & 117 deletions

File tree

packages/babel-jest/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,16 @@
1111
"main": "build/index.js",
1212
"types": "build/index.d.ts",
1313
"dependencies": {
14+
"@jest/transform": "^24.1.0",
1415
"@jest/types": "^24.1.0",
16+
"@types/babel__core": "^7.0.4",
1517
"babel-plugin-istanbul": "^5.1.0",
1618
"babel-preset-jest": "^24.1.0",
1719
"chalk": "^2.4.2",
1820
"slash": "^2.0.0"
1921
},
2022
"devDependencies": {
2123
"@babel/core": "^7.1.0",
22-
"@types/babel__core": "^7.0.4",
2324
"@types/slash": "^2.0.0"
2425
},
2526
"peerDependencies": {

packages/babel-jest/src/__tests__/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import {Config, Transform} from '@jest/types';
8+
import {Config} from '@jest/types';
99
import babelJest from '../index';
1010

1111
//Mock data for all the tests
@@ -30,14 +30,12 @@ test(`Returns source string with inline maps when no transformOptions is passed`
3030
sourceString,
3131
'dummy_path.js',
3232
(mockConfig as unknown) as Config.ProjectConfig,
33-
) as Transform.TransformedSource;
33+
) as any;
3434
expect(typeof result).toBe('object');
3535
expect(result.code).toBeDefined();
3636
expect(result.map).toBeDefined();
3737
expect(result.code).toMatch('//# sourceMappingURL');
3838
expect(result.code).toMatch('customMultiply');
39-
// @ts-ignore: it's fine if we get wrong types, the tests will fail then
4039
expect(result.map.sources).toEqual(['dummy_path.js']);
41-
// @ts-ignore: it's fine if we get wrong types, the tests will fail then
4240
expect(JSON.stringify(result.map.sourcesContent)).toMatch('customMultiply');
4341
});

packages/babel-jest/src/index.ts

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
import crypto from 'crypto';
99
import fs from 'fs';
1010
import path from 'path';
11-
import {Config, Transform} from '@jest/types';
11+
import {Transformer} from '@jest/transform';
12+
import {Config} from '@jest/types';
1213
import {
13-
transformSync as babelTransform,
1414
loadPartialConfig,
15-
TransformOptions,
1615
PartialConfig,
16+
TransformOptions,
17+
transformSync as babelTransform,
1718
} from '@babel/core';
1819
import chalk from 'chalk';
1920
import slash from 'slash';
@@ -22,9 +23,14 @@ const THIS_FILE = fs.readFileSync(__filename);
2223
const jestPresetPath = require.resolve('babel-preset-jest');
2324
const babelIstanbulPlugin = require.resolve('babel-plugin-istanbul');
2425

26+
// Make it non-optional
27+
type TransformerWithFactory = Transformer & {
28+
createTransformer: (options?: TransformOptions) => TransformerWithFactory;
29+
};
30+
2531
const createTransformer = (
2632
options: TransformOptions = {},
27-
): Transform.Transformer => {
33+
): TransformerWithFactory => {
2834
options = {
2935
...options,
3036
// @ts-ignore: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/33118
@@ -58,18 +64,15 @@ const createTransformer = (
5864
return babelConfig;
5965
}
6066

61-
return {
67+
const transformer: TransformerWithFactory = {
6268
canInstrument: true,
69+
createTransformer,
6370
getCacheKey(
64-
fileData: string,
65-
filename: Config.Path,
66-
configString: string,
67-
{
68-
config,
69-
instrument,
70-
rootDir,
71-
}: {config: Config.ProjectConfig} & Transform.CacheKeyOptions,
72-
): string {
71+
fileData,
72+
filename,
73+
configString,
74+
{config, instrument, rootDir},
75+
) {
7376
const babelOptions = loadBabelConfig(config.cwd, filename);
7477
const configPath = [
7578
babelOptions.config || '',
@@ -97,12 +100,7 @@ const createTransformer = (
97100
.update(process.env.BABEL_ENV || '')
98101
.digest('hex');
99102
},
100-
process(
101-
src: string,
102-
filename: Config.Path,
103-
config: Config.ProjectConfig,
104-
transformOptions?: Transform.TransformOptions,
105-
): string | Transform.TransformedSource {
103+
process(src, filename, config, transformOptions) {
106104
const babelOptions = {...loadBabelConfig(config.cwd, filename).options};
107105

108106
if (transformOptions && transformOptions.instrument) {
@@ -132,13 +130,8 @@ const createTransformer = (
132130
return src;
133131
},
134132
};
135-
};
136133

137-
const transformer = createTransformer();
138-
139-
// FIXME: This is not part of the exported TS types. When fixed, remember to
140-
// move @types/babel__core to `dependencies` instead of `devDependencies`
141-
// (one fix is to use ESM, maybe for Jest 25?)
142-
transformer.createTransformer = createTransformer;
134+
return transformer;
135+
};
143136

144-
export = transformer;
137+
export = createTransformer();

packages/babel-jest/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
},
77
// TODO: include `babel-preset-jest` if it's ever in TS even though we don't care about its types
88
"references": [
9+
{"path": "../jest-transform"},
910
{"path": "../jest-types"}
1011
]
1112
}

packages/jest-transform/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"micromatch": "^3.1.10",
2323
"realpath-native": "^1.1.0",
2424
"slash": "^2.0.0",
25+
"source-map": "^0.6.1",
2526
"write-file-atomic": "2.4.1"
2627
},
2728
"devDependencies": {

packages/jest-transform/src/ScriptTransformer.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import crypto from 'crypto';
99
import path from 'path';
1010
import vm from 'vm';
11-
import {Config, Transform} from '@jest/types';
11+
import {Config} from '@jest/types';
1212
import {createDirectory} from 'jest-util';
1313
import fs from 'graceful-fs';
1414
import {transformSync as babelTransform} from '@babel/core';
@@ -20,14 +20,19 @@ import stableStringify from 'fast-json-stable-stringify';
2020
import slash from 'slash';
2121
import writeFileAtomic from 'write-file-atomic';
2222
import {sync as realpath} from 'realpath-native';
23-
import {Options} from './types';
23+
import {
24+
Options,
25+
Transformer,
26+
TransformedSource,
27+
TransformResult,
28+
} from './types';
2429
import shouldInstrument from './shouldInstrument';
2530
import enhanceUnexpectedTokenMessage from './enhanceUnexpectedTokenMessage';
2631

2732
type ProjectCache = {
2833
configString: string;
2934
ignorePatternsRegExp: RegExp | null;
30-
transformedFiles: Map<string, Transform.TransformResult | string>;
35+
transformedFiles: Map<string, TransformResult | string>;
3136
};
3237

3338
// Use `require` to avoid TS rootDir
@@ -48,7 +53,7 @@ export default class ScriptTransformer {
4853
static EVAL_RESULT_VARIABLE: string;
4954
private _cache: ProjectCache;
5055
private _config: Config.ProjectConfig;
51-
private _transformCache: Map<Config.Path, Transform.Transformer>;
56+
private _transformCache: Map<Config.Path, Transformer>;
5257

5358
constructor(config: Config.ProjectConfig) {
5459
this._config = config;
@@ -136,7 +141,7 @@ export default class ScriptTransformer {
136141
}
137142

138143
private _getTransformer(filename: Config.Path) {
139-
let transform: Transform.Transformer | null = null;
144+
let transform: Transformer | null = null;
140145
if (!this._config.transform || !this._config.transform.length) {
141146
return null;
142147
}
@@ -148,7 +153,7 @@ export default class ScriptTransformer {
148153
return transformer;
149154
}
150155

151-
transform = require(transformPath) as Transform.Transformer;
156+
transform = require(transformPath) as Transformer;
152157
if (typeof transform.createTransformer === 'function') {
153158
transform = transform.createTransformer();
154159
}
@@ -243,7 +248,7 @@ export default class ScriptTransformer {
243248
};
244249
}
245250

246-
let transformed: Transform.TransformedSource = {
251+
let transformed: TransformedSource = {
247252
code: content,
248253
map: null,
249254
};
@@ -305,7 +310,7 @@ export default class ScriptTransformer {
305310
options: Options | null,
306311
instrument: boolean,
307312
fileSource?: string,
308-
): Transform.TransformResult {
313+
): TransformResult {
309314
const isInternalModule = !!(options && options.isInternalModule);
310315
const isCoreModule = !!(options && options.isCoreModule);
311316
const content = stripShebang(
@@ -367,10 +372,10 @@ export default class ScriptTransformer {
367372
filename: Config.Path,
368373
options: Options,
369374
fileSource?: string,
370-
): Transform.TransformResult | string {
375+
): TransformResult | string {
371376
let scriptCacheKey = null;
372377
let instrument = false;
373-
let result: Transform.TransformResult | string | undefined = '';
378+
let result: TransformResult | string | undefined = '';
374379

375380
if (!options.isCoreModule) {
376381
instrument = shouldInstrument(filename, options, this._config);

packages/jest-transform/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77

88
export {default as ScriptTransformer} from './ScriptTransformer';
99
export {default as shouldInstrument} from './shouldInstrument';
10+
export {Transformer} from './types';

packages/jest-transform/src/types.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
import {Script} from 'vm';
9+
import {RawSourceMap} from 'source-map';
810
import {Config} from '@jest/types';
911

1012
export type Options = Pick<
@@ -18,3 +20,52 @@ export type Options = Pick<
1820
isCoreModule?: boolean;
1921
isInternalModule?: boolean;
2022
};
23+
24+
// https://stackoverflow.com/a/48216010/1850276
25+
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
26+
27+
// This is fixed in a newer version, but that depends on Node 8 which is a
28+
// breaking change (engine warning when installing)
29+
interface FixedRawSourceMap extends Omit<RawSourceMap, 'version'> {
30+
version: number;
31+
}
32+
33+
export type TransformedSource = {
34+
code: string;
35+
map?: FixedRawSourceMap | string | null;
36+
};
37+
38+
export type TransformResult = {
39+
script: Script;
40+
mapCoverage: boolean;
41+
sourceMapPath: string | null;
42+
};
43+
44+
export type TransformOptions = {
45+
instrument: boolean;
46+
};
47+
48+
export type CacheKeyOptions = {
49+
config: Config.ProjectConfig;
50+
instrument: boolean;
51+
rootDir: string;
52+
};
53+
54+
export type Transformer = {
55+
canInstrument?: boolean;
56+
createTransformer?: (options?: any) => Transformer;
57+
58+
getCacheKey: (
59+
fileData: string,
60+
filePath: Config.Path,
61+
configStr: string,
62+
options: CacheKeyOptions,
63+
) => string;
64+
65+
process: (
66+
sourceText: string,
67+
sourcePath: Config.Path,
68+
config: Config.ProjectConfig,
69+
options?: TransformOptions,
70+
) => string | TransformedSource;
71+
};

packages/jest-types/package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,5 @@
1111
},
1212
"license": "MIT",
1313
"main": "build/index.js",
14-
"types": "build/index.d.ts",
15-
"dependencies": {
16-
"source-map": "^0.6.1"
17-
}
14+
"types": "build/index.d.ts"
1815
}

packages/jest-types/src/Transform.ts

Lines changed: 0 additions & 59 deletions
This file was deleted.

0 commit comments

Comments
 (0)