Skip to content

Commit

Permalink
decentralize pretty-format types (#7972)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeysal authored and SimenB committed Feb 24, 2019
1 parent c6280e4 commit 5d48312
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 181 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
### Chore & Maintenance

- `[*]`: Setup building, linting and testing of TypeScript ([#7808](https://github.com/facebook/jest/pull/7808), [#7855](https://github.com/facebook/jest/pull/7855), [#7951](https://github.com/facebook/jest/pull/7951))
- `[pretty-format]`: Migrate to TypeScript ([#7809](https://github.com/facebook/jest/pull/7809))
- `[pretty-format]`: Migrate to TypeScript ([#7809](https://github.com/facebook/jest/pull/7809), [#7809](https://github.com/facebook/jest/pull/7972))
- `[diff-sequences]`: Migrate to Typescript ([#7820](https://github.com/facebook/jest/pull/7820))
- `[jest-get-type]`: Migrate to TypeScript ([#7818](https://github.com/facebook/jest/pull/7818))
- `[jest-regex-util]`: Migrate to TypeScript ([#7822](https://github.com/facebook/jest/pull/7822))
Expand Down
9 changes: 4 additions & 5 deletions packages/jest-snapshot/src/mock_serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
* LICENSE file in the root directory of this source tree.
*/

import {PrettyFormat} from '@jest/types';
import {NewPlugin} from 'pretty-format';

export const serialize: PrettyFormat.NewPlugin['serialize'] = (
export const serialize: NewPlugin['serialize'] = (
val,
config,
indentation,
Expand Down Expand Up @@ -42,9 +42,8 @@ export const serialize: PrettyFormat.NewPlugin['serialize'] = (
return '[MockFunction' + nameString + ']' + callsString;
};

export const test: PrettyFormat.NewPlugin['test'] = val =>
val && !!val._isMockFunction;
export const test: NewPlugin['test'] = val => val && !!val._isMockFunction;

const plugin: PrettyFormat.NewPlugin = {serialize, test};
const plugin: NewPlugin = {serialize, test};

export default plugin;
7 changes: 3 additions & 4 deletions packages/jest-snapshot/src/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import prettyFormat from 'pretty-format';
import {PrettyFormat} from '@jest/types';
import prettyFormat, {Plugin, Plugins} from 'pretty-format';

import jestMockSerializer from './mock_serializer';

Expand All @@ -19,7 +18,7 @@ const {
AsymmetricMatcher,
} = prettyFormat.plugins;

let PLUGINS: PrettyFormat.Plugins = [
let PLUGINS: Plugins = [
ReactTestComponent,
ReactElement,
DOMElement,
Expand All @@ -30,7 +29,7 @@ let PLUGINS: PrettyFormat.Plugins = [
];

// Prepend to list so the last added is the first tested.
export const addSerializer = (plugin: PrettyFormat.Plugin) => {
export const addSerializer = (plugin: Plugin) => {
PLUGINS = [plugin].concat(PLUGINS);
};

Expand Down
117 changes: 0 additions & 117 deletions packages/jest-types/src/PrettyFormat.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 @@ -8,19 +8,9 @@
import * as Config from './Config';
import * as Console from './Console';
import * as Matchers from './Matchers';
import * as PrettyFormat from './PrettyFormat';
import * as SourceMaps from './SourceMaps';
import * as TestResult from './TestResult';
import * as Global from './Global';
import * as Environment from './Environment';

export {
Config,
Console,
Matchers,
PrettyFormat,
SourceMaps,
TestResult,
Global,
Environment,
};
export {Config, Console, Matchers, SourceMaps, TestResult, Global, Environment};
74 changes: 43 additions & 31 deletions packages/pretty-format/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,7 @@
*/

import style from 'ansi-styles';
import {
Colors,
Config,
Options,
OptionsReceived,
NewPlugin,
Plugin,
Plugins,
Refs,
Theme,
} from './types';
import * as PrettyFormat from './types';

import {
printIteratorEntries,
Expand Down Expand Up @@ -179,10 +169,10 @@ function printBasicValue(
*/
function printComplexValue(
val: any,
config: Config,
config: PrettyFormat.Config,
indentation: string,
depth: number,
refs: Refs,
refs: PrettyFormat.Refs,
hasCalledToJSON?: boolean,
): string {
if (refs.indexOf(val) !== -1) {
Expand Down Expand Up @@ -261,17 +251,19 @@ function printComplexValue(
'}';
}

function isNewPlugin(plugin: Plugin): plugin is NewPlugin {
return (plugin as NewPlugin).serialize != null;
function isNewPlugin(
plugin: PrettyFormat.Plugin,
): plugin is PrettyFormat.NewPlugin {
return (plugin as PrettyFormat.NewPlugin).serialize != null;
}

function printPlugin(
plugin: Plugin,
plugin: PrettyFormat.Plugin,
val: any,
config: Config,
config: PrettyFormat.Config,
indentation: string,
depth: number,
refs: Refs,
refs: PrettyFormat.Refs,
): string {
let printed;

Expand Down Expand Up @@ -306,7 +298,7 @@ function printPlugin(
return printed;
}

function findPlugin(plugins: Plugins, val: any) {
function findPlugin(plugins: PrettyFormat.Plugins, val: any) {
for (let p = 0; p < plugins.length; p++) {
try {
if (plugins[p].test(val)) {
Expand All @@ -322,10 +314,10 @@ function findPlugin(plugins: Plugins, val: any) {

function printer(
val: any,
config: Config,
config: PrettyFormat.Config,
indentation: string,
depth: number,
refs: Refs,
refs: PrettyFormat.Refs,
hasCalledToJSON?: boolean,
): string {
const plugin = findPlugin(config.plugins, val);
Expand Down Expand Up @@ -353,7 +345,7 @@ function printer(
);
}

const DEFAULT_THEME: Theme = {
const DEFAULT_THEME: PrettyFormat.Theme = {
comment: 'gray',
content: 'reset',
prop: 'yellow',
Expand All @@ -363,7 +355,7 @@ const DEFAULT_THEME: Theme = {

const DEFAULT_THEME_KEYS = Object.keys(DEFAULT_THEME);

const DEFAULT_OPTIONS: Options = {
const DEFAULT_OPTIONS: PrettyFormat.Options = {
callToJSON: true,
escapeRegex: false,
escapeString: true,
Expand All @@ -376,7 +368,7 @@ const DEFAULT_OPTIONS: Options = {
theme: DEFAULT_THEME,
};

function validateOptions(options: OptionsReceived) {
function validateOptions(options: PrettyFormat.OptionsReceived) {
Object.keys(options).forEach(key => {
if (!DEFAULT_OPTIONS.hasOwnProperty(key)) {
throw new Error(`pretty-format: Unknown option "${key}".`);
Expand All @@ -402,7 +394,9 @@ function validateOptions(options: OptionsReceived) {
}
}

const getColorsHighlight = (options: OptionsReceived): Colors =>
const getColorsHighlight = (
options: PrettyFormat.OptionsReceived,
): PrettyFormat.Colors =>
DEFAULT_THEME_KEYS.reduce((colors, key) => {
const value =
options.theme && (options.theme as any)[key] !== undefined
Expand All @@ -423,28 +417,30 @@ const getColorsHighlight = (options: OptionsReceived): Colors =>
return colors;
}, Object.create(null));

const getColorsEmpty = (): Colors =>
const getColorsEmpty = (): PrettyFormat.Colors =>
DEFAULT_THEME_KEYS.reduce((colors, key) => {
colors[key] = {close: '', open: ''};
return colors;
}, Object.create(null));

const getPrintFunctionName = (options?: OptionsReceived) =>
const getPrintFunctionName = (options?: PrettyFormat.OptionsReceived) =>
options && options.printFunctionName !== undefined
? options.printFunctionName
: DEFAULT_OPTIONS.printFunctionName;

const getEscapeRegex = (options?: OptionsReceived) =>
const getEscapeRegex = (options?: PrettyFormat.OptionsReceived) =>
options && options.escapeRegex !== undefined
? options.escapeRegex
: DEFAULT_OPTIONS.escapeRegex;

const getEscapeString = (options?: OptionsReceived) =>
const getEscapeString = (options?: PrettyFormat.OptionsReceived) =>
options && options.escapeString !== undefined
? options.escapeString
: DEFAULT_OPTIONS.escapeString;

const getConfig = (options?: OptionsReceived): Config => ({
const getConfig = (
options?: PrettyFormat.OptionsReceived,
): PrettyFormat.Config => ({
callToJSON:
options && options.callToJSON !== undefined
? options.callToJSON
Expand Down Expand Up @@ -486,7 +482,10 @@ function createIndent(indent: number): string {
* @param val any potential JavaScript object
* @param options Custom settings
*/
function prettyFormat(val: any, options?: OptionsReceived): string {
function prettyFormat(
val: any,
options?: PrettyFormat.OptionsReceived,
): string {
if (options) {
validateOptions(options);
if (options.plugins) {
Expand Down Expand Up @@ -520,4 +519,17 @@ prettyFormat.plugins = {
ReactTestComponent,
};

/* eslint-disable-next-line no-redeclare */
namespace prettyFormat {
export type Colors = PrettyFormat.Colors;
export type Config = PrettyFormat.Config;
export type Options = PrettyFormat.Options;
export type OptionsReceived = PrettyFormat.OptionsReceived;
export type NewPlugin = PrettyFormat.NewPlugin;
export type Plugin = PrettyFormat.Plugin;
export type Plugins = PrettyFormat.Plugins;
export type Refs = PrettyFormat.Refs;
export type Theme = PrettyFormat.Theme;
}

export = prettyFormat;
Loading

0 comments on commit 5d48312

Please sign in to comment.