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

use Plugin type for pretty-format plugins #3095

Merged
merged 2 commits into from
Mar 8, 2017
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
4 changes: 4 additions & 0 deletions packages/pretty-format/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ type Theme = {|
type InitialOptions = {|
callToJSON?: boolean,
escapeRegex?: boolean,
edgeSpacing?: string,
highlight?: boolean,
indent?: number,
maxDepth?: number,
min?: boolean,
plugins?: Plugins,
printFunctionName?: boolean,
spacing?: string,
theme?: Theme,
|};

Expand Down Expand Up @@ -770,13 +772,15 @@ function print(

const DEFAULTS: Options = {
callToJSON: true,
edgeSpacing: '\n',
escapeRegex: false,
highlight: false,
indent: 2,
maxDepth: Infinity,
min: false,
plugins: [],
printFunctionName: true,
spacing: '\n',
theme: {
content: 'reset',
prop: 'yellow',
Expand Down
19 changes: 10 additions & 9 deletions packages/pretty-format/src/plugins/AsymmetricMatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@

'use strict';

import type {Colors, Indent, Options, Print, Plugin} from '../types.js';

const asymmetricMatcher = Symbol.for('jest.asymmetricMatcher');
const SPACE = ' ';

class ArrayContaining extends Array {}
class ObjectContaining extends Object {}

const printAsymmetricMatcher = (
const print = (
val: any,
print: Function,
indent: Function,
opts: Object,
colors: Object,
print: Print,
indent: Indent,
opts: Options,
colors: Colors,
) => {
const stringedValue = val.toString();

Expand Down Expand Up @@ -49,7 +51,6 @@ const printAsymmetricMatcher = (
return val.toAsymmetricMatcher();
};

module.exports = {
print: printAsymmetricMatcher,
test: (object: any) => object && object.$$typeof === asymmetricMatcher,
};
const test = (object: any) => object && object.$$typeof === asymmetricMatcher;

module.exports = ({print, test}: Plugin);
4 changes: 2 additions & 2 deletions packages/pretty-format/src/plugins/ImmutableList.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

'use strict';

import type {Colors, Indent, Options, Print} from '../types.js';
import type {Colors, Indent, Options, Print, Plugin} from '../types.js';

const printImmutable = require('./lib/printImmutable');

Expand All @@ -25,4 +25,4 @@ const print = (
colors: Colors,
) => printImmutable(val, print, indent, opts, colors, 'List', false);

module.exports = {print, test};
module.exports = ({print, test}: Plugin);
8 changes: 5 additions & 3 deletions packages/pretty-format/src/plugins/ImmutableMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@

'use strict';

import type {Colors, Indent, Options, Print} from '../types.js';
import type {Colors, Indent, Options, Print, Plugin} from '../types.js';

const printImmutable = require('./lib/printImmutable');

const IS_MAP = '@@__IMMUTABLE_MAP__@@';
const test = (maybeMap: any) => !!(maybeMap && maybeMap[IS_MAP]);
const IS_ORDERED = '@@__IMMUTABLE_ORDERED__@@';
const test = (maybeMap: any) =>
!!(maybeMap && maybeMap[IS_MAP] && !maybeMap[IS_ORDERED]);

const print = (
val: any,
Expand All @@ -25,4 +27,4 @@ const print = (
colors: Colors,
) => printImmutable(val, print, indent, opts, colors, 'Map', true);

module.exports = {print, test};
module.exports = ({print, test}: Plugin);
9 changes: 3 additions & 6 deletions packages/pretty-format/src/plugins/ImmutableOrderedMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,14 @@

'use strict';

import type {Colors, Indent, Options, Print} from '../types.js';
import type {Colors, Indent, Options, Print, Plugin} from '../types.js';

const printImmutable = require('./lib/printImmutable');

const IS_MAP = '@@__IMMUTABLE_MAP__@@';
const IS_ORDERED = '@@__IMMUTABLE_ORDERED__@@';
const isMap = (maybeMap: any) => !!maybeMap[IS_MAP];
const isOrdered = (maybeOrdered: any) => !!maybeOrdered[IS_ORDERED];

const test = (maybeOrderedMap: any) =>
maybeOrderedMap && isMap(maybeOrderedMap) && isOrdered(maybeOrderedMap);
maybeOrderedMap && maybeOrderedMap[IS_MAP] && maybeOrderedMap[IS_ORDERED];

const print = (
val: any,
Expand All @@ -30,4 +27,4 @@ const print = (
colors: Colors,
) => printImmutable(val, print, indent, opts, colors, 'OrderedMap', true);

module.exports = {print, test};
module.exports = ({print, test}: Plugin);
9 changes: 3 additions & 6 deletions packages/pretty-format/src/plugins/ImmutableOrderedSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,14 @@

'use strict';

import type {Colors, Indent, Options, Print} from '../types.js';
import type {Colors, Indent, Options, Print, Plugin} from '../types.js';

const printImmutable = require('./lib/printImmutable');

const IS_SET = '@@__IMMUTABLE_SET__@@';
const IS_ORDERED = '@@__IMMUTABLE_ORDERED__@@';
const isSet = (maybeSet: any) => !!maybeSet[IS_SET];
const isOrdered = (maybeOrdered: any) => !!maybeOrdered[IS_ORDERED];

const test = (maybeOrderedSet: any) =>
maybeOrderedSet && isSet(maybeOrderedSet) && isOrdered(maybeOrderedSet);
maybeOrderedSet && maybeOrderedSet[IS_SET] && maybeOrderedSet[IS_ORDERED];

const print = (
val: any,
Expand All @@ -30,4 +27,4 @@ const print = (
colors: Colors,
) => printImmutable(val, print, indent, opts, colors, 'OrderedSet', false);

module.exports = {print, test};
module.exports = ({print, test}: Plugin);
4 changes: 2 additions & 2 deletions packages/pretty-format/src/plugins/ImmutablePlugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
'use strict';

module.exports = [
require('./ImmutableOrderedSet'),
require('./ImmutableOrderedMap'),
require('./ImmutableList'),
require('./ImmutableSet'),
require('./ImmutableMap'),
require('./ImmutableStack'),
require('./ImmutableOrderedSet'),
require('./ImmutableOrderedMap'),
];
8 changes: 5 additions & 3 deletions packages/pretty-format/src/plugins/ImmutableSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@

'use strict';

import type {Colors, Indent, Options, Print} from '../types.js';
import type {Colors, Indent, Options, Print, Plugin} from '../types.js';

const printImmutable = require('./lib/printImmutable');

const IS_SET = '@@__IMMUTABLE_SET__@@';
const test = (maybeSet: any) => !!(maybeSet && maybeSet[IS_SET]);
const IS_ORDERED = '@@__IMMUTABLE_ORDERED__@@';
const test = (maybeSet: any) =>
!!(maybeSet && maybeSet[IS_SET] && !maybeSet[IS_ORDERED]);

const print = (
val: any,
Expand All @@ -25,4 +27,4 @@ const print = (
colors: Colors,
) => printImmutable(val, print, indent, opts, colors, 'Set', false);

module.exports = {print, test};
module.exports = ({print, test}: Plugin);
4 changes: 2 additions & 2 deletions packages/pretty-format/src/plugins/ImmutableStack.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

'use strict';

import type {Colors, Indent, Options, Print} from '../types.js';
import type {Colors, Indent, Options, Print, Plugin} from '../types.js';

const printImmutable = require('./lib/printImmutable');

Expand All @@ -25,4 +25,4 @@ const print = (
colors: Colors,
) => printImmutable(val, print, indent, opts, colors, 'Stack', false);

module.exports = {print, test};
module.exports = ({print, test}: Plugin);
25 changes: 14 additions & 11 deletions packages/pretty-format/src/plugins/ReactElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
/* eslint-disable max-len */
'use strict';

import type {Colors, Indent, Options, Print, Plugin} from '../types.js';

const escapeHTML = require('./lib/escapeHTML');

const reactElement = Symbol.for('react.element');
Expand All @@ -24,7 +26,7 @@ function printChildren(flatChildren, print, indent, colors, opts) {
return flatChildren
.map(node => {
if (typeof node === 'object') {
return printElement(node, print, indent, colors, opts);
return print(node, print, indent, colors, opts);
} else if (typeof node === 'string') {
return colors.content.open + escapeHTML(node) + colors.content.close;
} else {
Expand Down Expand Up @@ -64,7 +66,13 @@ function printProps(props, print, indent, colors, opts) {
.join('');
}

function printElement(element, print, indent, colors, opts) {
const print = (
element: any,
print: Print,
indent: Indent,
opts: Options,
colors: Colors,
) => {
let result = colors.tag.open + '<';
let elementName;
if (typeof element.type === 'string') {
Expand Down Expand Up @@ -109,13 +117,8 @@ function printElement(element, print, indent, colors, opts) {
}

return result;
}

module.exports = {
print(val, print, indent, opts, colors) {
return printElement(val, print, indent, colors, opts);
},
test(object) {
return object && object.$$typeof === reactElement;
},
};

const test = (object: any) => object && object.$$typeof === reactElement;

module.exports = ({print, test}: Plugin);
45 changes: 22 additions & 23 deletions packages/pretty-format/src/plugins/ReactTestComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,20 @@
/* eslint-disable max-len */
'use strict';

import type {
Colors,
Indent,
Options,
Print,
Plugin,
ReactTestObject,
ReactTestChild,
} from '../types.js';

const escapeHTML = require('./lib/escapeHTML');

const reactTestInstance = Symbol.for('react.test.json');

type ReactTestObject = {|
$$typeof: Symbol,
type: string,
props?: Object,
children?: null | Array<ReactTestChild>,
|};
// Child can be `number` in Stack renderer but not in Fiber renderer.
type ReactTestChild = ReactTestObject | string | number;

function printChildren(
children: Array<ReactTestChild>,
print,
Expand Down Expand Up @@ -108,17 +109,15 @@ function printInstance(instance: ReactTestChild, print, indent, colors, opts) {
return result;
}

module.exports = {
print(
val: ReactTestObject,
print: (val: any) => string,
indent: (str: string) => string,
opts: Object,
colors: Object,
) {
return printInstance(val, print, indent, colors, opts);
},
test(object: Object) {
return object && object.$$typeof === reactTestInstance;
},
};
const print = (
val: ReactTestObject,
print: Print,
indent: Indent,
opts: Options,
colors: Colors,
) => printInstance(val, print, indent, colors, opts);

const test = (object: Object) =>
object && object.$$typeof === reactTestInstance;

module.exports = ({print, test}: Plugin);
12 changes: 7 additions & 5 deletions packages/pretty-format/src/plugins/lib/printImmutable.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

'use strict';

import type {Colors, Indent, Options, Print} from '../../types.js';

const IMMUTABLE_NAMESPACE = 'Immutable.';
const SPACE = ' ';

Expand All @@ -19,11 +21,11 @@ const addFinalEdgeSpacing = (length: number, edgeSpacing: string) =>
length > 0 ? edgeSpacing : '';

const printImmutable = (
val: Object,
print: Function,
indent: Function,
opts: Object,
colors: Object,
val: any,
print: Print,
indent: Indent,
opts: Options,
colors: Colors,
immutableDataStructureName: string,
isMap: boolean,
): string => {
Expand Down
12 changes: 12 additions & 0 deletions packages/pretty-format/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ export type StringOrNull = string | null;

export type Options = {|
callToJSON: boolean,
edgeSpacing: string,
escapeRegex: boolean,
highlight: boolean,
indent: number,
maxDepth: number,
min: boolean,
plugins: Plugins,
printFunctionName: boolean,
spacing: string,
theme: {|
content: string,
prop: string,
Expand All @@ -44,3 +46,13 @@ export type Plugin = {
};

export type Plugins = Array<Plugin>;

export type ReactTestObject = {|
$$typeof: Symbol,
type: string,
props?: Object,
children?: null | Array<ReactTestChild>,
|};

// Child can be `number` in Stack renderer but not in Fiber renderer.
export type ReactTestChild = ReactTestObject | string | number;