Skip to content

Commit

Permalink
move transform types to transform package and fix babel-jest export t…
Browse files Browse the repository at this point in the history
…ypes
  • Loading branch information
SimenB committed Feb 17, 2019
1 parent 1270969 commit 3f1dcd8
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 117 deletions.
3 changes: 2 additions & 1 deletion packages/babel-jest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@
"main": "build/index.js",
"types": "build/index.d.ts",
"dependencies": {
"@jest/transform": "^24.1.0",
"@jest/types": "^24.1.0",
"@types/babel__core": "^7.0.4",
"babel-plugin-istanbul": "^5.1.0",
"babel-preset-jest": "^24.1.0",
"chalk": "^2.4.2",
"slash": "^2.0.0"
},
"devDependencies": {
"@babel/core": "^7.1.0",
"@types/babel__core": "^7.0.4",
"@types/slash": "^2.0.0"
},
"peerDependencies": {
Expand Down
6 changes: 2 additions & 4 deletions packages/babel-jest/src/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import {Config, Transform} from '@jest/types';
import {Config} from '@jest/types';
import babelJest from '../index';

//Mock data for all the tests
Expand All @@ -30,14 +30,12 @@ test(`Returns source string with inline maps when no transformOptions is passed`
sourceString,
'dummy_path.js',
(mockConfig as unknown) as Config.ProjectConfig,
) as Transform.TransformedSource;
) as any;
expect(typeof result).toBe('object');
expect(result.code).toBeDefined();
expect(result.map).toBeDefined();
expect(result.code).toMatch('//# sourceMappingURL');
expect(result.code).toMatch('customMultiply');
// @ts-ignore: it's fine if we get wrong types, the tests will fail then
expect(result.map.sources).toEqual(['dummy_path.js']);
// @ts-ignore: it's fine if we get wrong types, the tests will fail then
expect(JSON.stringify(result.map.sourcesContent)).toMatch('customMultiply');
});
50 changes: 22 additions & 28 deletions packages/babel-jest/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
import crypto from 'crypto';
import fs from 'fs';
import path from 'path';
import {Config, Transform} from '@jest/types';
import {Transformer} from '@jest/transform';
import {Config} from '@jest/types';
import {
transformSync as babelTransform,
loadPartialConfig,
TransformOptions,
PartialConfig,
TransformOptions,
transformSync as babelTransform,
} from '@babel/core';
import chalk from 'chalk';
import slash from 'slash';
Expand All @@ -22,9 +23,15 @@ const THIS_FILE = fs.readFileSync(__filename);
const jestPresetPath = require.resolve('babel-preset-jest');
const babelIstanbulPlugin = require.resolve('babel-plugin-istanbul');

// Narrow down the types
interface BabelJestTransformer extends Transformer {
canInstrument: true;
createTransformer: (options?: TransformOptions) => BabelJestTransformer;
}

