diff --git a/CHANGELOG.md b/CHANGELOG.md index 361a0877ccf7..d68df0efdee3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -62,6 +62,7 @@ - `[jest-runner]`: Migrate to TypeScript ([#7968](https://github.com/facebook/jest/pull/7968)) - `[jest-runtime]`: Migrate to TypeScript ([#7964](https://github.com/facebook/jest/pull/7964), [#7988](https://github.com/facebook/jest/pull/7988)) - `[@jest/fake-timers]`: Extract FakeTimers class from `jest-util` into a new separate package ([#7987](https://github.com/facebook/jest/pull/7987)) +- `[jest-repl]`: Migrate to TypeScript ([#8000](https://github.com/facebook/jest/pull/8000)) ### Performance diff --git a/packages/jest-repl/package.json b/packages/jest-repl/package.json index 8c9abb3ec472..f4bb47655945 100644 --- a/packages/jest-repl/package.json +++ b/packages/jest-repl/package.json @@ -8,7 +8,10 @@ }, "license": "MIT", "main": "build/index.js", + "types": "build/index.d.ts", "dependencies": { + "@jest/transform": "^24.1.0", + "@jest/types": "^24.1.0", "jest-config": "^24.1.0", "jest-runtime": "^24.1.0", "jest-validate": "^24.0.0", diff --git a/packages/jest-repl/src/cli/args.js b/packages/jest-repl/src/cli/args.ts similarity index 88% rename from packages/jest-repl/src/cli/args.js rename to packages/jest-repl/src/cli/args.ts index c5d29b013765..b0c72fd6e3c4 100644 --- a/packages/jest-repl/src/cli/args.js +++ b/packages/jest-repl/src/cli/args.ts @@ -4,15 +4,13 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @flow */ import Runtime from 'jest-runtime'; export const usage = 'Usage: $0 [--config=]'; -export const options = { - ...Runtime.getCLIOptions(), +export const options = Object.assign({}, Runtime.getCLIOptions(), { replname: { alias: 'r', description: @@ -20,4 +18,4 @@ export const options = { 'transformed. For example, "repl.ts" if using a TypeScript transformer.', type: 'string', }, -}; +}); diff --git a/packages/jest-repl/src/cli/index.js b/packages/jest-repl/src/cli/index.ts similarity index 74% rename from packages/jest-repl/src/cli/index.js rename to packages/jest-repl/src/cli/index.ts index b9a0abaf9c3b..5bbf3f975c20 100644 --- a/packages/jest-repl/src/cli/index.js +++ b/packages/jest-repl/src/cli/index.ts @@ -5,26 +5,26 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @flow */ -import path from 'path'; - import Runtime from 'jest-runtime'; import yargs from 'yargs'; +// @ts-ignore: Wait for jest-validate to get migrated import {validateCLIOptions} from 'jest-validate'; import {deprecationEntries} from 'jest-config'; -import {version as VERSION} from '../../package.json'; import * as args from './args'; -const REPL_SCRIPT = path.resolve(__dirname, './repl.js'); +const {version: VERSION} = require('../../package.json'); + +const REPL_SCRIPT = require.resolve('./repl.js'); -module.exports = function() { +export = function() { const argv = yargs.usage(args.usage).options(args.options).argv; validateCLIOptions(argv, {...args.options, deprecationEntries}); argv._ = [REPL_SCRIPT]; + // @ts-ignore: not the same arguments Runtime.runCLI(argv, [`Jest REPL v${VERSION}`]); }; diff --git a/packages/jest-repl/src/cli/repl.js b/packages/jest-repl/src/cli/repl.ts similarity index 70% rename from packages/jest-repl/src/cli/repl.js rename to packages/jest-repl/src/cli/repl.ts index 6a0b8fa5ccea..66f5725ecbed 100644 --- a/packages/jest-repl/src/cli/repl.js +++ b/packages/jest-repl/src/cli/repl.ts @@ -5,30 +5,37 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @flow */ -import type {GlobalConfig, ProjectConfig} from 'types/Config'; - -declare var jestGlobalConfig: GlobalConfig; -declare var jestProjectConfig: ProjectConfig; -declare var jest: Object; +declare const jestGlobalConfig: Config.GlobalConfig; +declare const jestProjectConfig: Config.ProjectConfig; import path from 'path'; import repl from 'repl'; import vm from 'vm'; +import {Transformer} from '@jest/transform'; +import {Config} from '@jest/types'; -let transformer; +let transformer: Transformer; -const evalCommand = (cmd, context, filename, callback, config) => { +const evalCommand: repl.REPLEval = ( + cmd: string, + _context: any, + _filename: string, + callback: (e: Error | null, result?: any) => void, +) => { let result; try { if (transformer) { - cmd = transformer.process( + const transformResult = transformer.process( cmd, jestGlobalConfig.replname || 'jest.js', jestProjectConfig, ); + cmd = + typeof transformResult === 'string' + ? transformResult + : transformResult.code; } result = vm.runInThisContext(cmd); } catch (e) { @@ -37,7 +44,7 @@ const evalCommand = (cmd, context, filename, callback, config) => { return callback(null, result); }; -const isRecoverableError = error => { +const isRecoverableError = (error: Error) => { if (error && error.name === 'SyntaxError') { return [ 'Unterminated template', @@ -59,7 +66,6 @@ if (jestProjectConfig.transform) { } } if (transformerPath) { - /* $FlowFixMe */ transformer = require(transformerPath); if (typeof transformer.process !== 'function') { throw new TypeError( @@ -69,17 +75,15 @@ if (jestProjectConfig.transform) { } } -const replInstance = repl.start({ +const replInstance: repl.REPLServer = repl.start({ eval: evalCommand, prompt: '\u203A ', useGlobal: true, }); -// $FlowFixMe: https://github.com/facebook/flow/pull/4713 -replInstance.context.require = moduleName => { +replInstance.context.require = (moduleName: string) => { if (/(\/|\\|\.)/.test(moduleName)) { moduleName = path.resolve(process.cwd(), moduleName); } - /* $FlowFixMe */ return require(moduleName); }; diff --git a/packages/jest-repl/tsconfig.json b/packages/jest-repl/tsconfig.json new file mode 100644 index 000000000000..98a536c2c508 --- /dev/null +++ b/packages/jest-repl/tsconfig.json @@ -0,0 +1,15 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "rootDir": "src", + "outDir": "build" + }, + "references": [ + {"path": "../jest-config"}, + {"path": "../jest-runtime"}, + {"path": "../jest-transform"}, + {"path": "../jest-types"} + // TODO: Wait for this to get migrated + // {"path": "../jest-validate"} + ] +}