From 55e7ed856f8b6483d8c6b10a86bf42bdd14aa4e7 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Tue, 26 Feb 2019 13:13:39 +0100 Subject: [PATCH 1/2] chore: migrate jest-validate to TypeScript --- CHANGELOG.md | 1 + .../src/ReporterValidationErrors.ts | 5 +-- packages/jest-config/src/ValidConfig.ts | 2 +- packages/jest-config/src/normalize.ts | 1 - packages/jest-config/src/utils.ts | 1 - packages/jest-config/tsconfig.json | 5 ++- packages/jest-repl/src/cli/index.ts | 2 +- packages/jest-repl/tsconfig.json | 5 +-- packages/jest-runtime/src/cli/index.ts | 2 +- packages/jest-runtime/tsconfig.json | 4 +- packages/jest-validate/package.json | 2 + .../src/__tests__/validateCLIOptions.test.js | 2 - .../src/{condition.js => condition.ts} | 7 +-- .../{defaultConfig.js => defaultConfig.ts} | 10 ++--- .../src/{deprecated.js => deprecated.ts} | 8 ++-- .../src/{errors.js => errors.ts} | 5 +-- .../{exampleConfig.js => exampleConfig.ts} | 14 +++--- .../jest-validate/src/{index.js => index.ts} | 4 +- packages/jest-validate/src/types.js | 44 ------------------- packages/jest-validate/src/types.ts | 44 +++++++++++++++++++ .../jest-validate/src/{utils.js => utils.ts} | 6 +-- .../src/{validate.js => validate.ts} | 24 +++++----- ...ateCLIOptions.js => validateCLIOptions.ts} | 26 ++++++----- .../src/{warnings.js => warnings.ts} | 9 ++-- packages/jest-validate/tsconfig.json | 12 +++++ 25 files changed, 121 insertions(+), 124 deletions(-) rename packages/jest-validate/src/{condition.js => condition.ts} (85%) rename packages/jest-validate/src/{defaultConfig.js => defaultConfig.ts} (86%) rename packages/jest-validate/src/{deprecated.js => deprecated.ts} (85%) rename packages/jest-validate/src/{errors.js => errors.ts} (95%) rename packages/jest-validate/src/{exampleConfig.js => exampleConfig.ts} (64%) rename packages/jest-validate/src/{index.js => index.ts} (95%) delete mode 100644 packages/jest-validate/src/types.js create mode 100644 packages/jest-validate/src/types.ts rename packages/jest-validate/src/{utils.js => utils.ts} (94%) rename packages/jest-validate/src/{validate.js => validate.ts} (83%) rename packages/jest-validate/src/{validateCLIOptions.js => validateCLIOptions.ts} (86%) rename packages/jest-validate/src/{warnings.js => warnings.ts} (90%) create mode 100644 packages/jest-validate/tsconfig.json diff --git a/CHANGELOG.md b/CHANGELOG.md index f7e612711dae..312504ec2424 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -69,6 +69,7 @@ - `[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)) +- `[jest-validate]`: Migrate to TypeScript ([#7991](https://github.com/facebook/jest/pull/7991)) ### Performance diff --git a/packages/jest-config/src/ReporterValidationErrors.ts b/packages/jest-config/src/ReporterValidationErrors.ts index 0ded7dc3ef29..571f60605513 100644 --- a/packages/jest-config/src/ReporterValidationErrors.ts +++ b/packages/jest-config/src/ReporterValidationErrors.ts @@ -7,7 +7,6 @@ */ import {Config} from '@jest/types'; -// @ts-ignore: Not migrated to TS import {ValidationError} from 'jest-validate'; import chalk from 'chalk'; import getType from 'jest-get-type'; @@ -27,7 +26,7 @@ const ERROR = `${BULLET}Reporter Validation Error`; export function createReporterError( reporterIndex: number, reporterValue: Array | string, -): ValidationError { +) { const errorMessage = ` Reporter at index ${reporterIndex} must be of type:\n` + ` ${chalk.bold.green(validReporterTypes.join(' or '))}\n` + @@ -44,7 +43,7 @@ export function createArrayReporterError( value: string | Object, expectedType: string, valueName: string, -): ValidationError { +) { const errorMessage = ` Unexpected value for ${valueName} ` + `at index ${valueIndex} of reporter at index ${reporterIndex}\n` + diff --git a/packages/jest-config/src/ValidConfig.ts b/packages/jest-config/src/ValidConfig.ts index eb1019e57ec9..1aec66632ba7 100644 --- a/packages/jest-config/src/ValidConfig.ts +++ b/packages/jest-config/src/ValidConfig.ts @@ -7,7 +7,6 @@ import {Config} from '@jest/types'; import {replacePathSepForRegex} from 'jest-regex-util'; -// @ts-ignore: Not migrated to TS import {multipleValidOptions} from 'jest-validate'; import {NODE_MODULES} from './constants'; @@ -15,6 +14,7 @@ const NODE_MODULES_REGEXP = replacePathSepForRegex(NODE_MODULES); const initialOptions: Config.InitialOptions = { automock: false, + // @ts-ignore TODO: type this properly bail: multipleValidOptions(false, 0), browser: false, cache: true, diff --git a/packages/jest-config/src/normalize.ts b/packages/jest-config/src/normalize.ts index ed8940cc3d57..7bf9f8a531fa 100644 --- a/packages/jest-config/src/normalize.ts +++ b/packages/jest-config/src/normalize.ts @@ -9,7 +9,6 @@ import crypto from 'crypto'; import path from 'path'; import glob from 'glob'; import {Config} from '@jest/types'; -// @ts-ignore: Not migrated to TS import {ValidationError, validate} from 'jest-validate'; import {clearLine, replacePathSepForGlob} from 'jest-util'; import chalk from 'chalk'; diff --git a/packages/jest-config/src/utils.ts b/packages/jest-config/src/utils.ts index 5a93c832969d..fb3b139f75b6 100644 --- a/packages/jest-config/src/utils.ts +++ b/packages/jest-config/src/utils.ts @@ -7,7 +7,6 @@ import path from 'path'; import {Config} from '@jest/types'; -// @ts-ignore: Not migrated to TS import {ValidationError} from 'jest-validate'; import Resolver from 'jest-resolve'; import chalk from 'chalk'; diff --git a/packages/jest-config/tsconfig.json b/packages/jest-config/tsconfig.json index 16502ade2786..b33c1679180a 100644 --- a/packages/jest-config/tsconfig.json +++ b/packages/jest-config/tsconfig.json @@ -4,8 +4,8 @@ "rootDir": "src", "outDir": "build" }, - // TODO: This is missing `jest-validate`, in addition to and `jest-jasmine2`, - // but those are just `require.resolve`d, so no real use for their types + // TODO: This is missing `jest-jasmine2`, but that is just + // `require.resolve`d, so no real use for its types "references": [ {"path": "../jest-environment-jsdom"}, {"path": "../jest-environment-node"}, @@ -14,6 +14,7 @@ {"path": "../jest-resolve"}, {"path": "../jest-types"}, {"path": "../jest-util"}, + {"path": "../jest-validate"}, {"path": "../pretty-format"} ] } diff --git a/packages/jest-repl/src/cli/index.ts b/packages/jest-repl/src/cli/index.ts index 5bbf3f975c20..6e79485ca23c 100644 --- a/packages/jest-repl/src/cli/index.ts +++ b/packages/jest-repl/src/cli/index.ts @@ -9,7 +9,6 @@ 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 * as args from './args'; @@ -21,6 +20,7 @@ const REPL_SCRIPT = require.resolve('./repl.js'); export = function() { const argv = yargs.usage(args.usage).options(args.options).argv; + // @ts-ignore: not the same arguments validateCLIOptions(argv, {...args.options, deprecationEntries}); argv._ = [REPL_SCRIPT]; diff --git a/packages/jest-repl/tsconfig.json b/packages/jest-repl/tsconfig.json index 98a536c2c508..49565cc45a7e 100644 --- a/packages/jest-repl/tsconfig.json +++ b/packages/jest-repl/tsconfig.json @@ -8,8 +8,7 @@ {"path": "../jest-config"}, {"path": "../jest-runtime"}, {"path": "../jest-transform"}, - {"path": "../jest-types"} - // TODO: Wait for this to get migrated - // {"path": "../jest-validate"} + {"path": "../jest-types"}, + {"path": "../jest-validate"} ] } diff --git a/packages/jest-runtime/src/cli/index.ts b/packages/jest-runtime/src/cli/index.ts index 70a125076fb4..afe10d9c572f 100644 --- a/packages/jest-runtime/src/cli/index.ts +++ b/packages/jest-runtime/src/cli/index.ts @@ -13,7 +13,6 @@ import yargs from 'yargs'; import {Config} from '@jest/types'; import {JestEnvironment} from '@jest/environment'; import {Console, setGlobal} from 'jest-util'; -// @ts-ignore: Not migrated to TS import {validateCLIOptions} from 'jest-validate'; import {readConfig, deprecationEntries} from 'jest-config'; import {VERSION} from '../version'; @@ -35,6 +34,7 @@ export function run(cliArgv?: Config.Argv, cliInfo?: Array) { .version(false) .options(args.options).argv; + // @ts-ignore: fix this at some point validateCLIOptions(argv, {...args.options, deprecationEntries}); } diff --git a/packages/jest-runtime/tsconfig.json b/packages/jest-runtime/tsconfig.json index d94c744cb988..1623a915ab6b 100644 --- a/packages/jest-runtime/tsconfig.json +++ b/packages/jest-runtime/tsconfig.json @@ -4,7 +4,6 @@ "rootDir": "src", "outDir": "build" }, - // TODO: Missing `jest-validate` "references": [ {"path": "../jest-config"}, {"path": "../jest-environment"}, @@ -15,8 +14,9 @@ {"path": "../jest-regex-util"}, {"path": "../jest-resolve"}, {"path": "../jest-snapshot"}, - {"path": "../jest-util"}, {"path": "../jest-types"}, + {"path": "../jest-util"}, + {"path": "../jest-validate"}, {"path": "../pretty-format"} ] } diff --git a/packages/jest-validate/package.json b/packages/jest-validate/package.json index 2186f1af66c3..c726259d8855 100644 --- a/packages/jest-validate/package.json +++ b/packages/jest-validate/package.json @@ -8,7 +8,9 @@ }, "license": "MIT", "main": "build/index.js", + "types": "build/index.d.ts", "dependencies": { + "@jest/types": "^24.1.0", "camelcase": "^5.0.0", "chalk": "^2.0.1", "jest-get-type": "^24.0.0", diff --git a/packages/jest-validate/src/__tests__/validateCLIOptions.test.js b/packages/jest-validate/src/__tests__/validateCLIOptions.test.js index 08fc29a497c9..5a4dabfbcf9c 100644 --- a/packages/jest-validate/src/__tests__/validateCLIOptions.test.js +++ b/packages/jest-validate/src/__tests__/validateCLIOptions.test.js @@ -6,8 +6,6 @@ * */ -'use strict'; - import validateCLIOptions from '../validateCLIOptions'; test('validates yargs special options', () => { diff --git a/packages/jest-validate/src/condition.js b/packages/jest-validate/src/condition.ts similarity index 85% rename from packages/jest-validate/src/condition.js rename to packages/jest-validate/src/condition.ts index 39fd9107c07c..0df9b8cf1219 100644 --- a/packages/jest-validate/src/condition.js +++ b/packages/jest-validate/src/condition.ts @@ -3,8 +3,6 @@ * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. - * - * @flow */ const toString = Object.prototype.toString; @@ -23,6 +21,7 @@ function validationConditionSingle(option: any, validOption: any): boolean { export function getValues(validOption: any) { if ( Array.isArray(validOption) && + // @ts-ignore validOption[MULTIPLE_VALID_OPTIONS_SYMBOL] ) { return validOption; @@ -34,9 +33,11 @@ export function validationCondition(option: any, validOption: any): boolean { return getValues(validOption).some(e => validationConditionSingle(option, e)); } +// TODO: This should infer the types of its arguments, and return a union type of the types +// See https://github.com/Microsoft/TypeScript/issues/5453 export function multipleValidOptions(...args: Array) { const options = [...args]; - // $FlowFixMe + // @ts-ignore options[MULTIPLE_VALID_OPTIONS_SYMBOL] = true; return options; } diff --git a/packages/jest-validate/src/defaultConfig.js b/packages/jest-validate/src/defaultConfig.ts similarity index 86% rename from packages/jest-validate/src/defaultConfig.js rename to packages/jest-validate/src/defaultConfig.ts index 5242a4d3cadf..6856bbfdeabf 100644 --- a/packages/jest-validate/src/defaultConfig.js +++ b/packages/jest-validate/src/defaultConfig.ts @@ -3,11 +3,9 @@ * * 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 {ValidationOptions} from './types'; +import {ValidationOptions} from './types'; import {deprecationWarning} from './deprecated'; import {unknownOptionWarning} from './warnings'; @@ -15,7 +13,7 @@ import {errorMessage} from './errors'; import {validationCondition} from './condition'; import {ERROR, DEPRECATION, WARNING} from './utils'; -export default ({ +const validationOptions: ValidationOptions = { comment: '', condition: validationCondition, deprecate: deprecationWarning, @@ -31,4 +29,6 @@ export default ({ warning: WARNING, }, unknown: unknownOptionWarning, -}: ValidationOptions); +}; + +export default validationOptions; diff --git a/packages/jest-validate/src/deprecated.js b/packages/jest-validate/src/deprecated.ts similarity index 85% rename from packages/jest-validate/src/deprecated.js rename to packages/jest-validate/src/deprecated.ts index 29eb637e4e46..1d751928f6c1 100644 --- a/packages/jest-validate/src/deprecated.js +++ b/packages/jest-validate/src/deprecated.ts @@ -3,11 +3,9 @@ * * 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 {ValidationOptions} from './types'; +import {DeprecatedOptions, ValidationOptions} from './types'; import {logValidationWarning, DEPRECATION} from './utils'; @@ -19,9 +17,9 @@ const deprecationMessage = (message: string, options: ValidationOptions) => { }; export const deprecationWarning = ( - config: Object, + config: {[key: string]: any}, option: string, - deprecatedOptions: Object, + deprecatedOptions: DeprecatedOptions, options: ValidationOptions, ): boolean => { if (option in deprecatedOptions) { diff --git a/packages/jest-validate/src/errors.js b/packages/jest-validate/src/errors.ts similarity index 95% rename from packages/jest-validate/src/errors.js rename to packages/jest-validate/src/errors.ts index 55bebf76fffa..c5d205330883 100644 --- a/packages/jest-validate/src/errors.js +++ b/packages/jest-validate/src/errors.ts @@ -3,16 +3,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 type {ValidationOptions} from './types'; - import chalk from 'chalk'; import getType from 'jest-get-type'; import {formatPrettyObject, ValidationError, ERROR} from './utils'; import {getValues} from './condition'; +import {ValidationOptions} from './types'; export const errorMessage = ( option: string, diff --git a/packages/jest-validate/src/exampleConfig.js b/packages/jest-validate/src/exampleConfig.ts similarity index 64% rename from packages/jest-validate/src/exampleConfig.js rename to packages/jest-validate/src/exampleConfig.ts index b0d6564ca367..95efae40c352 100644 --- a/packages/jest-validate/src/exampleConfig.js +++ b/packages/jest-validate/src/exampleConfig.ts @@ -3,20 +3,18 @@ * * 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 {ValidationOptions} from './types'; +import {ValidationOptions} from './types'; const config: ValidationOptions = { comment: ' A comment', - condition: (option, validOption) => true, - deprecate: (config, option, deprecatedOptions, options) => false, + condition: () => true, + deprecate: () => false, deprecatedConfig: { - key: config => {}, + key: () => {}, }, - error: (option, received, defaultValue, options) => {}, + error: () => {}, exampleConfig: {key: 'value', test: 'case'}, recursive: true, recursiveBlacklist: [], @@ -25,7 +23,7 @@ const config: ValidationOptions = { error: 'Validation Error', warning: 'Validation Warning', }, - unknown: (config, option, options) => {}, + unknown: () => {}, }; export default config; diff --git a/packages/jest-validate/src/index.js b/packages/jest-validate/src/index.ts similarity index 95% rename from packages/jest-validate/src/index.js rename to packages/jest-validate/src/index.ts index 3e367bfe8974..ff5d721cdd81 100644 --- a/packages/jest-validate/src/index.js +++ b/packages/jest-validate/src/index.ts @@ -3,8 +3,6 @@ * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. - * - * @flow */ import { @@ -17,7 +15,7 @@ import validate from './validate'; import validateCLIOptions from './validateCLIOptions'; import {multipleValidOptions} from './condition'; -module.exports = { +export = { ValidationError, createDidYouMeanMessage, format, diff --git a/packages/jest-validate/src/types.js b/packages/jest-validate/src/types.js deleted file mode 100644 index 5b54958e5882..000000000000 --- a/packages/jest-validate/src/types.js +++ /dev/null @@ -1,44 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @flow - */ - -type Title = {| - deprecation?: string, - error?: string, - warning?: string, -|}; - -export type ValidationOptions = { - comment?: string, - condition?: (option: any, validOption: any) => boolean, - deprecate?: ( - config: Object, - option: string, - deprecatedOptions: Object, - options: ValidationOptions, - ) => boolean, - deprecatedConfig?: {[key: string]: Function}, - error?: ( - option: string, - received: any, - defaultValue: any, - options: ValidationOptions, - path?: Array, - ) => void, - exampleConfig: Object, - recursive?: boolean, - recursiveBlacklist?: Array, - title?: Title, - unknown?: ( - config: Object, - exampleConfig: Object, - option: string, - options: ValidationOptions, - path?: Array, - ) => void, -}; diff --git a/packages/jest-validate/src/types.ts b/packages/jest-validate/src/types.ts new file mode 100644 index 000000000000..06686e35f82e --- /dev/null +++ b/packages/jest-validate/src/types.ts @@ -0,0 +1,44 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +type Title = { + deprecation?: string; + error?: string; + warning?: string; +}; + +export type DeprecatedOptions = Record; + +export type ValidationOptions = { + comment?: string; + condition?: (option: any, validOption: any) => boolean; + deprecate?: ( + config: {[key: string]: any}, + option: string, + deprecatedOptions: DeprecatedOptions, + options: ValidationOptions, + ) => boolean; + deprecatedConfig?: DeprecatedOptions; + error?: ( + option: string, + received: any, + defaultValue: any, + options: ValidationOptions, + path?: Array, + ) => void; + exampleConfig: {[key: string]: any}; + recursive?: boolean; + recursiveBlacklist?: Array; + title?: Title; + unknown?: ( + config: {[key: string]: any}, + exampleConfig: {[key: string]: any}, + option: string, + options: ValidationOptions, + path?: Array, + ) => void; +}; diff --git a/packages/jest-validate/src/utils.js b/packages/jest-validate/src/utils.ts similarity index 94% rename from packages/jest-validate/src/utils.js rename to packages/jest-validate/src/utils.ts index 093c51d6e9c2..3af86cf2719e 100644 --- a/packages/jest-validate/src/utils.js +++ b/packages/jest-validate/src/utils.ts @@ -3,8 +3,6 @@ * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. - * - * @flow */ import chalk from 'chalk'; @@ -32,7 +30,7 @@ export class ValidationError extends Error { name: string; message: string; - constructor(name: string, message: string, comment: ?string) { + constructor(name: string, message: string, comment?: string | null) { super(); comment = comment ? '\n\n' + comment : '\n'; this.name = ''; @@ -44,7 +42,7 @@ export class ValidationError extends Error { export const logValidationWarning = ( name: string, message: string, - comment?: ?string, + comment?: string | null, ) => { comment = comment ? '\n\n' + comment : '\n'; console.warn(chalk.yellow(chalk.bold(name) + ':\n\n' + message + comment)); diff --git a/packages/jest-validate/src/validate.js b/packages/jest-validate/src/validate.ts similarity index 83% rename from packages/jest-validate/src/validate.js rename to packages/jest-validate/src/validate.ts index 0495b5990774..746ab021f804 100644 --- a/packages/jest-validate/src/validate.js +++ b/packages/jest-validate/src/validate.ts @@ -3,11 +3,9 @@ * * 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 {ValidationOptions} from './types'; +import {ValidationOptions} from './types'; import defaultConfig from './defaultConfig'; @@ -16,15 +14,15 @@ let hasDeprecationWarnings = false; const shouldSkipValidationForPath = ( path: Array, key: string, - blacklist: ?Array, + blacklist?: Array, ) => (blacklist ? blacklist.includes([...path, key].join('.')) : false); const _validate = ( - config: Object, - exampleConfig: Object, + config: {[key: string]: any}, + exampleConfig: {[key: string]: any}, options: ValidationOptions, path: Array = [], -) => { +): {hasDeprecationWarnings: boolean} => { if ( typeof config !== 'object' || config == null || @@ -48,7 +46,7 @@ const _validate = ( ); hasDeprecationWarnings = hasDeprecationWarnings || isDeprecatedKey; - } else if (hasOwnProperty.call(exampleConfig, key)) { + } else if (Object.hasOwnProperty.call(exampleConfig, key)) { if ( typeof options.condition === 'function' && typeof options.error === 'function' && @@ -78,14 +76,14 @@ const _validate = ( return {hasDeprecationWarnings}; }; -const validate = (config: Object, options: ValidationOptions) => { +const validate = (config: {[key: string]: any}, options: ValidationOptions) => { hasDeprecationWarnings = false; // Preserve default blacklist entries even with user-supplied blacklist - const combinedBlacklist: Array = [].concat( - defaultConfig.recursiveBlacklist || [], - options.recursiveBlacklist || [], - ); + const combinedBlacklist: Array = [ + ...(defaultConfig.recursiveBlacklist || []), + ...(options.recursiveBlacklist || []), + ]; const defaultedOptions: ValidationOptions = Object.assign({ ...defaultConfig, diff --git a/packages/jest-validate/src/validateCLIOptions.js b/packages/jest-validate/src/validateCLIOptions.ts similarity index 86% rename from packages/jest-validate/src/validateCLIOptions.js rename to packages/jest-validate/src/validateCLIOptions.ts index faf863efc9a2..4b4fdcd5df96 100644 --- a/packages/jest-validate/src/validateCLIOptions.js +++ b/packages/jest-validate/src/validateCLIOptions.ts @@ -3,17 +3,15 @@ * * 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 {Argv} from 'types/Argv'; - +import {Config} from '@jest/types'; import chalk from 'chalk'; import camelcase from 'camelcase'; import {createDidYouMeanMessage, format, ValidationError} from './utils'; import {deprecationWarning} from './deprecated'; import defaultConfig from './defaultConfig'; +import {DeprecatedOptions} from './types'; const BULLET: string = chalk.bold('\u25cf'); export const DOCUMENTATION_NOTE = ` ${chalk.bold('CLI Options Documentation:')} @@ -51,8 +49,8 @@ const createCLIValidationError = ( const logDeprecatedOptions = ( deprecatedOptions: Array, - deprecationEntries: Object, - argv: Argv, + deprecationEntries: DeprecatedOptions, + argv: Config.Argv, ) => { deprecatedOptions.forEach(opt => { deprecationWarning(argv, opt, deprecationEntries, { @@ -63,12 +61,15 @@ const logDeprecatedOptions = ( }; export default function validateCLIOptions( - argv: Argv, - options: Object, + argv: Config.Argv, + options: { + [s: string]: {alias?: string}; + }, rawArgv: string[] = [], ) { const yargsSpecialOptions = ['$0', '_', 'help', 'h']; - const deprecationEntries = options.deprecationEntries || {}; + const deprecationEntries = (options.deprecationEntries || + {}) as DeprecatedOptions; const allowedOptions = Object.keys(options).reduce( (acc, option) => acc.add(option).add(options[option].alias || option), new Set(yargsSpecialOptions), @@ -88,13 +89,14 @@ export default function validateCLIOptions( (acc, entry) => { if (options[entry]) { acc[entry] = deprecationEntries[entry]; - if (options[entry].alias) { - acc[options[entry].alias] = deprecationEntries[entry]; + const alias = options[entry].alias; + if (alias) { + acc[alias] = deprecationEntries[entry]; } } return acc; }, - {}, + {} as {[s: string]: Function}, ); const deprecations = new Set(Object.keys(CLIDeprecations)); const deprecatedOptions = Object.keys(argv).filter( diff --git a/packages/jest-validate/src/warnings.js b/packages/jest-validate/src/warnings.ts similarity index 90% rename from packages/jest-validate/src/warnings.js rename to packages/jest-validate/src/warnings.ts index 24a65da6f069..dd234ae94afe 100644 --- a/packages/jest-validate/src/warnings.js +++ b/packages/jest-validate/src/warnings.ts @@ -3,13 +3,10 @@ * * 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 {ValidationOptions} from './types'; - import chalk from 'chalk'; +import {ValidationOptions} from './types'; import { format, logValidationWarning, @@ -18,8 +15,8 @@ import { } from './utils'; export const unknownOptionWarning = ( - config: Object, - exampleConfig: Object, + config: {[s: string]: any}, + exampleConfig: {[key: string]: any}, option: string, options: ValidationOptions, path?: Array, diff --git a/packages/jest-validate/tsconfig.json b/packages/jest-validate/tsconfig.json new file mode 100644 index 000000000000..7c636682050f --- /dev/null +++ b/packages/jest-validate/tsconfig.json @@ -0,0 +1,12 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "rootDir": "src", + "outDir": "build" + }, + "references": [ + {"path": "../jest-get-type"}, + {"path": "../jest-types"}, + {"path": "../pretty-format"} + ] +} From f84fe87acb696a4becd3978bd3314897c7c893c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Thu, 28 Feb 2019 22:55:08 +0100 Subject: [PATCH 2/2] type yargs a bit --- packages/jest-repl/src/cli/index.ts | 6 +++--- packages/jest-runtime/src/cli/index.ts | 2 +- packages/jest-validate/src/validateCLIOptions.ts | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/jest-repl/src/cli/index.ts b/packages/jest-repl/src/cli/index.ts index 6e79485ca23c..4349baa2bdc4 100644 --- a/packages/jest-repl/src/cli/index.ts +++ b/packages/jest-repl/src/cli/index.ts @@ -11,6 +11,7 @@ import Runtime from 'jest-runtime'; import yargs from 'yargs'; import {validateCLIOptions} from 'jest-validate'; import {deprecationEntries} from 'jest-config'; +import {Config} from '@jest/types'; import * as args from './args'; const {version: VERSION} = require('../../package.json'); @@ -18,13 +19,12 @@ const {version: VERSION} = require('../../package.json'); const REPL_SCRIPT = require.resolve('./repl.js'); export = function() { - const argv = yargs.usage(args.usage).options(args.options).argv; + const argv = yargs.usage(args.usage).options(args.options).argv; - // @ts-ignore: not the same arguments + // @ts-ignore: fix this at some point 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-runtime/src/cli/index.ts b/packages/jest-runtime/src/cli/index.ts index afe10d9c572f..381b300e0078 100644 --- a/packages/jest-runtime/src/cli/index.ts +++ b/packages/jest-runtime/src/cli/index.ts @@ -28,7 +28,7 @@ export function run(cliArgv?: Config.Argv, cliInfo?: Array) { if (cliArgv) { argv = cliArgv; } else { - argv = yargs + argv = yargs .usage(args.usage) .help(false) .version(false) diff --git a/packages/jest-validate/src/validateCLIOptions.ts b/packages/jest-validate/src/validateCLIOptions.ts index 4b4fdcd5df96..54691ded5ac3 100644 --- a/packages/jest-validate/src/validateCLIOptions.ts +++ b/packages/jest-validate/src/validateCLIOptions.ts @@ -64,12 +64,12 @@ export default function validateCLIOptions( argv: Config.Argv, options: { [s: string]: {alias?: string}; + deprecationEntries: DeprecatedOptions; }, rawArgv: string[] = [], ) { const yargsSpecialOptions = ['$0', '_', 'help', 'h']; - const deprecationEntries = (options.deprecationEntries || - {}) as DeprecatedOptions; + const deprecationEntries = options.deprecationEntries || {}; const allowedOptions = Object.keys(options).reduce( (acc, option) => acc.add(option).add(options[option].alias || option), new Set(yargsSpecialOptions), @@ -96,7 +96,7 @@ export default function validateCLIOptions( } return acc; }, - {} as {[s: string]: Function}, + {} as Record, ); const deprecations = new Set(Object.keys(CLIDeprecations)); const deprecatedOptions = Object.keys(argv).filter(