const createTransformer = (
options: TransformOptions = {},
): Transform.Transformer => {
): BabelJestTransformer => {
options = {
...options,
// @ts-ignore: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/33118
Expand Down Expand Up @@ -58,18 +65,15 @@ const createTransformer = (
return babelConfig;
}

return {
const transformer: BabelJestTransformer = {
canInstrument: true,
createTransformer,
getCacheKey(
fileData: string,
filename: Config.Path,
configString: string,
{
config,
instrument,
rootDir,
}: {config: Config.ProjectConfig} & Transform.CacheKeyOptions,
): string {
fileData,
filename,
configString,
{config, instrument, rootDir},
) {
const babelOptions = loadBabelConfig(config.cwd, filename);
const configPath = [
babelOptions.config || '',
Expand Down Expand Up @@ -97,12 +101,7 @@ const createTransformer = (
.update(process.env.BABEL_ENV || '')
.digest('hex');
},
process(
src: string,
filename: Config.Path,
config: Config.ProjectConfig,
transformOptions?: Transform.TransformOptions,
): string | Transform.TransformedSource {
process(src, filename, config, transformOptions) {
const babelOptions = {...loadBabelConfig(config.cwd, filename).options};

if (transformOptions && transformOptions.instrument) {
Expand Down Expand Up @@ -132,13 +131,8 @@ const createTransformer = (
return src;
},
};
};

const transformer = createTransformer();

// FIXME: This is not part of the exported TS types. When fixed, remember to
// move @types/babel__core to `dependencies` instead of `devDependencies`
// (one fix is to use ESM, maybe for Jest 25?)
transformer.createTransformer = createTransformer;
return transformer;
};

export = transformer;
export = createTransformer();
1 change: 1 addition & 0 deletions packages/babel-jest/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
},
// TODO: include `babel-preset-jest` if it's ever in TS even though we don't care about its types
"references": [
{"path": "../jest-transform"},
{"path": "../jest-types"}
]
}
1 change: 1 addition & 0 deletions packages/jest-transform/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"micromatch": "^3.1.10",
"realpath-native": "^1.1.0",
"slash": "^2.0.0",
"source-map": "^0.6.1",
"write-file-atomic": "2.4.1"
},
"devDependencies": {
Expand Down
25 changes: 15 additions & 10 deletions packages/jest-transform/src/ScriptTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import crypto from 'crypto';
import path from 'path';
import vm from 'vm';
import {Config, Transform} from '@jest/types';
import {Config} from '@jest/types';
import {createDirectory} from 'jest-util';
import fs from 'graceful-fs';
import {transformSync as babelTransform} from '@babel/core';
Expand All @@ -20,14 +20,19 @@ import stableStringify from 'fast-json-stable-stringify';
import slash from 'slash';
import writeFileAtomic from 'write-file-atomic';
import {sync as realpath} from 'realpath-native';
import {Options} from './types';
import {
Options,
Transformer,
TransformedSource,
TransformResult,
} from './types';
import shouldInstrument from './shouldInstrument';
import enhanceUnexpectedTokenMessage from './enhanceUnexpectedTokenMessage';

type ProjectCache = {
configString: string;
ignorePatternsRegExp: RegExp | null;
transformedFiles: Map<string, Transform.TransformResult | string>;
transformedFiles: Map<string, TransformResult | string>;
};

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

constructor(config: Config.ProjectConfig) {
this._config = config;
Expand Down Expand Up @@ -136,7 +141,7 @@ export default class ScriptTransformer {
}

private _getTransformer(filename: Config.Path) {
let transform: Transform.Transformer | null = null;
let transform: Transformer | null = null;
if (!this._config.transform || !this._config.transform.length) {
return null;
}
Expand All @@ -148,7 +153,7 @@ export default class ScriptTransformer {
return transformer;
}

transform = require(transformPath) as Transform.Transformer;
transform = require(transformPath) as Transformer;
if (typeof transform.createTransformer === 'function') {
transform = transform.createTransformer();
}
Expand Down Expand Up @@ -243,7 +248,7 @@ export default class ScriptTransformer {
};
}

let transformed: Transform.TransformedSource = {
let transformed: TransformedSource = {
code: content,
map: null,
};
Expand Down Expand Up @@ -305,7 +310,7 @@ export default class ScriptTransformer {
options: Options | null,
instrument: boolean,
fileSource?: string,
): Transform.TransformResult {
): TransformResult {
const isInternalModule = !!(options && options.isInternalModule);
const isCoreModule = !!(options && options.isCoreModule);
const content = stripShebang(
Expand Down Expand Up @@ -367,10 +372,10 @@ export default class ScriptTransformer {
filename: Config.Path,
options: Options,
fileSource?: string,
): Transform.TransformResult | string {
): TransformResult | string {
let scriptCacheKey = null;
let instrument = false;
let result: Transform.TransformResult | string | undefined = '';
let result: TransformResult | string | undefined = '';

if (!options.isCoreModule) {
instrument = shouldInstrument(filename, options, this._config);
Expand Down
1 change: 1 addition & 0 deletions packages/jest-transform/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@

export {default as ScriptTransformer} from './ScriptTransformer';
export {default as shouldInstrument} from './shouldInstrument';
export {Transformer} from './types';
51 changes: 51 additions & 0 deletions packages/jest-transform/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* LICENSE file in the root directory of this source tree.
*/

import {Script} from 'vm';
import {RawSourceMap} from 'source-map';
import {Config} from '@jest/types';

export type Options = Pick<
Expand All @@ -18,3 +20,52 @@ export type Options = Pick<
isCoreModule?: boolean;
isInternalModule?: boolean;
};

// https://stackoverflow.com/a/48216010/1850276
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;

// This is fixed in a newer version, but that depends on Node 8 which is a
// breaking change (engine warning when installing)
interface FixedRawSourceMap extends Omit<RawSourceMap, 'version'> {
version: number;
}

export type TransformedSource = {
code: string;
map?: FixedRawSourceMap | string | null;
};

export type TransformResult = {
script: Script;
mapCoverage: boolean;
sourceMapPath: string | null;
};

export type TransformOptions = {
instrument: boolean;
};

export type CacheKeyOptions = {
config: Config.ProjectConfig;
instrument: boolean;
rootDir: string;
};

export interface Transformer {
canInstrument?: boolean;
createTransformer?: (options?: any) => Transformer;

getCacheKey: (
fileData: string,
filePath: Config.Path,
configStr: string,
options: CacheKeyOptions,
) => string;

process: (
sourceText: string,
sourcePath: Config.Path,
config: Config.ProjectConfig,
options?: TransformOptions,
) => string | TransformedSource;
}
5 changes: 1 addition & 4 deletions packages/jest-types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,5 @@
},
"license": "MIT",
"main": "build/index.js",
"types": "build/index.d.ts",
"dependencies": {
"source-map": "^0.6.1"
}
"types": "build/index.d.ts"
}
59 changes: 0 additions & 59 deletions packages/jest-types/src/Transform.ts

This file was deleted.

12 changes: 1 addition & 11 deletions packages/jest-types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,7 @@ import * as Console from './Console';
import * as SourceMaps from './SourceMaps';
import * as TestResult from './TestResult';
import * as Mocks from './Mocks';
import * as Transform from './Transform';
import * as PrettyFormat from './PrettyFormat';
import * as Matchers from './Matchers';

export {
Config,
Console,
SourceMaps,
TestResult,
Mocks,
Transform,
PrettyFormat,
Matchers,
};
export {Config, Console, SourceMaps, TestResult, Mocks, PrettyFormat, Matchers};

0 comments on commit 3f1dcd8

Please sign in to comment.