From 15e93985c9abf55f4e7ed6324b091667b9d967d1 Mon Sep 17 00:00:00 2001 From: Cristian Penarrieta Date: Tue, 7 Mar 2017 16:20:44 -0800 Subject: [PATCH 1/2] use Plugin type for pretty-format plugins --- packages/pretty-format/src/index.js | 4 ++ .../src/plugins/AsymmetricMatcher.js | 19 ++++---- .../src/plugins/ImmutableList.js | 4 +- .../pretty-format/src/plugins/ImmutableMap.js | 11 +++-- .../src/plugins/ImmutableOrderedMap.js | 4 +- .../src/plugins/ImmutableOrderedSet.js | 4 +- .../src/plugins/ImmutablePlugins.js | 4 +- .../pretty-format/src/plugins/ImmutableSet.js | 11 +++-- .../src/plugins/ImmutableStack.js | 4 +- .../pretty-format/src/plugins/ReactElement.js | 25 ++++++----- .../src/plugins/ReactTestComponent.js | 45 +++++++++---------- .../src/plugins/lib/printImmutable.js | 12 ++--- packages/pretty-format/src/types.js | 12 +++++ 13 files changed, 95 insertions(+), 64 deletions(-) diff --git a/packages/pretty-format/src/index.js b/packages/pretty-format/src/index.js index 315bf0ad7341..eeb1c6263057 100644 --- a/packages/pretty-format/src/index.js +++ b/packages/pretty-format/src/index.js @@ -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, |}; @@ -770,6 +772,7 @@ function print( const DEFAULTS: Options = { callToJSON: true, + edgeSpacing: '\n', escapeRegex: false, highlight: false, indent: 2, @@ -777,6 +780,7 @@ const DEFAULTS: Options = { min: false, plugins: [], printFunctionName: true, + spacing: '\n', theme: { content: 'reset', prop: 'yellow', diff --git a/packages/pretty-format/src/plugins/AsymmetricMatcher.js b/packages/pretty-format/src/plugins/AsymmetricMatcher.js index 4001a694e418..839d97dbd246 100644 --- a/packages/pretty-format/src/plugins/AsymmetricMatcher.js +++ b/packages/pretty-format/src/plugins/AsymmetricMatcher.js @@ -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(); @@ -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); diff --git a/packages/pretty-format/src/plugins/ImmutableList.js b/packages/pretty-format/src/plugins/ImmutableList.js index dedfd7925caf..9239bb6d3fe5 100644 --- a/packages/pretty-format/src/plugins/ImmutableList.js +++ b/packages/pretty-format/src/plugins/ImmutableList.js @@ -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'); @@ -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); diff --git a/packages/pretty-format/src/plugins/ImmutableMap.js b/packages/pretty-format/src/plugins/ImmutableMap.js index c569b342303f..89d663ae0bfc 100644 --- a/packages/pretty-format/src/plugins/ImmutableMap.js +++ b/packages/pretty-format/src/plugins/ImmutableMap.js @@ -10,12 +10,17 @@ '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 isMap = (maybeMap: any) => !!maybeMap[IS_MAP]; +const isNotOrdered = (maybeOrdered: any) => !maybeOrdered[IS_ORDERED]; + +const test = (maybeMap: any) => + !!(maybeMap && isMap(maybeMap) && isNotOrdered(maybeMap)); const print = ( val: any, @@ -25,4 +30,4 @@ const print = ( colors: Colors, ) => printImmutable(val, print, indent, opts, colors, 'Map', true); -module.exports = {print, test}; +module.exports = ({print, test}: Plugin); diff --git a/packages/pretty-format/src/plugins/ImmutableOrderedMap.js b/packages/pretty-format/src/plugins/ImmutableOrderedMap.js index ffae01029db2..ebee70bed7a5 100644 --- a/packages/pretty-format/src/plugins/ImmutableOrderedMap.js +++ b/packages/pretty-format/src/plugins/ImmutableOrderedMap.js @@ -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'); @@ -30,4 +30,4 @@ const print = ( colors: Colors, ) => printImmutable(val, print, indent, opts, colors, 'OrderedMap', true); -module.exports = {print, test}; +module.exports = ({print, test}: Plugin); diff --git a/packages/pretty-format/src/plugins/ImmutableOrderedSet.js b/packages/pretty-format/src/plugins/ImmutableOrderedSet.js index 41b7cafee022..051fea2ceef3 100644 --- a/packages/pretty-format/src/plugins/ImmutableOrderedSet.js +++ b/packages/pretty-format/src/plugins/ImmutableOrderedSet.js @@ -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'); @@ -30,4 +30,4 @@ const print = ( colors: Colors, ) => printImmutable(val, print, indent, opts, colors, 'OrderedSet', false); -module.exports = {print, test}; +module.exports = ({print, test}: Plugin); diff --git a/packages/pretty-format/src/plugins/ImmutablePlugins.js b/packages/pretty-format/src/plugins/ImmutablePlugins.js index 650cfe2c6fda..03f8a189a97e 100644 --- a/packages/pretty-format/src/plugins/ImmutablePlugins.js +++ b/packages/pretty-format/src/plugins/ImmutablePlugins.js @@ -11,10 +11,10 @@ 'use strict'; module.exports = [ - require('./ImmutableOrderedSet'), - require('./ImmutableOrderedMap'), require('./ImmutableList'), require('./ImmutableSet'), require('./ImmutableMap'), require('./ImmutableStack'), + require('./ImmutableOrderedSet'), + require('./ImmutableOrderedMap'), ]; diff --git a/packages/pretty-format/src/plugins/ImmutableSet.js b/packages/pretty-format/src/plugins/ImmutableSet.js index ca18c537e1e5..45448bee303d 100644 --- a/packages/pretty-format/src/plugins/ImmutableSet.js +++ b/packages/pretty-format/src/plugins/ImmutableSet.js @@ -10,12 +10,17 @@ '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 isSet = (maybeSet: any) => !!maybeSet[IS_SET]; +const isNotOrdered = (maybeOrdered: any) => !maybeOrdered[IS_ORDERED]; + +const test = (maybeSet: any) => + !!(maybeSet && isSet(maybeSet) && isNotOrdered(maybeSet)); const print = ( val: any, @@ -25,4 +30,4 @@ const print = ( colors: Colors, ) => printImmutable(val, print, indent, opts, colors, 'Set', false); -module.exports = {print, test}; +module.exports = ({print, test}: Plugin); diff --git a/packages/pretty-format/src/plugins/ImmutableStack.js b/packages/pretty-format/src/plugins/ImmutableStack.js index a7502f28ed72..7f4de0331042 100644 --- a/packages/pretty-format/src/plugins/ImmutableStack.js +++ b/packages/pretty-format/src/plugins/ImmutableStack.js @@ -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'); @@ -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); diff --git a/packages/pretty-format/src/plugins/ReactElement.js b/packages/pretty-format/src/plugins/ReactElement.js index 6e12881af28c..d1884e9c7bf5 100644 --- a/packages/pretty-format/src/plugins/ReactElement.js +++ b/packages/pretty-format/src/plugins/ReactElement.js @@ -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'); @@ -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 { @@ -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') { @@ -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); diff --git a/packages/pretty-format/src/plugins/ReactTestComponent.js b/packages/pretty-format/src/plugins/ReactTestComponent.js index ba3d34483455..74bc26741b6c 100644 --- a/packages/pretty-format/src/plugins/ReactTestComponent.js +++ b/packages/pretty-format/src/plugins/ReactTestComponent.js @@ -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, -|}; -// Child can be `number` in Stack renderer but not in Fiber renderer. -type ReactTestChild = ReactTestObject | string | number; - function printChildren( children: Array, print, @@ -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); diff --git a/packages/pretty-format/src/plugins/lib/printImmutable.js b/packages/pretty-format/src/plugins/lib/printImmutable.js index d25999db14bc..5e69d26ede88 100644 --- a/packages/pretty-format/src/plugins/lib/printImmutable.js +++ b/packages/pretty-format/src/plugins/lib/printImmutable.js @@ -10,6 +10,8 @@ 'use strict'; +import type {Colors, Indent, Options, Print} from '../../types.js'; + const IMMUTABLE_NAMESPACE = 'Immutable.'; const SPACE = ' '; @@ -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 => { diff --git a/packages/pretty-format/src/types.js b/packages/pretty-format/src/types.js index ba684992524b..f6c77bb8753b 100644 --- a/packages/pretty-format/src/types.js +++ b/packages/pretty-format/src/types.js @@ -17,6 +17,7 @@ export type StringOrNull = string | null; export type Options = {| callToJSON: boolean, + edgeSpacing: string, escapeRegex: boolean, highlight: boolean, indent: number, @@ -24,6 +25,7 @@ export type Options = {| min: boolean, plugins: Plugins, printFunctionName: boolean, + spacing: string, theme: {| content: string, prop: string, @@ -44,3 +46,13 @@ export type Plugin = { }; export type Plugins = Array; + +export type ReactTestObject = {| + $$typeof: Symbol, + type: string, + props?: Object, + children?: null | Array, +|}; + +// Child can be `number` in Stack renderer but not in Fiber renderer. +export type ReactTestChild = ReactTestObject | string | number; From 2e4fc823849764faab2ae4f814aa5afc9ea8269b Mon Sep 17 00:00:00 2001 From: Cristian Penarrieta Date: Wed, 8 Mar 2017 00:54:17 -0800 Subject: [PATCH 2/2] removing unnecesary overhead --- packages/pretty-format/src/plugins/ImmutableMap.js | 5 +---- packages/pretty-format/src/plugins/ImmutableOrderedMap.js | 5 +---- packages/pretty-format/src/plugins/ImmutableOrderedSet.js | 5 +---- packages/pretty-format/src/plugins/ImmutableSet.js | 5 +---- 4 files changed, 4 insertions(+), 16 deletions(-) diff --git a/packages/pretty-format/src/plugins/ImmutableMap.js b/packages/pretty-format/src/plugins/ImmutableMap.js index 89d663ae0bfc..222873de5c17 100644 --- a/packages/pretty-format/src/plugins/ImmutableMap.js +++ b/packages/pretty-format/src/plugins/ImmutableMap.js @@ -16,11 +16,8 @@ const printImmutable = require('./lib/printImmutable'); const IS_MAP = '@@__IMMUTABLE_MAP__@@'; const IS_ORDERED = '@@__IMMUTABLE_ORDERED__@@'; -const isMap = (maybeMap: any) => !!maybeMap[IS_MAP]; -const isNotOrdered = (maybeOrdered: any) => !maybeOrdered[IS_ORDERED]; - const test = (maybeMap: any) => - !!(maybeMap && isMap(maybeMap) && isNotOrdered(maybeMap)); + !!(maybeMap && maybeMap[IS_MAP] && !maybeMap[IS_ORDERED]); const print = ( val: any, diff --git a/packages/pretty-format/src/plugins/ImmutableOrderedMap.js b/packages/pretty-format/src/plugins/ImmutableOrderedMap.js index ebee70bed7a5..2ff1f1d84ecc 100644 --- a/packages/pretty-format/src/plugins/ImmutableOrderedMap.js +++ b/packages/pretty-format/src/plugins/ImmutableOrderedMap.js @@ -16,11 +16,8 @@ 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, diff --git a/packages/pretty-format/src/plugins/ImmutableOrderedSet.js b/packages/pretty-format/src/plugins/ImmutableOrderedSet.js index 051fea2ceef3..823134aacac3 100644 --- a/packages/pretty-format/src/plugins/ImmutableOrderedSet.js +++ b/packages/pretty-format/src/plugins/ImmutableOrderedSet.js @@ -16,11 +16,8 @@ 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, diff --git a/packages/pretty-format/src/plugins/ImmutableSet.js b/packages/pretty-format/src/plugins/ImmutableSet.js index 45448bee303d..6a404b21d47f 100644 --- a/packages/pretty-format/src/plugins/ImmutableSet.js +++ b/packages/pretty-format/src/plugins/ImmutableSet.js @@ -16,11 +16,8 @@ const printImmutable = require('./lib/printImmutable'); const IS_SET = '@@__IMMUTABLE_SET__@@'; const IS_ORDERED = '@@__IMMUTABLE_ORDERED__@@'; -const isSet = (maybeSet: any) => !!maybeSet[IS_SET]; -const isNotOrdered = (maybeOrdered: any) => !maybeOrdered[IS_ORDERED]; - const test = (maybeSet: any) => - !!(maybeSet && isSet(maybeSet) && isNotOrdered(maybeSet)); + !!(maybeSet && maybeSet[IS_SET] && !maybeSet[IS_ORDERED]); const print = ( val: any,