Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: replace function types #10436

Merged
merged 4 commits into from
Sep 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/jest-circus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@jest/environment": "^26.3.0",
"@jest/test-result": "^26.3.0",
"@jest/types": "^26.3.0",
"@types/babel__traverse": "^7.0.4",
"@types/node": "*",
"chalk": "^4.0.0",
"co": "^4.6.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import type BabelTraverse from '@babel/traverse';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this in package.json?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup :)

import type {Circus, Config, Global} from '@jest/types';
import type {JestEnvironment} from '@jest/environment';
import {
Expand Down Expand Up @@ -51,7 +52,7 @@ export const initialize = async ({
config: Config.ProjectConfig;
environment: JestEnvironment;
getPrettier: () => null | any;
getBabelTraverse: () => Function;
getBabelTraverse: () => typeof BabelTraverse;
globalConfig: Config.GlobalConfig;
localRequire: (path: Config.Path) => any;
testPath: Config.Path;
Expand Down
15 changes: 9 additions & 6 deletions packages/jest-config/src/Deprecated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@

import chalk = require('chalk');
import prettyFormat = require('pretty-format');
import type {DeprecatedOptions} from 'jest-validate';

const format = (value: unknown) => prettyFormat(value, {min: true});

export default {
const deprecatedOptions: DeprecatedOptions = {
browser: () => ` Option ${chalk.bold(
'"browser"',
)} has been deprecated. Please install "browser-resolve" and use the "resolver" option in Jest configuration as follows:
Expand All @@ -26,7 +27,7 @@ export default {
Please update your configuration.`,

preprocessorIgnorePatterns: (options: {
preprocessorIgnorePatterns: Array<string>;
preprocessorIgnorePatterns?: Array<string>;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While technically at runtime this isn't true (because whole logic is that these functions are being called when options contains the property they're the value of), given they're deprecated and this doesn't break anything, I think it's fine 🤷

}) => ` Option ${chalk.bold(
'"preprocessorIgnorePatterns"',
)} was replaced by ${chalk.bold(
Expand All @@ -43,7 +44,7 @@ export default {
Please update your configuration.`,

scriptPreprocessor: (options: {
scriptPreprocessor: string;
scriptPreprocessor?: string;
}) => ` Option ${chalk.bold(
'"scriptPreprocessor"',
)} was replaced by ${chalk.bold(
Expand All @@ -60,7 +61,7 @@ export default {
Please update your configuration.`,

setupTestFrameworkScriptFile: (_options: {
setupTestFrameworkScriptFile: Array<string>;
setupTestFrameworkScriptFile?: string;
}) => ` Option ${chalk.bold(
'"setupTestFrameworkScriptFile"',
)} was replaced by configuration ${chalk.bold(
Expand All @@ -70,7 +71,7 @@ export default {
Please update your configuration.`,

testPathDirs: (options: {
testPathDirs: Array<string>;
testPathDirs?: Array<string>;
}) => ` Option ${chalk.bold('"testPathDirs"')} was replaced by ${chalk.bold(
'"roots"',
)}.
Expand All @@ -82,4 +83,6 @@ export default {

Please update your configuration.
`,
} as Record<string, Function>;
};

export default deprecatedOptions;
1 change: 1 addition & 0 deletions packages/jest-snapshot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"dependencies": {
"@babel/types": "^7.0.0",
"@jest/types": "^26.3.0",
"@types/babel__traverse": "^7.0.4",
"@types/prettier": "^2.0.0",
"chalk": "^4.0.0",
"expect": "^26.4.2",
Expand Down
8 changes: 4 additions & 4 deletions packages/jest-snapshot/src/State.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ import {
testNameToKey,
} from './utils';
import {InlineSnapshot, saveInlineSnapshots} from './inline_snapshots';
import type {SnapshotData} from './types';
import type {BabelTraverse, Prettier, SnapshotData} from './types';

export type SnapshotStateOptions = {
updateSnapshot: Config.SnapshotUpdateState;
getPrettier: () => null | typeof import('prettier');
getBabelTraverse: () => Function;
getPrettier: () => null | Prettier;
getBabelTraverse: () => BabelTraverse;
expand?: boolean;
};

Expand Down Expand Up @@ -62,7 +62,7 @@ export default class SnapshotState {
private _snapshotPath: Config.Path;
private _inlineSnapshots: Array<InlineSnapshot>;
private _uncheckedKeys: Set<string>;
private _getBabelTraverse: () => Function;
private _getBabelTraverse: () => BabelTraverse;
private _getPrettier: () => null | typeof import('prettier');

added: number;
Expand Down
12 changes: 6 additions & 6 deletions packages/jest-snapshot/src/inline_snapshots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import {
templateLiteral,
} from '@babel/types';
import type {Frame} from 'jest-message-util';

import type {Config} from '@jest/types';
import type {BabelTraverse, Prettier} from './types';
import {escapeBacktickString} from './utils';

export type InlineSnapshot = {
Expand All @@ -26,8 +26,8 @@ export type InlineSnapshot = {

export function saveInlineSnapshots(
snapshots: Array<InlineSnapshot>,
prettier: typeof import('prettier') | null,
babelTraverse: Function,
prettier: Prettier | null,
babelTraverse: BabelTraverse,
): void {
if (!prettier) {
throw new Error(
Expand Down Expand Up @@ -60,7 +60,7 @@ const saveSnapshotsForFile = (
snapshots: Array<InlineSnapshot>,
sourceFilePath: Config.Path,
prettier: any,
babelTraverse: Function,
babelTraverse: BabelTraverse,
) => {
const sourceFile = fs.readFileSync(sourceFilePath, 'utf8');

Expand Down Expand Up @@ -181,7 +181,7 @@ const createInsertionParser = (
snapshots: Array<InlineSnapshot>,
snapshotMatcherNames: Array<string>,
inferredParser: string,
babelTraverse: Function,
babelTraverse: BabelTraverse,
) => (
text: string,
parsers: Record<string, (text: string) => any>,
Expand Down Expand Up @@ -248,7 +248,7 @@ const createInsertionParser = (
const createFormattingParser = (
snapshotMatcherNames: Array<string>,
inferredParser: string,
babelTraverse: Function,
babelTraverse: BabelTraverse,
) => (
text: string,
parsers: Record<string, (text: string) => any>,
Expand Down
3 changes: 3 additions & 0 deletions packages/jest-snapshot/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ export type ExpectationResult = {
pass: boolean;
message: () => string;
};

export type BabelTraverse = typeof import('@babel/traverse').default;
export type Prettier = typeof import('prettier');
3 changes: 1 addition & 2 deletions packages/jest-validate/src/exampleConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ const config: ValidationOptions = {
condition: () => true,
deprecate: () => false,
deprecatedConfig: {
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
key: () => {},
key: (): string => 'Deprecation message',
},
error: () => {},
exampleConfig: {key: 'value', test: 'case'},
Expand Down
1 change: 1 addition & 0 deletions packages/jest-validate/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export {
format,
logValidationWarning,
} from './utils';
export type {DeprecatedOptions} from './types';
export {default as validate} from './validate';
export {default as validateCLIOptions} from './validateCLIOptions';
export {multipleValidOptions} from './condition';
4 changes: 3 additions & 1 deletion packages/jest-validate/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ type Title = {
warning?: string;
};

export type DeprecatedOptions = Record<string, Function>;
export type DeprecatedOptionFunc = (arg: Record<string, any>) => string;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export type DeprecatedOptionFunc = (arg: Record<string, any>) => string;
export type DeprecatedOptionFunc = (arg: Record<string, unknown>) => string;

?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iirc no because then TS knows too much and would force us to properly type the deprecated types too - I think a good way to make this change would be to do it after the latest deprecated options are removed, so to require all future deprecated options to be more accurately typed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good 👍


export type DeprecatedOptions = Record<string, DeprecatedOptionFunc>;

export type ValidationOptions = {
comment?: string;
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-validate/src/validateCLIOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type {Options} from 'yargs';
import {ValidationError, createDidYouMeanMessage, format} from './utils';
import {deprecationWarning} from './deprecated';
import defaultConfig from './defaultConfig';
import type {DeprecatedOptions} from './types';
import type {DeprecatedOptionFunc, DeprecatedOptions} from './types';

const BULLET: string = chalk.bold('\u25cf');
export const DOCUMENTATION_NOTE = ` ${chalk.bold('CLI Options Documentation:')}
Expand Down Expand Up @@ -88,7 +88,7 @@ export default function validateCLIOptions(
}

const CLIDeprecations = Object.keys(deprecationEntries).reduce<
Record<string, Function>
Record<string, DeprecatedOptionFunc>
>((acc, entry) => {
if (options[entry]) {
acc[entry] = deprecationEntries[entry];
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11887,6 +11887,7 @@ fsevents@^1.2.7:
"@babel/traverse": ^7.3.4
"@babel/types": ^7.0.0
"@jest/types": ^26.3.0
"@types/babel__traverse": ^7.0.4
"@types/graceful-fs": ^4.1.3
"@types/natural-compare": ^1.4.0
"@types/prettier": ^2.0.0
Expand Down