From 5b5d6820538636c140c19f26fe69d49b1ab89cc3 Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Thu, 3 Aug 2017 13:28:32 -0400 Subject: [PATCH 01/10] Replace print with serialize in Immutable plugins --- .../src/__tests__/expect_util.js | 10 +- .../src/__tests__/immutable.test.js | 373 ++++++++++++++---- .../src/__tests__/pretty_format.test.js | 45 ++- .../src/plugins/immutable_list.js | 24 +- .../src/plugins/immutable_map.js | 24 +- .../src/plugins/immutable_ordered_map.js | 33 +- .../src/plugins/immutable_ordered_set.js | 33 +- .../src/plugins/immutable_record.js | 33 +- .../src/plugins/immutable_set.js | 24 +- .../src/plugins/immutable_stack.js | 33 +- .../src/plugins/lib/print_immutable.js | 133 +++++-- 11 files changed, 553 insertions(+), 212 deletions(-) diff --git a/packages/pretty-format/src/__tests__/expect_util.js b/packages/pretty-format/src/__tests__/expect_util.js index 5cb623fa163b..f99f06ea4b6d 100644 --- a/packages/pretty-format/src/__tests__/expect_util.js +++ b/packages/pretty-format/src/__tests__/expect_util.js @@ -10,21 +10,21 @@ 'use strict'; -import type {Plugins} from 'types/PrettyFormat'; +import type {OptionsReceived, Plugins} from 'types/PrettyFormat'; const diff = require('jest-diff'); const prettyFormat = require('../'); module.exports = { getPrettyPrint: (plugins: Plugins) => - function(received: any, expected: any, opts: any) { + function(received: any, expected: any, options?: OptionsReceived) { const prettyFormatted = prettyFormat( received, Object.assign( - { + ({ plugins, - }, - opts, + }: OptionsReceived), + options, ), ); const pass = prettyFormatted === expected; diff --git a/packages/pretty-format/src/__tests__/immutable.test.js b/packages/pretty-format/src/__tests__/immutable.test.js index 54b88a0bd70b..67e465f0545c 100644 --- a/packages/pretty-format/src/__tests__/immutable.test.js +++ b/packages/pretty-format/src/__tests__/immutable.test.js @@ -12,13 +12,12 @@ import React from 'react'; import Immutable from 'immutable'; -import ReactElementPlugin from '../plugins/react_element'; -import ReactTestComponentPlugin from '../plugins/react_test_component'; -import ImmutablePlugins from '../plugins/immutable_plugins'; +import prettyFormat from '../'; +const {Immutable: ImmutablePlugins, ReactElement} = prettyFormat.plugins; import expectUtil from './expect_util'; const toPrettyPrintTo = expectUtil.getPrettyPrint( - [ReactElementPlugin, ReactTestComponentPlugin].concat(ImmutablePlugins), + [ReactElement].concat(ImmutablePlugins), ); const expect = global.expect; @@ -97,19 +96,19 @@ describe('Immutable.OrderedSet plugin', () => { ); }); - it('supports React components {min: true}', () => { - const reactComponent = React.createElement('Mouse', null, 'Hello World'); + it('supports React elements {min: true}', () => { + const reactElement = React.createElement('Mouse', null, 'Hello World'); expect( - Immutable.OrderedSet([reactComponent, reactComponent]), + Immutable.OrderedSet([reactElement, reactElement]), ).toPrettyPrintTo('Immutable.OrderedSet [Hello World]', { min: true, }); }); - it('supports React components {min: false}', () => { - const reactComponent = React.createElement('Mouse', null, 'Hello World'); + it('supports React elements {min: false}', () => { + const reactElement = React.createElement('Mouse', null, 'Hello World'); expect( - Immutable.OrderedSet([reactComponent, reactComponent]), + Immutable.OrderedSet([reactElement, reactElement]), ).toPrettyPrintTo( 'Immutable.OrderedSet [\n \n Hello World\n ,\n]', {min: false}, @@ -180,19 +179,19 @@ describe('Immutable.List plugin', () => { ); }); - it('supports React components {min: true}', () => { - const reactComponent = React.createElement('Mouse', null, 'Hello World'); + it('supports React elements {min: true}', () => { + const reactElement = React.createElement('Mouse', null, 'Hello World'); expect( - Immutable.List([reactComponent, reactComponent]), + Immutable.List([reactElement, reactElement]), ).toPrettyPrintTo( 'Immutable.List [Hello World, Hello World]', {min: true}, ); }); - it('supports React components {min: false}', () => { - const reactComponent = React.createElement('Mouse', null, 'Hello World'); - expect(Immutable.List([reactComponent, reactComponent])).toPrettyPrintTo( + it('supports React elements {min: false}', () => { + const reactElement = React.createElement('Mouse', null, 'Hello World'); + expect(Immutable.List([reactElement, reactElement])).toPrettyPrintTo( 'Immutable.List [\n \n Hello World\n ,\n \n Hello World\n ,\n]', ); }); @@ -263,19 +262,19 @@ describe('Immutable.Stack plugin', () => { ); }); - it('supports React components {min: true}', () => { - const reactComponent = React.createElement('Mouse', null, 'Hello World'); + it('supports React elements {min: true}', () => { + const reactElement = React.createElement('Mouse', null, 'Hello World'); expect( - Immutable.Stack([reactComponent, reactComponent]), + Immutable.Stack([reactElement, reactElement]), ).toPrettyPrintTo( 'Immutable.Stack [Hello World, Hello World]', {min: true}, ); }); - it('supports React components {min: false}', () => { - const reactComponent = React.createElement('Mouse', null, 'Hello World'); - expect(Immutable.Stack([reactComponent, reactComponent])).toPrettyPrintTo( + it('supports React elements {min: false}', () => { + const reactElement = React.createElement('Mouse', null, 'Hello World'); + expect(Immutable.Stack([reactElement, reactElement])).toPrettyPrintTo( 'Immutable.Stack [\n \n Hello World\n ,\n \n Hello World\n ,\n]', ); }); @@ -342,18 +341,18 @@ describe('Immutable.Set plugin', () => { ); }); - it('supports React components {min: true}', () => { - const reactComponent = React.createElement('Mouse', null, 'Hello World'); + it('supports React elements {min: true}', () => { + const reactElement = React.createElement('Mouse', null, 'Hello World'); expect( - Immutable.Set([reactComponent, reactComponent]), + Immutable.Set([reactElement, reactElement]), ).toPrettyPrintTo('Immutable.Set [Hello World]', { min: true, }); }); - it('supports React components {min: false}', () => { - const reactComponent = React.createElement('Mouse', null, 'Hello World'); - expect(Immutable.Set([reactComponent, reactComponent])).toPrettyPrintTo( + it('supports React elements {min: false}', () => { + const reactElement = React.createElement('Mouse', null, 'Hello World'); + expect(Immutable.Set([reactElement, reactElement])).toPrettyPrintTo( 'Immutable.Set [\n \n Hello World\n ,\n]', ); }); @@ -371,7 +370,7 @@ describe('Immutable.Map plugin', () => { }); it('supports an object with single key', () => { - expect(Immutable.Map({a: 1})).toPrettyPrintTo('Immutable.Map {a: 1}', { + expect(Immutable.Map({a: 1})).toPrettyPrintTo('Immutable.Map {"a": 1}', { min: true, }); }); @@ -379,45 +378,43 @@ describe('Immutable.Map plugin', () => { it('supports an object with multiple keys {min: true}', () => { expect( Immutable.Map({a: 1, b: 2, c: 3}), - ).toPrettyPrintTo('Immutable.Map {a: 1, b: 2, c: 3}', {min: true}); + ).toPrettyPrintTo('Immutable.Map {"a": 1, "b": 2, "c": 3}', {min: true}); }); it('supports an object with multiple keys {min: false}', () => { expect(Immutable.Map({a: 1, b: 2, c: 3})).toPrettyPrintTo( - 'Immutable.Map {\n a: 1,\n b: 2,\n c: 3,\n}', + 'Immutable.Map {\n "a": 1,\n "b": 2,\n "c": 3,\n}', ); }); it('supports object elements {min: true}', () => { expect( Immutable.Map({key: {a: 1, b: 2, c: 3}}), - ).toPrettyPrintTo('Immutable.Map {key: {"a": 1, "b": 2, "c": 3}}', { + ).toPrettyPrintTo('Immutable.Map {"key": {"a": 1, "b": 2, "c": 3}}', { min: true, }); }); it('supports object elements {min: false}', () => { expect(Immutable.Map({key: {a: 1, b: 2, c: 3}})).toPrettyPrintTo( - 'Immutable.Map {\n key: Object {\n "a": 1,\n "b": 2,\n "c": 3,\n },\n}', + 'Immutable.Map {\n "key": Object {\n "a": 1,\n "b": 2,\n "c": 3,\n },\n}', ); }); - it('supports React components {min: true}', () => { - const reactComponent = React.createElement('Mouse', null, 'Hello World'); + it('supports React elements {min: true}', () => { + const reactElement = React.createElement('Mouse', null, 'Hello World'); expect( - Immutable.Map({a: reactComponent, b: reactComponent}), + Immutable.Map({a: reactElement, b: reactElement}), ).toPrettyPrintTo( - 'Immutable.Map {a: Hello World, b: Hello World}', + 'Immutable.Map {"a": Hello World, "b": Hello World}', {min: true}, ); }); - it('supports React components {min: false}', () => { - const reactComponent = React.createElement('Mouse', null, 'Hello World'); - expect( - Immutable.Map({a: reactComponent, b: reactComponent}), - ).toPrettyPrintTo( - 'Immutable.Map {\n a: \n Hello World\n ,\n b: \n Hello World\n ,\n}', + it('supports React elements {min: false}', () => { + const reactElement = React.createElement('Mouse', null, 'Hello World'); + expect(Immutable.Map({a: reactElement, b: reactElement})).toPrettyPrintTo( + 'Immutable.Map {\n "a": \n Hello World\n ,\n "b": \n Hello World\n ,\n}', ); }); }); @@ -438,53 +435,105 @@ describe('Immutable.OrderedMap plugin', () => { it('supports an object with single key', () => { expect( Immutable.OrderedMap({a: 1}), - ).toPrettyPrintTo('Immutable.OrderedMap {a: 1}', {min: true}); + ).toPrettyPrintTo('Immutable.OrderedMap {"a": 1}', {min: true}); }); it('supports an object with multiple keys {min: true}', () => { expect( Immutable.OrderedMap({a: 1, b: 2, c: 3}), - ).toPrettyPrintTo('Immutable.OrderedMap {a: 1, b: 2, c: 3}', {min: true}); + ).toPrettyPrintTo('Immutable.OrderedMap {"a": 1, "b": 2, "c": 3}', { + min: true, + }); }); it('supports an object with multiple keys {min: false}', () => { expect(Immutable.OrderedMap({a: 1, b: 2, c: 3})).toPrettyPrintTo( - 'Immutable.OrderedMap {\n a: 1,\n b: 2,\n c: 3,\n}', + 'Immutable.OrderedMap {\n "a": 1,\n "b": 2,\n "c": 3,\n}', ); }); it('supports object elements {min: true}', () => { expect( Immutable.OrderedMap({key: {a: 1, b: 2, c: 3}}), - ).toPrettyPrintTo('Immutable.OrderedMap {key: {"a": 1, "b": 2, "c": 3}}', { - min: true, - }); + ).toPrettyPrintTo( + 'Immutable.OrderedMap {"key": {"a": 1, "b": 2, "c": 3}}', + { + min: true, + }, + ); }); it('supports object elements {min: false}', () => { expect(Immutable.OrderedMap({key: {a: 1, b: 2, c: 3}})).toPrettyPrintTo( - 'Immutable.OrderedMap {\n key: Object {\n "a": 1,\n "b": 2,\n "c": 3,\n },\n}', + 'Immutable.OrderedMap {\n "key": Object {\n "a": 1,\n "b": 2,\n "c": 3,\n },\n}', ); }); - it('supports React components {min: true}', () => { - const reactComponent = React.createElement('Mouse', null, 'Hello World'); + it('supports React elements {min: true}', () => { + const reactElement = React.createElement('Mouse', null, 'Hello World'); expect( - Immutable.OrderedMap({a: reactComponent, b: reactComponent}), + Immutable.OrderedMap({a: reactElement, b: reactElement}), ).toPrettyPrintTo( - 'Immutable.OrderedMap {a: Hello World, b: Hello World}', + 'Immutable.OrderedMap {"a": Hello World, "b": Hello World}', {min: true}, ); }); - it('supports React components {min: false}', () => { - const reactComponent = React.createElement('Mouse', null, 'Hello World'); + it('supports React elements {min: false}', () => { + const reactElement = React.createElement('Mouse', null, 'Hello World'); expect( - Immutable.OrderedMap({a: reactComponent, b: reactComponent}), + Immutable.OrderedMap({a: reactElement, b: reactElement}), ).toPrettyPrintTo( - 'Immutable.OrderedMap {\n a: \n Hello World\n ,\n b: \n Hello World\n ,\n}', + 'Immutable.OrderedMap {\n "a": \n Hello World\n ,\n "b": \n Hello World\n ,\n}', ); }); + + it('supports non-string keys', () => { + const val = Immutable.OrderedMap([ + ['multi\nline\nstring', 'multiline string'], + [false, 'boolean'], + ['false', 'string'], + [0, 'number'], + ['0', 'string'], + [null, 'null'], + ['null', 'string'], + [undefined, 'undefined'], + ['undefined', 'string'], + [Symbol('description'), 'symbol'], + ['Symbol(description)', 'string'], + [['array', 'key'], 'array'], + [{key: 'value'}, 'object'], + [Immutable.Map({key: 'value'}), 'immutable map'], + ]); + const expected = [ + 'Immutable.OrderedMap {', + ' "multi', + 'line', + 'string": "multiline string",', + ' false: "boolean",', + ' "false": "string",', + ' 0: "number",', + ' "0": "string",', + ' null: "null",', + ' "null": "string",', + ' undefined: "undefined",', + ' "undefined": "string",', + ' Symbol(description): "symbol",', + ' "Symbol(description)": "string",', + ' Array [', + ' "array",', + ' "key",', + ' ]: "array",', + ' Object {', + ' "key": "value",', + ' }: "object",', + ' Immutable.Map {', + ' "key": "value",', + ' }: "immutable map",', + '}', + ].join('\n'); + expect(val).toPrettyPrintTo(expected); + }); }); describe('Immutable.Record plugin', () => { @@ -507,7 +556,7 @@ describe('Immutable.Record plugin', () => { it('supports a record with descriptive name', () => { const ABRecord = Immutable.Record({a: 1, b: 2}, 'ABRecord'); - expect(ABRecord()).toPrettyPrintTo('Immutable.ABRecord {a: 1, b: 2}', { + expect(ABRecord()).toPrettyPrintTo('Immutable.ABRecord {"a": 1, "b": 2}', { min: true, }); }); @@ -515,7 +564,7 @@ describe('Immutable.Record plugin', () => { it('supports a record without descriptive name', () => { const ABRecord = Immutable.Record({a: 1, b: 2}); - expect(ABRecord()).toPrettyPrintTo('Immutable.Record {a: 1, b: 2}', { + expect(ABRecord()).toPrettyPrintTo('Immutable.Record {"a": 1, "b": 2}', { min: true, }); }); @@ -525,14 +574,14 @@ describe('Immutable.Record plugin', () => { expect( ABRecord({a: 3, b: 4}), - ).toPrettyPrintTo('Immutable.ABRecord {a: 3, b: 4}', {min: true}); + ).toPrettyPrintTo('Immutable.ABRecord {"a": 3, "b": 4}', {min: true}); }); it('supports a record with values {min: false}', () => { const ABRecord = Immutable.Record({a: 1, b: 2}, 'ABRecord'); expect(ABRecord({a: 3, b: 4})).toPrettyPrintTo( - 'Immutable.ABRecord {\n a: 3,\n b: 4,\n}', + 'Immutable.ABRecord {\n "a": 3,\n "b": 4,\n}', ); }); @@ -544,9 +593,12 @@ describe('Immutable.Record plugin', () => { expect( ABRecord(), - ).toPrettyPrintTo('Immutable.ABRecord {a: Immutable.Map {c: 1}, b: 2}', { - min: true, - }); + ).toPrettyPrintTo( + 'Immutable.ABRecord {"a": Immutable.Map {"c": 1}, "b": 2}', + { + min: true, + }, + ); }); it('supports a record with Map value {min: false}', () => { @@ -556,7 +608,7 @@ describe('Immutable.Record plugin', () => { ); expect(ABRecord()).toPrettyPrintTo( - 'Immutable.ABRecord {\n a: Immutable.Map {\n c: 1,\n },\n b: 2,\n}', + 'Immutable.ABRecord {\n "a": Immutable.Map {\n "c": 1,\n },\n "b": 2,\n}', ); }); @@ -567,7 +619,7 @@ describe('Immutable.Record plugin', () => { expect( ABRecord(), ).toPrettyPrintTo( - 'Immutable.ABRecord {a: Immutable.CDRecord {c: 3, d: 4}, b: 2}', + 'Immutable.ABRecord {"a": Immutable.CDRecord {"c": 3, "d": 4}, "b": 2}', {min: true}, ); }); @@ -577,7 +629,192 @@ describe('Immutable.Record plugin', () => { const ABRecord = Immutable.Record({a: CDRecord(), b: 2}, 'ABRecord'); expect(ABRecord()).toPrettyPrintTo( - 'Immutable.ABRecord {\n a: Immutable.CDRecord {\n c: 3,\n d: 4,\n },\n b: 2,\n}', + 'Immutable.ABRecord {\n "a": Immutable.CDRecord {\n "c": 3,\n "d": 4,\n },\n "b": 2,\n}', + ); + }); +}); + +describe('indentation of heterogeneous collections', () => { + // Don’t interpret tests that pretty-format and plugins are compatible + // as recommendation to compose immutable and non-immutable collections. + test('empty Immutable.List as child of Object', () => { + const val = { + filter: 'all', + todos: Immutable.List([]), + }; + expect(val).toPrettyPrintTo( + [ + 'Object {', + ' "filter": "all",', + ' "todos": Immutable.List [', + ' ],', + '}', + ].join('\n'), ); }); + test('empty Immutable.Map as child of Array', () => { + const val = [Immutable.Map({})]; + expect(val).toPrettyPrintTo( + ['Array [', ' Immutable.Map {', ' },', ']'].join('\n'), + ); + }); + + test('non-empty Array as child of Immutable.Map', () => { + const val = Immutable.Map({ + filter: 'completed', + todos: [ + Immutable.Map({ + completed: true, + text: 'Replace print with serialize', + }), + ], + }); + expect(val).toPrettyPrintTo( + [ + 'Immutable.Map {', + ' "filter": "completed",', + ' "todos": Array [', + ' Immutable.Map {', + ' "completed": true,', + ' "text": "Replace print with serialize",', + ' },', + ' ],', + '}', + ].join('\n'), + ); + }); + test('non-empty Object as child of Immutable.List', () => { + const val = Immutable.List([ + { + completed: true, + text: 'Replace print with serialize', + }, + ]); + expect(val).toPrettyPrintTo( + [ + 'Immutable.List [', + ' Object {', + ' "completed": true,', + ' "text": "Replace print with serialize",', + ' },', + ']', + ].join('\n'), + ); + }); +}); + +describe('indent option', () => { + const val = Immutable.Map({ + filter: 'completed', + todos: Immutable.List([ + Immutable.Map({ + completed: true, + text: 'Replace print with serialize', + }), + Immutable.Map({ + completed: false, + text: 'Return if depth exceeds max', + }), + ]), + }); + const expected = [ + 'Immutable.Map {', + ' "filter": "completed",', + ' "todos": Immutable.List [', + ' Immutable.Map {', + ' "completed": true,', + ' "text": "Replace print with serialize",', + ' },', + ' Immutable.Map {', + ' "completed": false,', + ' "text": "Return if depth exceeds max",', + ' },', + ' ],', + '}', + ].join('\n'); + test('default implicit: 2 spaces', () => { + expect(val).toPrettyPrintTo(expected); + }); + test('default explicit: 2 spaces', () => { + expect(val).toPrettyPrintTo(expected, {indent: 2}); + }); + + // Tests assume that no strings in val contain multiple adjacent spaces! + test('non-default: 0 spaces', () => { + const indent = 0; + expect(val).toPrettyPrintTo(expected.replace(/ {2}/g, ' '.repeat(indent)), { + indent, + }); + }); + test('non-default: 4 spaces', () => { + const indent = 4; + expect(val).toPrettyPrintTo(expected.replace(/ {2}/g, ' '.repeat(indent)), { + indent, + }); + }); +}); + +describe('maxDepth option', () => { + // Don’t interpret tests that pretty-format and plugins are compatible + // as recommendation to compose immutable and non-immutable collections. + test('Immutable.List as child of Object', () => { + const val = { + // ++depth === 1 + filter: 'all', + todos: Immutable.List([ + Immutable.Map({ + completed: true, + text: 'Return if depth exceeds max', + }), + ]), + }; + const expected = [ + 'Object {', + ' "filter": "all",', + ' "todos": [Immutable.List],', + '}', + ].join('\n'); + expect(val).toPrettyPrintTo(expected, {maxDepth: 1}); + }); + test('Immutable.Map as child of Array', () => { + const val = [ + // ++depth === 1 + Immutable.Map({ + completed: false, + text: 'Return if depth exceeds max', + }), + ]; + const expected = ['Array [', ' [Immutable.Map],', ']'].join('\n'); + expect(val).toPrettyPrintTo(expected, {maxDepth: 1}); + }); + + test('Immutable.Map as descendants in immutable collection', () => { + const val = Immutable.Map({ + // ++depth === 1 + filter: 'uncompleted', + todos: Immutable.List([ + // ++depth === 2 + Immutable.Map({ + // ++depth === 3 + completed: true, + text: 'Replace print with serialize', + }), + Immutable.Map({ + // ++depth === 3 + completed: true, + text: 'Return if depth exceeds max', + }), + ]), + }); + const expected = [ + 'Immutable.Map {', + ' "filter": "uncompleted",', + ' "todos": Immutable.List [', + ' [Immutable.Map],', + ' [Immutable.Map],', + ' ],', + '}', + ].join('\n'); + expect(val).toPrettyPrintTo(expected, {maxDepth: 2}); + }); }); diff --git a/packages/pretty-format/src/__tests__/pretty_format.test.js b/packages/pretty-format/src/__tests__/pretty_format.test.js index 0cd01cd16772..e32dccd513cd 100644 --- a/packages/pretty-format/src/__tests__/pretty_format.test.js +++ b/packages/pretty-format/src/__tests__/pretty_format.test.js @@ -162,11 +162,46 @@ describe('prettyFormat()', () => { }); it('prints a map with non-string keys', () => { - const val = new Map(); - val.set({prop: 'value'}, {prop: 'value'}); - expect(prettyFormat(val)).toEqual( - 'Map {\n Object {\n "prop": "value",\n } => Object {\n "prop": "value",\n },\n}', - ); + const val = new Map([ + ['multi\nline\nstring', 'multiline string'], + [false, 'boolean'], + ['false', 'string'], + [0, 'number'], + ['0', 'string'], + [null, 'null'], + ['null', 'string'], + [undefined, 'undefined'], + ['undefined', 'string'], + [Symbol('description'), 'symbol'], + ['Symbol(description)', 'string'], + [['array', 'key'], 'array'], + [{key: 'value'}, 'object'], + ]); + const expected = [ + 'Map {', + ' "multi', + 'line', + 'string" => "multiline string",', + ' false => "boolean",', + ' "false" => "string",', + ' 0 => "number",', + ' "0" => "string",', + ' null => "null",', + ' "null" => "string",', + ' undefined => "undefined",', + ' "undefined" => "string",', + ' Symbol(description) => "symbol",', + ' "Symbol(description)" => "string",', + ' Array [', + ' "array",', + ' "key",', + ' ] => "array",', + ' Object {', + ' "key": "value",', + ' } => "object",', + '}', + ].join('\n'); + expect(prettyFormat(val)).toEqual(expected); }); it('prints NaN', () => { diff --git a/packages/pretty-format/src/plugins/immutable_list.js b/packages/pretty-format/src/plugins/immutable_list.js index e4d9cd0c41c3..c84ac228bf08 100644 --- a/packages/pretty-format/src/plugins/immutable_list.js +++ b/packages/pretty-format/src/plugins/immutable_list.js @@ -8,25 +8,21 @@ * @flow */ -import type { - Colors, - Indent, - PluginOptions, - Print, - Plugin, -} from 'types/PrettyFormat'; +import type {Config, NewPlugin, Printer, Refs} from 'types/PrettyFormat'; import printImmutable from './lib/print_immutable'; const IS_LIST = '@@__IMMUTABLE_LIST__@@'; export const test = (maybeList: any) => !!(maybeList && maybeList[IS_LIST]); -export const print = ( +export const serialize = ( val: any, - print: Print, - indent: Indent, - opts: PluginOptions, - colors: Colors, -) => printImmutable(val, print, indent, opts, colors, 'List', false); + config: Config, + indentation: string, + depth: number, + refs: Refs, + printer: Printer, +) => + printImmutable(val, config, indentation, depth, refs, printer, 'List', false); -export default ({print, test}: Plugin); +export default ({serialize, test}: NewPlugin); diff --git a/packages/pretty-format/src/plugins/immutable_map.js b/packages/pretty-format/src/plugins/immutable_map.js index 7a9ebfb85e62..2f2e73d3fab1 100644 --- a/packages/pretty-format/src/plugins/immutable_map.js +++ b/packages/pretty-format/src/plugins/immutable_map.js @@ -8,13 +8,7 @@ * @flow */ -import type { - Colors, - Indent, - PluginOptions, - Print, - Plugin, -} from 'types/PrettyFormat'; +import type {Config, NewPlugin, Printer, Refs} from 'types/PrettyFormat'; import printImmutable from './lib/print_immutable'; @@ -23,12 +17,14 @@ const IS_ORDERED = '@@__IMMUTABLE_ORDERED__@@'; export const test = (maybeMap: any) => !!(maybeMap && maybeMap[IS_MAP] && !maybeMap[IS_ORDERED]); -export const print = ( +export const serialize = ( val: any, - print: Print, - indent: Indent, - opts: PluginOptions, - colors: Colors, -) => printImmutable(val, print, indent, opts, colors, 'Map', true); + config: Config, + indentation: string, + depth: number, + refs: Refs, + printer: Printer, +) => + printImmutable(val, config, indentation, depth, refs, printer, 'Map', true); -export default ({print, test}: Plugin); +export default ({serialize, test}: NewPlugin); diff --git a/packages/pretty-format/src/plugins/immutable_ordered_map.js b/packages/pretty-format/src/plugins/immutable_ordered_map.js index 212e0eba5889..b18c22fca2a8 100644 --- a/packages/pretty-format/src/plugins/immutable_ordered_map.js +++ b/packages/pretty-format/src/plugins/immutable_ordered_map.js @@ -8,13 +8,7 @@ * @flow */ -import type { - Colors, - Indent, - PluginOptions, - Print, - Plugin, -} from 'types/PrettyFormat'; +import type {Config, NewPlugin, Printer, Refs} from 'types/PrettyFormat'; import printImmutable from './lib/print_immutable'; @@ -23,12 +17,23 @@ const IS_ORDERED = '@@__IMMUTABLE_ORDERED__@@'; export const test = (maybeOrderedMap: any) => maybeOrderedMap && maybeOrderedMap[IS_MAP] && maybeOrderedMap[IS_ORDERED]; -export const print = ( +export const serialize = ( val: any, - print: Print, - indent: Indent, - opts: PluginOptions, - colors: Colors, -) => printImmutable(val, print, indent, opts, colors, 'OrderedMap', true); + config: Config, + indentation: string, + depth: number, + refs: Refs, + printer: Printer, +) => + printImmutable( + val, + config, + indentation, + depth, + refs, + printer, + 'OrderedMap', + true, + ); -export default ({print, test}: Plugin); +export default ({serialize, test}: NewPlugin); diff --git a/packages/pretty-format/src/plugins/immutable_ordered_set.js b/packages/pretty-format/src/plugins/immutable_ordered_set.js index cddc9d77b991..f5c0d2e32aab 100644 --- a/packages/pretty-format/src/plugins/immutable_ordered_set.js +++ b/packages/pretty-format/src/plugins/immutable_ordered_set.js @@ -8,13 +8,7 @@ * @flow */ -import type { - Colors, - Indent, - PluginOptions, - Print, - Plugin, -} from 'types/PrettyFormat'; +import type {Config, NewPlugin, Printer, Refs} from 'types/PrettyFormat'; import printImmutable from './lib/print_immutable'; @@ -23,12 +17,23 @@ const IS_ORDERED = '@@__IMMUTABLE_ORDERED__@@'; export const test = (maybeOrderedSet: any) => maybeOrderedSet && maybeOrderedSet[IS_SET] && maybeOrderedSet[IS_ORDERED]; -export const print = ( +export const serialize = ( val: any, - print: Print, - indent: Indent, - opts: PluginOptions, - colors: Colors, -) => printImmutable(val, print, indent, opts, colors, 'OrderedSet', false); + config: Config, + indentation: string, + depth: number, + refs: Refs, + printer: Printer, +) => + printImmutable( + val, + config, + indentation, + depth, + refs, + printer, + 'OrderedSet', + false, + ); -export default ({print, test}: Plugin); +export default ({serialize, test}: NewPlugin); diff --git a/packages/pretty-format/src/plugins/immutable_record.js b/packages/pretty-format/src/plugins/immutable_record.js index 8a4de33e57ed..14fd4b5b824d 100644 --- a/packages/pretty-format/src/plugins/immutable_record.js +++ b/packages/pretty-format/src/plugins/immutable_record.js @@ -8,13 +8,7 @@ * @flow */ -import type { - Colors, - Indent, - PluginOptions, - Print, - Plugin, -} from 'types/PrettyFormat'; +import type {Config, NewPlugin, Printer, Refs} from 'types/PrettyFormat'; import printImmutable from './lib/print_immutable'; @@ -22,12 +16,23 @@ const IS_RECORD = '@@__IMMUTABLE_RECORD__@@'; export const test = (maybeRecord: any) => !!(maybeRecord && maybeRecord[IS_RECORD]); -export const print = ( +export const serialize = ( val: any, - print: Print, - indent: Indent, - opts: PluginOptions, - colors: Colors, -) => printImmutable(val, print, indent, opts, colors, 'Record', true); + config: Config, + indentation: string, + depth: number, + refs: Refs, + printer: Printer, +) => + printImmutable( + val, + config, + indentation, + depth, + refs, + printer, + 'Record', + true, + ); -export default ({print, test}: Plugin); +export default ({serialize, test}: NewPlugin); diff --git a/packages/pretty-format/src/plugins/immutable_set.js b/packages/pretty-format/src/plugins/immutable_set.js index c5c66d94cc7d..c76ee3017e13 100644 --- a/packages/pretty-format/src/plugins/immutable_set.js +++ b/packages/pretty-format/src/plugins/immutable_set.js @@ -8,13 +8,7 @@ * @flow */ -import type { - Colors, - Indent, - PluginOptions, - Print, - Plugin, -} from 'types/PrettyFormat'; +import type {Config, NewPlugin, Printer, Refs} from 'types/PrettyFormat'; import printImmutable from './lib/print_immutable'; @@ -23,12 +17,14 @@ const IS_ORDERED = '@@__IMMUTABLE_ORDERED__@@'; export const test = (maybeSet: any) => !!(maybeSet && maybeSet[IS_SET] && !maybeSet[IS_ORDERED]); -export const print = ( +export const serialize = ( val: any, - print: Print, - indent: Indent, - opts: PluginOptions, - colors: Colors, -) => printImmutable(val, print, indent, opts, colors, 'Set', false); + config: Config, + indentation: string, + depth: number, + refs: Refs, + printer: Printer, +) => + printImmutable(val, config, indentation, depth, refs, printer, 'Set', false); -export default ({print, test}: Plugin); +export default ({serialize, test}: NewPlugin); diff --git a/packages/pretty-format/src/plugins/immutable_stack.js b/packages/pretty-format/src/plugins/immutable_stack.js index 00aed6010ce0..fe30a0f2007c 100644 --- a/packages/pretty-format/src/plugins/immutable_stack.js +++ b/packages/pretty-format/src/plugins/immutable_stack.js @@ -8,25 +8,30 @@ * @flow */ -import type { - Colors, - Indent, - PluginOptions, - Print, - Plugin, -} from 'types/PrettyFormat'; +import type {Config, NewPlugin, Printer, Refs} from 'types/PrettyFormat'; import printImmutable from './lib/print_immutable'; const IS_STACK = '@@__IMMUTABLE_STACK__@@'; export const test = (maybeStack: any) => !!(maybeStack && maybeStack[IS_STACK]); -export const print = ( +export const serialize = ( val: any, - print: Print, - indent: Indent, - opts: PluginOptions, - colors: Colors, -) => printImmutable(val, print, indent, opts, colors, 'Stack', false); + config: Config, + indentation: string, + depth: number, + refs: Refs, + printer: Printer, +) => + printImmutable( + val, + config, + indentation, + depth, + refs, + printer, + 'Stack', + false, + ); -export default ({print, test}: Plugin); +export default ({serialize, test}: NewPlugin); diff --git a/packages/pretty-format/src/plugins/lib/print_immutable.js b/packages/pretty-format/src/plugins/lib/print_immutable.js index 26f62ecfb3c7..a033c71b38bf 100644 --- a/packages/pretty-format/src/plugins/lib/print_immutable.js +++ b/packages/pretty-format/src/plugins/lib/print_immutable.js @@ -8,58 +8,119 @@ * @flow */ -import type {Colors, Indent, PluginOptions, Print} from 'types/PrettyFormat'; +import type {Config, Printer, Refs} from 'types/PrettyFormat'; + +import {printIteratorEntries, printIteratorValues} from '../../collections'; const IMMUTABLE_NAMESPACE = 'Immutable.'; const SPACE = ' '; -const addKey = (isMap: boolean, key: any) => (isMap ? key + ': ' : ''); - -const addFinalEdgeSpacing = (length: number, edgeSpacing: string) => - length !== 0 ? edgeSpacing : ''; - const printImmutable = ( val: any, - print: Print, - indent: Indent, - opts: PluginOptions, - colors: Colors, + config: Config, + indentation: string, + depth: number, + refs: Refs, + printer: Printer, immutableDataStructureName: string, isMap: boolean, ): string => { - const [openTag, closeTag] = isMap ? ['{', '}'] : ['[', ']']; - const fullStructureName = val._name || immutableDataStructureName; + const name = IMMUTABLE_NAMESPACE + (val._name || immutableDataStructureName); + if (++depth > config.maxDepth) { + return '[' + name + ']'; + } - let result = - IMMUTABLE_NAMESPACE + - fullStructureName + + if (isMap) { + if (Array.isArray(val._keys)) { + return ( + name + + SPACE + + '{' + + (val._keys.length !== 0 + ? printRecordProperties( + val, + config, + indentation, + depth, + refs, + printer, + ) + : config.spacingOuter + indentation) + + '}' + ); + } + return ( + name + + SPACE + + '{' + + (val.size !== 0 + ? printIteratorEntries( + val.entries(), + config, + indentation, + depth, + refs, + printer, + ) + : config.spacingOuter + indentation) + + '}' + ); + } + return ( + name + SPACE + - openTag + - opts.edgeSpacing; + '[' + + (val.size !== 0 + ? printIteratorValues( + val.values(), + config, + indentation, + depth, + refs, + printer, + ) + : config.spacingOuter + indentation) + + ']' + ); +}; - const immutableArray = []; +// Return properties of a record +// with spacing, indentation, and comma +// without surrounding braces +export function printRecordProperties( + val: Object, + config: Config, + indentation: string, + depth: number, + refs: Refs, + printer: Printer, +): string { + let result = ''; + const keys = val._keys; - const pushToImmutableArray = (item: any, key: string) => { - immutableArray.push(indent(addKey(isMap, key) + print(item))); - }; + if (keys.length) { + result += config.spacingOuter; - if (Array.isArray(val._keys)) { - // if we have a record, we can not iterate on it directly - val._keys.forEach(key => pushToImmutableArray(val.get(key), key)); - } else { - val.forEach((item, key) => pushToImmutableArray(item, key)); - } + const indentationNext = indentation + config.indent; + + for (let i = 0; i < keys.length; i++) { + const key = keys[i]; + const name = printer(key, config, indentationNext, depth, refs); + const value = printer(val.get(key), config, indentationNext, depth, refs); + + result += indentationNext + name + ': ' + value; + + if (i < keys.length - 1) { + result += ',' + config.spacingInner; + } else if (!config.min) { + result += ','; + } + } - result += immutableArray.join(',' + opts.spacing); - if (!opts.min && immutableArray.length !== 0) { - result += ','; + result += config.spacingOuter + indentation; } - return ( - result + - addFinalEdgeSpacing(immutableArray.length, opts.edgeSpacing) + - closeTag - ); -}; + return result; +} export default printImmutable; From ab6b3d332e41c93c1bba5dfad845ad0558a07bc2 Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Thu, 3 Aug 2017 13:39:31 -0400 Subject: [PATCH 02/10] Delete multiline string key to avoid conflict with 4183 --- packages/pretty-format/src/__tests__/immutable.test.js | 4 ---- packages/pretty-format/src/__tests__/pretty_format.test.js | 4 ---- 2 files changed, 8 deletions(-) diff --git a/packages/pretty-format/src/__tests__/immutable.test.js b/packages/pretty-format/src/__tests__/immutable.test.js index 67e465f0545c..b0f054b545da 100644 --- a/packages/pretty-format/src/__tests__/immutable.test.js +++ b/packages/pretty-format/src/__tests__/immutable.test.js @@ -490,7 +490,6 @@ describe('Immutable.OrderedMap plugin', () => { it('supports non-string keys', () => { const val = Immutable.OrderedMap([ - ['multi\nline\nstring', 'multiline string'], [false, 'boolean'], ['false', 'string'], [0, 'number'], @@ -507,9 +506,6 @@ describe('Immutable.OrderedMap plugin', () => { ]); const expected = [ 'Immutable.OrderedMap {', - ' "multi', - 'line', - 'string": "multiline string",', ' false: "boolean",', ' "false": "string",', ' 0: "number",', diff --git a/packages/pretty-format/src/__tests__/pretty_format.test.js b/packages/pretty-format/src/__tests__/pretty_format.test.js index e32dccd513cd..d5faa065aa94 100644 --- a/packages/pretty-format/src/__tests__/pretty_format.test.js +++ b/packages/pretty-format/src/__tests__/pretty_format.test.js @@ -163,7 +163,6 @@ describe('prettyFormat()', () => { it('prints a map with non-string keys', () => { const val = new Map([ - ['multi\nline\nstring', 'multiline string'], [false, 'boolean'], ['false', 'string'], [0, 'number'], @@ -179,9 +178,6 @@ describe('prettyFormat()', () => { ]); const expected = [ 'Map {', - ' "multi', - 'line', - 'string" => "multiline string",', ' false => "boolean",', ' "false" => "string",', ' 0 => "number",', From 257dc40abd6f66be6d24f9d822c058b8a9c4f0ea Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Thu, 3 Aug 2017 16:18:05 -0400 Subject: [PATCH 03/10] Update 3 snapshot tests --- .../src/__tests__/__snapshots__/spy_matchers.test.js.snap | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/jest-matchers/src/__tests__/__snapshots__/spy_matchers.test.js.snap b/packages/jest-matchers/src/__tests__/__snapshots__/spy_matchers.test.js.snap index 75a482ba8acf..7d1c41967f82 100644 --- a/packages/jest-matchers/src/__tests__/__snapshots__/spy_matchers.test.js.snap +++ b/packages/jest-matchers/src/__tests__/__snapshots__/spy_matchers.test.js.snap @@ -20,7 +20,7 @@ exports[`lastCalledWith works with Immutable.js objects 1`] = ` "expect(jest.fn()).not.lastCalledWith(expected) Expected mock function to not have been last called with: - [Immutable.Map {a: {\\"b\\": \\"c\\"}}, Immutable.Map {a: {\\"b\\": \\"c\\"}}]" + [Immutable.Map {\\"a\\": {\\"b\\": \\"c\\"}}, Immutable.Map {\\"a\\": {\\"b\\": \\"c\\"}}]" `; exports[`lastCalledWith works with Map 1`] = ` @@ -239,7 +239,7 @@ exports[`toHaveBeenCalledWith works with Immutable.js objects 1`] = ` "expect(jest.fn()).not.toHaveBeenCalledWith(expected) Expected mock function not to have been called with: - [Immutable.Map {a: {\\"b\\": \\"c\\"}}, Immutable.Map {a: {\\"b\\": \\"c\\"}}]" + [Immutable.Map {\\"a\\": {\\"b\\": \\"c\\"}}, Immutable.Map {\\"a\\": {\\"b\\": \\"c\\"}}]" `; exports[`toHaveBeenCalledWith works with Map 1`] = ` @@ -322,7 +322,7 @@ exports[`toHaveBeenLastCalledWith works with Immutable.js objects 1`] = ` "expect(jest.fn()).not.toHaveBeenLastCalledWith(expected) Expected mock function to not have been last called with: - [Immutable.Map {a: {\\"b\\": \\"c\\"}}, Immutable.Map {a: {\\"b\\": \\"c\\"}}]" + [Immutable.Map {\\"a\\": {\\"b\\": \\"c\\"}}, Immutable.Map {\\"a\\": {\\"b\\": \\"c\\"}}]" `; exports[`toHaveBeenLastCalledWith works with Map 1`] = ` From 02260c39269d75748c45f0504e9627a88553c48e Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Thu, 3 Aug 2017 16:18:56 -0400 Subject: [PATCH 04/10] Replace printRecordProperties with getRecordIterator --- packages/pretty-format/src/collections.js | 5 +- .../src/plugins/lib/print_immutable.js | 50 +++++-------------- 2 files changed, 17 insertions(+), 38 deletions(-) diff --git a/packages/pretty-format/src/collections.js b/packages/pretty-format/src/collections.js index 82fa62fecb1a..926d00f786de 100644 --- a/packages/pretty-format/src/collections.js +++ b/packages/pretty-format/src/collections.js @@ -20,7 +20,10 @@ const isSymbol = key => // with spacing, indentation, and comma // without surrounding punctuation (for example, braces) export function printIteratorEntries( - iterator: Iterator<[any, any]>, + // Flow 0.51.0: property `@@iterator` of $Iterator not found in Object + // To allow simplistic getRecordIterator in print_immutable.js + // replaced Iterator<[any, any]> with any + iterator: any, config: Config, indentation: string, depth: number, diff --git a/packages/pretty-format/src/plugins/lib/print_immutable.js b/packages/pretty-format/src/plugins/lib/print_immutable.js index a033c71b38bf..a44499123d39 100644 --- a/packages/pretty-format/src/plugins/lib/print_immutable.js +++ b/packages/pretty-format/src/plugins/lib/print_immutable.js @@ -37,8 +37,8 @@ const printImmutable = ( SPACE + '{' + (val._keys.length !== 0 - ? printRecordProperties( - val, + ? printIteratorEntries( + getRecordIterator(val), config, indentation, depth, @@ -84,43 +84,19 @@ const printImmutable = ( ); }; -// Return properties of a record -// with spacing, indentation, and comma -// without surrounding braces -export function printRecordProperties( - val: Object, - config: Config, - indentation: string, - depth: number, - refs: Refs, - printer: Printer, -): string { - let result = ''; +function getRecordIterator(val) { const keys = val._keys; - - if (keys.length) { - result += config.spacingOuter; - - const indentationNext = indentation + config.indent; - - for (let i = 0; i < keys.length; i++) { - const key = keys[i]; - const name = printer(key, config, indentationNext, depth, refs); - const value = printer(val.get(key), config, indentationNext, depth, refs); - - result += indentationNext + name + ': ' + value; - - if (i < keys.length - 1) { - result += ',' + config.spacingInner; - } else if (!config.min) { - result += ','; + let i = 0; + return { + next() { + const done = i >= keys.length; + if (done) { + return {done: true}; } - } - - result += config.spacingOuter + indentation; - } - - return result; + const key = keys[i++]; + return {done: false, value: [key, val.get(key)]}; + }, + }; } export default printImmutable; From 83ea633d52edfda8294be74e8a04de0062d30055 Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Fri, 4 Aug 2017 08:38:44 -0400 Subject: [PATCH 05/10] Export 3 print functions from renamed immutable.js file --- packages/pretty-format/src/collections.js | 2 +- .../src/plugins/immutable_list.js | 4 +- .../src/plugins/immutable_map.js | 4 +- .../src/plugins/immutable_ordered_map.js | 5 +- .../src/plugins/immutable_ordered_set.js | 5 +- .../src/plugins/immutable_record.js | 14 +-- .../src/plugins/immutable_set.js | 4 +- .../src/plugins/immutable_stack.js | 13 +- .../src/plugins/lib/immutable.js | 115 ++++++++++++++++++ .../src/plugins/lib/print_immutable.js | 102 ---------------- 10 files changed, 130 insertions(+), 138 deletions(-) create mode 100644 packages/pretty-format/src/plugins/lib/immutable.js delete mode 100644 packages/pretty-format/src/plugins/lib/print_immutable.js diff --git a/packages/pretty-format/src/collections.js b/packages/pretty-format/src/collections.js index 926d00f786de..c3f7980930b6 100644 --- a/packages/pretty-format/src/collections.js +++ b/packages/pretty-format/src/collections.js @@ -21,7 +21,7 @@ const isSymbol = key => // without surrounding punctuation (for example, braces) export function printIteratorEntries( // Flow 0.51.0: property `@@iterator` of $Iterator not found in Object - // To allow simplistic getRecordIterator in print_immutable.js + // To allow simplistic getRecordIterator in immutable.js // replaced Iterator<[any, any]> with any iterator: any, config: Config, diff --git a/packages/pretty-format/src/plugins/immutable_list.js b/packages/pretty-format/src/plugins/immutable_list.js index c84ac228bf08..eaee04d6106e 100644 --- a/packages/pretty-format/src/plugins/immutable_list.js +++ b/packages/pretty-format/src/plugins/immutable_list.js @@ -10,7 +10,7 @@ import type {Config, NewPlugin, Printer, Refs} from 'types/PrettyFormat'; -import printImmutable from './lib/print_immutable'; +import {printImmutableValues} from './lib/immutable'; const IS_LIST = '@@__IMMUTABLE_LIST__@@'; export const test = (maybeList: any) => !!(maybeList && maybeList[IS_LIST]); @@ -23,6 +23,6 @@ export const serialize = ( refs: Refs, printer: Printer, ) => - printImmutable(val, config, indentation, depth, refs, printer, 'List', false); + printImmutableValues(val, config, indentation, depth, refs, printer, 'List'); export default ({serialize, test}: NewPlugin); diff --git a/packages/pretty-format/src/plugins/immutable_map.js b/packages/pretty-format/src/plugins/immutable_map.js index 2f2e73d3fab1..307262b2363d 100644 --- a/packages/pretty-format/src/plugins/immutable_map.js +++ b/packages/pretty-format/src/plugins/immutable_map.js @@ -10,7 +10,7 @@ import type {Config, NewPlugin, Printer, Refs} from 'types/PrettyFormat'; -import printImmutable from './lib/print_immutable'; +import {printImmutableEntries} from './lib/immutable'; const IS_MAP = '@@__IMMUTABLE_MAP__@@'; const IS_ORDERED = '@@__IMMUTABLE_ORDERED__@@'; @@ -25,6 +25,6 @@ export const serialize = ( refs: Refs, printer: Printer, ) => - printImmutable(val, config, indentation, depth, refs, printer, 'Map', true); + printImmutableEntries(val, config, indentation, depth, refs, printer, 'Map'); export default ({serialize, test}: NewPlugin); diff --git a/packages/pretty-format/src/plugins/immutable_ordered_map.js b/packages/pretty-format/src/plugins/immutable_ordered_map.js index b18c22fca2a8..3c867a8cee2f 100644 --- a/packages/pretty-format/src/plugins/immutable_ordered_map.js +++ b/packages/pretty-format/src/plugins/immutable_ordered_map.js @@ -10,7 +10,7 @@ import type {Config, NewPlugin, Printer, Refs} from 'types/PrettyFormat'; -import printImmutable from './lib/print_immutable'; +import {printImmutableEntries} from './lib/immutable'; const IS_MAP = '@@__IMMUTABLE_MAP__@@'; const IS_ORDERED = '@@__IMMUTABLE_ORDERED__@@'; @@ -25,7 +25,7 @@ export const serialize = ( refs: Refs, printer: Printer, ) => - printImmutable( + printImmutableEntries( val, config, indentation, @@ -33,7 +33,6 @@ export const serialize = ( refs, printer, 'OrderedMap', - true, ); export default ({serialize, test}: NewPlugin); diff --git a/packages/pretty-format/src/plugins/immutable_ordered_set.js b/packages/pretty-format/src/plugins/immutable_ordered_set.js index f5c0d2e32aab..adde8ad0da59 100644 --- a/packages/pretty-format/src/plugins/immutable_ordered_set.js +++ b/packages/pretty-format/src/plugins/immutable_ordered_set.js @@ -10,7 +10,7 @@ import type {Config, NewPlugin, Printer, Refs} from 'types/PrettyFormat'; -import printImmutable from './lib/print_immutable'; +import {printImmutableValues} from './lib/immutable'; const IS_SET = '@@__IMMUTABLE_SET__@@'; const IS_ORDERED = '@@__IMMUTABLE_ORDERED__@@'; @@ -25,7 +25,7 @@ export const serialize = ( refs: Refs, printer: Printer, ) => - printImmutable( + printImmutableValues( val, config, indentation, @@ -33,7 +33,6 @@ export const serialize = ( refs, printer, 'OrderedSet', - false, ); export default ({serialize, test}: NewPlugin); diff --git a/packages/pretty-format/src/plugins/immutable_record.js b/packages/pretty-format/src/plugins/immutable_record.js index 14fd4b5b824d..fabb617e2113 100644 --- a/packages/pretty-format/src/plugins/immutable_record.js +++ b/packages/pretty-format/src/plugins/immutable_record.js @@ -10,7 +10,7 @@ import type {Config, NewPlugin, Printer, Refs} from 'types/PrettyFormat'; -import printImmutable from './lib/print_immutable'; +import {printImmutableRecord} from './lib/immutable'; const IS_RECORD = '@@__IMMUTABLE_RECORD__@@'; export const test = (maybeRecord: any) => @@ -23,16 +23,6 @@ export const serialize = ( depth: number, refs: Refs, printer: Printer, -) => - printImmutable( - val, - config, - indentation, - depth, - refs, - printer, - 'Record', - true, - ); +) => printImmutableRecord(val, config, indentation, depth, refs, printer); export default ({serialize, test}: NewPlugin); diff --git a/packages/pretty-format/src/plugins/immutable_set.js b/packages/pretty-format/src/plugins/immutable_set.js index c76ee3017e13..e81a51b0e620 100644 --- a/packages/pretty-format/src/plugins/immutable_set.js +++ b/packages/pretty-format/src/plugins/immutable_set.js @@ -10,7 +10,7 @@ import type {Config, NewPlugin, Printer, Refs} from 'types/PrettyFormat'; -import printImmutable from './lib/print_immutable'; +import {printImmutableValues} from './lib/immutable'; const IS_SET = '@@__IMMUTABLE_SET__@@'; const IS_ORDERED = '@@__IMMUTABLE_ORDERED__@@'; @@ -25,6 +25,6 @@ export const serialize = ( refs: Refs, printer: Printer, ) => - printImmutable(val, config, indentation, depth, refs, printer, 'Set', false); + printImmutableValues(val, config, indentation, depth, refs, printer, 'Set'); export default ({serialize, test}: NewPlugin); diff --git a/packages/pretty-format/src/plugins/immutable_stack.js b/packages/pretty-format/src/plugins/immutable_stack.js index fe30a0f2007c..fe517e71e0e3 100644 --- a/packages/pretty-format/src/plugins/immutable_stack.js +++ b/packages/pretty-format/src/plugins/immutable_stack.js @@ -10,7 +10,7 @@ import type {Config, NewPlugin, Printer, Refs} from 'types/PrettyFormat'; -import printImmutable from './lib/print_immutable'; +import {printImmutableValues} from './lib/immutable'; const IS_STACK = '@@__IMMUTABLE_STACK__@@'; export const test = (maybeStack: any) => !!(maybeStack && maybeStack[IS_STACK]); @@ -23,15 +23,6 @@ export const serialize = ( refs: Refs, printer: Printer, ) => - printImmutable( - val, - config, - indentation, - depth, - refs, - printer, - 'Stack', - false, - ); + printImmutableValues(val, config, indentation, depth, refs, printer, 'Stack'); export default ({serialize, test}: NewPlugin); diff --git a/packages/pretty-format/src/plugins/lib/immutable.js b/packages/pretty-format/src/plugins/lib/immutable.js new file mode 100644 index 000000000000..a8fa74116e79 --- /dev/null +++ b/packages/pretty-format/src/plugins/lib/immutable.js @@ -0,0 +1,115 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @flow + */ + +import type {Config, Printer, Refs} from 'types/PrettyFormat'; + +import {printIteratorEntries, printIteratorValues} from '../../collections'; + +const IMMUTABLE_NAMESPACE = 'Immutable.'; +const SPACE = ' '; + +export const printImmutableEntries = ( + val: any, + config: Config, + indentation: string, + depth: number, + refs: Refs, + printer: Printer, + type: string, +): string => { + const name = IMMUTABLE_NAMESPACE + type; + return ++depth > config.maxDepth + ? '[' + name + ']' + : name + + SPACE + + '{' + + (val.size !== 0 + ? printIteratorEntries( + val.entries(), + config, + indentation, + depth, + refs, + printer, + ) + : config.spacingOuter + indentation) + + '}'; +}; + +// Return an iterator for Immutable Record as if it had an entries method. +const getRecordEntries = val => { + let i = 0; + return { + next() { + if (i < val._keys.length) { + const key = val._keys[i++]; + return {done: false, value: [key, val.get(key)]}; + } + return {done: true}; + }, + }; +}; + +// _name property is defined only for an Immutable Record instance +// which was constructed with a second optional descriptive name arg +export const printImmutableRecord = ( + val: any, + config: Config, + indentation: string, + depth: number, + refs: Refs, + printer: Printer, +): string => { + const name = IMMUTABLE_NAMESPACE + (val._name || 'Record'); + return ++depth > config.maxDepth + ? '[' + name + ']' + : name + + SPACE + + '{' + + (val._keys.length !== 0 + ? printIteratorEntries( + getRecordEntries(val), + config, + indentation, + depth, + refs, + printer, + ) + : config.spacingOuter + indentation) + + '}'; +}; + +export const printImmutableValues = ( + val: any, + config: Config, + indentation: string, + depth: number, + refs: Refs, + printer: Printer, + type: string, +): string => { + const name = IMMUTABLE_NAMESPACE + type; + return ++depth > config.maxDepth + ? '[' + name + ']' + : name + + SPACE + + '[' + + (val.size !== 0 + ? printIteratorValues( + val.values(), + config, + indentation, + depth, + refs, + printer, + ) + : config.spacingOuter + indentation) + + ']'; +}; diff --git a/packages/pretty-format/src/plugins/lib/print_immutable.js b/packages/pretty-format/src/plugins/lib/print_immutable.js deleted file mode 100644 index a44499123d39..000000000000 --- a/packages/pretty-format/src/plugins/lib/print_immutable.js +++ /dev/null @@ -1,102 +0,0 @@ -/** - * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - * @flow - */ - -import type {Config, Printer, Refs} from 'types/PrettyFormat'; - -import {printIteratorEntries, printIteratorValues} from '../../collections'; - -const IMMUTABLE_NAMESPACE = 'Immutable.'; -const SPACE = ' '; - -const printImmutable = ( - val: any, - config: Config, - indentation: string, - depth: number, - refs: Refs, - printer: Printer, - immutableDataStructureName: string, - isMap: boolean, -): string => { - const name = IMMUTABLE_NAMESPACE + (val._name || immutableDataStructureName); - if (++depth > config.maxDepth) { - return '[' + name + ']'; - } - - if (isMap) { - if (Array.isArray(val._keys)) { - return ( - name + - SPACE + - '{' + - (val._keys.length !== 0 - ? printIteratorEntries( - getRecordIterator(val), - config, - indentation, - depth, - refs, - printer, - ) - : config.spacingOuter + indentation) + - '}' - ); - } - return ( - name + - SPACE + - '{' + - (val.size !== 0 - ? printIteratorEntries( - val.entries(), - config, - indentation, - depth, - refs, - printer, - ) - : config.spacingOuter + indentation) + - '}' - ); - } - return ( - name + - SPACE + - '[' + - (val.size !== 0 - ? printIteratorValues( - val.values(), - config, - indentation, - depth, - refs, - printer, - ) - : config.spacingOuter + indentation) + - ']' - ); -}; - -function getRecordIterator(val) { - const keys = val._keys; - let i = 0; - return { - next() { - const done = i >= keys.length; - if (done) { - return {done: true}; - } - const key = keys[i++]; - return {done: false, value: [key, val.get(key)]}; - }, - }; -} - -export default printImmutable; From 88ec7e8169cd53c1bf2d1ea2ccb6d5fccd16e3aa Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Fri, 4 Aug 2017 08:42:13 -0400 Subject: [PATCH 06/10] Move comment --- packages/pretty-format/src/plugins/lib/immutable.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/pretty-format/src/plugins/lib/immutable.js b/packages/pretty-format/src/plugins/lib/immutable.js index a8fa74116e79..984326c54d9a 100644 --- a/packages/pretty-format/src/plugins/lib/immutable.js +++ b/packages/pretty-format/src/plugins/lib/immutable.js @@ -57,8 +57,6 @@ const getRecordEntries = val => { }; }; -// _name property is defined only for an Immutable Record instance -// which was constructed with a second optional descriptive name arg export const printImmutableRecord = ( val: any, config: Config, @@ -67,6 +65,8 @@ export const printImmutableRecord = ( refs: Refs, printer: Printer, ): string => { + // _name property is defined only for an Immutable Record instance + // which was constructed with a second optional descriptive name arg const name = IMMUTABLE_NAMESPACE + (val._name || 'Record'); return ++depth > config.maxDepth ? '[' + name + ']' From 076e952bda933f1710c708e7cf7bdff8a00a37ca Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Sat, 5 Aug 2017 14:28:37 -0400 Subject: [PATCH 07/10] Roll up immutable plugins --- .../src/__tests__/plugins.test.js | 2 +- .../src/__tests__/immutable.test.js | 46 ++++- packages/pretty-format/src/index.js | 2 +- .../pretty-format/src/plugins/immutable.js | 195 ++++++++++++++++++ .../src/plugins/immutable_list.js | 28 --- .../src/plugins/immutable_map.js | 30 --- .../src/plugins/immutable_ordered_map.js | 38 ---- .../src/plugins/immutable_ordered_set.js | 38 ---- .../src/plugins/immutable_plugins.js | 27 --- .../src/plugins/immutable_record.js | 28 --- .../src/plugins/immutable_set.js | 30 --- .../src/plugins/immutable_stack.js | 28 --- .../src/plugins/lib/immutable.js | 115 ----------- 13 files changed, 236 insertions(+), 371 deletions(-) create mode 100644 packages/pretty-format/src/plugins/immutable.js delete mode 100644 packages/pretty-format/src/plugins/immutable_list.js delete mode 100644 packages/pretty-format/src/plugins/immutable_map.js delete mode 100644 packages/pretty-format/src/plugins/immutable_ordered_map.js delete mode 100644 packages/pretty-format/src/plugins/immutable_ordered_set.js delete mode 100644 packages/pretty-format/src/plugins/immutable_plugins.js delete mode 100644 packages/pretty-format/src/plugins/immutable_record.js delete mode 100644 packages/pretty-format/src/plugins/immutable_set.js delete mode 100644 packages/pretty-format/src/plugins/immutable_stack.js delete mode 100644 packages/pretty-format/src/plugins/lib/immutable.js diff --git a/packages/jest-snapshot/src/__tests__/plugins.test.js b/packages/jest-snapshot/src/__tests__/plugins.test.js index ed463a0aa5f5..bde7bfaad3af 100644 --- a/packages/jest-snapshot/src/__tests__/plugins.test.js +++ b/packages/jest-snapshot/src/__tests__/plugins.test.js @@ -29,7 +29,7 @@ const testPath = names => { it('gets plugins', () => { const {getSerializers} = require('../plugins'); const plugins = getSerializers(); - expect(plugins.length).toBe(10); + expect(plugins.length).toBe(4); }); it('adds plugins from an empty array', () => testPath([])); diff --git a/packages/pretty-format/src/__tests__/immutable.test.js b/packages/pretty-format/src/__tests__/immutable.test.js index b0f054b545da..c875de0691da 100644 --- a/packages/pretty-format/src/__tests__/immutable.test.js +++ b/packages/pretty-format/src/__tests__/immutable.test.js @@ -23,7 +23,7 @@ const toPrettyPrintTo = expectUtil.getPrettyPrint( const expect = global.expect; expect.extend({toPrettyPrintTo}); -describe('Immutable.OrderedSet plugin', () => { +describe('Immutable.OrderedSet', () => { it('supports an empty collection {min: true}', () => { expect( Immutable.OrderedSet([]), @@ -116,7 +116,7 @@ describe('Immutable.OrderedSet plugin', () => { }); }); -describe('Immutable.List plugin', () => { +describe('Immutable.List', () => { it('supports an empty collection {min: true}', () => { expect(Immutable.List([])).toPrettyPrintTo('Immutable.List []', { min: true, @@ -197,7 +197,7 @@ describe('Immutable.List plugin', () => { }); }); -describe('Immutable.Stack plugin', () => { +describe('Immutable.Stack', () => { it('supports an empty collection {min: true}', () => { expect(Immutable.Stack([])).toPrettyPrintTo('Immutable.Stack []', { min: true, @@ -280,7 +280,7 @@ describe('Immutable.Stack plugin', () => { }); }); -describe('Immutable.Set plugin', () => { +describe('Immutable.Set', () => { it('supports an empty collection {min: true}', () => { expect(Immutable.Set([])).toPrettyPrintTo('Immutable.Set []', {min: true}); }); @@ -358,7 +358,7 @@ describe('Immutable.Set plugin', () => { }); }); -describe('Immutable.Map plugin', () => { +describe('Immutable.Map', () => { it('supports an empty collection {min: true}', () => { expect(Immutable.Map({})).toPrettyPrintTo('Immutable.Map {}', {min: true}); }); @@ -419,7 +419,7 @@ describe('Immutable.Map plugin', () => { }); }); -describe('Immutable.OrderedMap plugin', () => { +describe('Immutable.OrderedMap', () => { it('supports an empty collection {min: true}', () => { expect( Immutable.OrderedMap({}), @@ -532,7 +532,7 @@ describe('Immutable.OrderedMap plugin', () => { }); }); -describe('Immutable.Record plugin', () => { +describe('Immutable.Record', () => { it('supports an empty record {min: true}', () => { const ABRecord = Immutable.Record({}, 'ABRecord'); @@ -814,3 +814,35 @@ describe('maxDepth option', () => { expect(val).toPrettyPrintTo(expected, {maxDepth: 2}); }); }); + +describe('Immutable.Seq', () => { + const expected = '[Immutable.Seq]'; + it('supports an empty sequence from array {min: true}', () => { + expect(Immutable.Seq([])).toPrettyPrintTo(expected, {min: true}); + }); + it('supports an empty sequence from array {min: false}', () => { + expect(Immutable.Seq([])).toPrettyPrintTo(expected, {min: false}); + }); + it('supports a non-empty sequence from array {min: true}', () => { + expect(Immutable.Seq([0, 1, 2])).toPrettyPrintTo(expected, {min: true}); + }); + it('supports a non-empty sequence from array {min: false}', () => { + expect(Immutable.Seq([0, 1, 2])).toPrettyPrintTo(expected, {min: false}); + }); + it('supports an empty sequence from object {min: true}', () => { + expect(Immutable.Seq({})).toPrettyPrintTo(expected, {min: true}); + }); + it('supports an empty sequence from object {min: false}', () => { + expect(Immutable.Seq({})).toPrettyPrintTo(expected, {min: false}); + }); + it('supports a non-empty sequence from object {min: true}', () => { + expect(Immutable.Seq({key: 'value'})).toPrettyPrintTo(expected, { + min: true, + }); + }); + it('supports a non-empty sequence from object {min: false}', () => { + expect(Immutable.Seq({key: 'value'})).toPrettyPrintTo(expected, { + min: false, + }); + }); +}); diff --git a/packages/pretty-format/src/index.js b/packages/pretty-format/src/index.js index d4b6647f5367..ade05a5211c0 100644 --- a/packages/pretty-format/src/index.js +++ b/packages/pretty-format/src/index.js @@ -32,7 +32,7 @@ import { import AsymmetricMatcher from './plugins/asymmetric_matcher'; import ConvertAnsi from './plugins/convert_ansi'; import HTMLElement from './plugins/html_element'; -import Immutable from './plugins/immutable_plugins'; +import Immutable from './plugins/immutable'; import ReactElement from './plugins/react_element'; import ReactTestComponent from './plugins/react_test_component'; diff --git a/packages/pretty-format/src/plugins/immutable.js b/packages/pretty-format/src/plugins/immutable.js new file mode 100644 index 000000000000..23b02be8cc8a --- /dev/null +++ b/packages/pretty-format/src/plugins/immutable.js @@ -0,0 +1,195 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @flow + */ + +import type {Config, Printer, NewPlugin, Refs} from 'types/PrettyFormat'; + +import {printIteratorEntries, printIteratorValues} from '../collections'; + +// SENTINEL constants are from https://github.com/facebook/immutable-js + +const IS_ITERABLE_SENTINEL = '@@__IMMUTABLE_ITERABLE__@@'; +const IS_RECORD_SENTINEL = '@@__IMMUTABLE_RECORD__@@'; // v4 or later + +const IS_LIST_SENTINEL = '@@__IMMUTABLE_LIST__@@'; +const IS_MAP_SENTINEL = '@@__IMMUTABLE_MAP__@@'; +const IS_SEQ_SENTINEL = '@@__IMMUTABLE_SEQ__@@'; +const IS_SET_SENTINEL = '@@__IMMUTABLE_SET__@@'; +const IS_STACK_SENTINEL = '@@__IMMUTABLE_STACK__@@'; + +const IS_ORDERED_SENTINEL = '@@__IMMUTABLE_ORDERED__@@'; + +const getName = name => 'Immutable.' + name; +const SPACE = ' '; + +const printImmutableEntries = ( + val: any, + config: Config, + indentation: string, + depth: number, + refs: Refs, + printer: Printer, + type: string, +): string => + ++depth > config.maxDepth + ? '[' + getName(type) + ']' + : getName(type) + + SPACE + + '{' + + (val.size !== 0 + ? printIteratorEntries( + val.entries(), + config, + indentation, + depth, + refs, + printer, + ) + : config.spacingOuter + indentation) + + '}'; + +// Return an iterator for Immutable Record in v4 or later. +const getRecordEntries = val => { + let i = 0; + return { + next() { + if (i < val._keys.length) { + const key = val._keys[i++]; + return {done: false, value: [key, val.get(key)]}; + } + return {done: true}; + }, + }; +}; + +const printImmutableRecord = ( + val: any, + config: Config, + indentation: string, + depth: number, + refs: Refs, + printer: Printer, +): string => { + // _name property is defined only for an Immutable Record instance + // which was constructed with a second optional descriptive name arg + const name = getName(val._name || 'Record'); + const size = Array.isArray(val._keys) ? val._keys.length : val.size; + const entries = typeof Array.isArray(val._keys) + ? getRecordEntries(val) // v4 or later + : val.entries(); // v3 or earlier + return ++depth > config.maxDepth + ? '[' + name + ']' + : name + + SPACE + + '{' + + (size !== 0 + ? printIteratorEntries( + entries, + config, + indentation, + depth, + refs, + printer, + ) + : config.spacingOuter + indentation) + + '}'; +}; + +const printImmutableValues = ( + val: any, + config: Config, + indentation: string, + depth: number, + refs: Refs, + printer: Printer, + type: string, +): string => + ++depth > config.maxDepth + ? '[' + getName(type) + ']' + : getName(type) + + SPACE + + '[' + + (val.size !== 0 + ? printIteratorValues( + val.values(), + config, + indentation, + depth, + refs, + printer, + ) + : config.spacingOuter + indentation) + + ']'; + +export const serialize = ( + val: any, + config: Config, + indentation: string, + depth: number, + refs: Refs, + printer: Printer, +): string => { + if (val[IS_SEQ_SENTINEL]) { + return '[' + getName('Seq') + ']'; + } + + if (val[IS_MAP_SENTINEL]) { + return printImmutableEntries( + val, + config, + indentation, + depth, + refs, + printer, + val[IS_ORDERED_SENTINEL] ? 'OrderedMap' : 'Map', + ); + } + + if (val[IS_LIST_SENTINEL]) { + return printImmutableValues( + val, + config, + indentation, + depth, + refs, + printer, + 'List', + ); + } + if (val[IS_SET_SENTINEL]) { + return printImmutableValues( + val, + config, + indentation, + depth, + refs, + printer, + val[IS_ORDERED_SENTINEL] ? 'OrderedSet' : 'Set', + ); + } + if (val[IS_STACK_SENTINEL]) { + return printImmutableValues( + val, + config, + indentation, + depth, + refs, + printer, + 'Stack', + ); + } + + // For compatibility with immutable v3 and v4, let record be the default. + return printImmutableRecord(val, config, indentation, depth, refs, printer); +}; + +export const test = (val: any) => + val && (val[IS_ITERABLE_SENTINEL] || val[IS_RECORD_SENTINEL]); + +export default ({serialize, test}: NewPlugin); diff --git a/packages/pretty-format/src/plugins/immutable_list.js b/packages/pretty-format/src/plugins/immutable_list.js deleted file mode 100644 index eaee04d6106e..000000000000 --- a/packages/pretty-format/src/plugins/immutable_list.js +++ /dev/null @@ -1,28 +0,0 @@ -/** - * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - * @flow - */ - -import type {Config, NewPlugin, Printer, Refs} from 'types/PrettyFormat'; - -import {printImmutableValues} from './lib/immutable'; - -const IS_LIST = '@@__IMMUTABLE_LIST__@@'; -export const test = (maybeList: any) => !!(maybeList && maybeList[IS_LIST]); - -export const serialize = ( - val: any, - config: Config, - indentation: string, - depth: number, - refs: Refs, - printer: Printer, -) => - printImmutableValues(val, config, indentation, depth, refs, printer, 'List'); - -export default ({serialize, test}: NewPlugin); diff --git a/packages/pretty-format/src/plugins/immutable_map.js b/packages/pretty-format/src/plugins/immutable_map.js deleted file mode 100644 index 307262b2363d..000000000000 --- a/packages/pretty-format/src/plugins/immutable_map.js +++ /dev/null @@ -1,30 +0,0 @@ -/** - * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - * @flow - */ - -import type {Config, NewPlugin, Printer, Refs} from 'types/PrettyFormat'; - -import {printImmutableEntries} from './lib/immutable'; - -const IS_MAP = '@@__IMMUTABLE_MAP__@@'; -const IS_ORDERED = '@@__IMMUTABLE_ORDERED__@@'; -export const test = (maybeMap: any) => - !!(maybeMap && maybeMap[IS_MAP] && !maybeMap[IS_ORDERED]); - -export const serialize = ( - val: any, - config: Config, - indentation: string, - depth: number, - refs: Refs, - printer: Printer, -) => - printImmutableEntries(val, config, indentation, depth, refs, printer, 'Map'); - -export default ({serialize, test}: NewPlugin); diff --git a/packages/pretty-format/src/plugins/immutable_ordered_map.js b/packages/pretty-format/src/plugins/immutable_ordered_map.js deleted file mode 100644 index 3c867a8cee2f..000000000000 --- a/packages/pretty-format/src/plugins/immutable_ordered_map.js +++ /dev/null @@ -1,38 +0,0 @@ -/** - * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - * @flow - */ - -import type {Config, NewPlugin, Printer, Refs} from 'types/PrettyFormat'; - -import {printImmutableEntries} from './lib/immutable'; - -const IS_MAP = '@@__IMMUTABLE_MAP__@@'; -const IS_ORDERED = '@@__IMMUTABLE_ORDERED__@@'; -export const test = (maybeOrderedMap: any) => - maybeOrderedMap && maybeOrderedMap[IS_MAP] && maybeOrderedMap[IS_ORDERED]; - -export const serialize = ( - val: any, - config: Config, - indentation: string, - depth: number, - refs: Refs, - printer: Printer, -) => - printImmutableEntries( - val, - config, - indentation, - depth, - refs, - printer, - 'OrderedMap', - ); - -export default ({serialize, test}: NewPlugin); diff --git a/packages/pretty-format/src/plugins/immutable_ordered_set.js b/packages/pretty-format/src/plugins/immutable_ordered_set.js deleted file mode 100644 index adde8ad0da59..000000000000 --- a/packages/pretty-format/src/plugins/immutable_ordered_set.js +++ /dev/null @@ -1,38 +0,0 @@ -/** - * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - * @flow - */ - -import type {Config, NewPlugin, Printer, Refs} from 'types/PrettyFormat'; - -import {printImmutableValues} from './lib/immutable'; - -const IS_SET = '@@__IMMUTABLE_SET__@@'; -const IS_ORDERED = '@@__IMMUTABLE_ORDERED__@@'; -export const test = (maybeOrderedSet: any) => - maybeOrderedSet && maybeOrderedSet[IS_SET] && maybeOrderedSet[IS_ORDERED]; - -export const serialize = ( - val: any, - config: Config, - indentation: string, - depth: number, - refs: Refs, - printer: Printer, -) => - printImmutableValues( - val, - config, - indentation, - depth, - refs, - printer, - 'OrderedSet', - ); - -export default ({serialize, test}: NewPlugin); diff --git a/packages/pretty-format/src/plugins/immutable_plugins.js b/packages/pretty-format/src/plugins/immutable_plugins.js deleted file mode 100644 index 29a074e2a004..000000000000 --- a/packages/pretty-format/src/plugins/immutable_plugins.js +++ /dev/null @@ -1,27 +0,0 @@ -/** - * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - * @flow - */ - -import ImmutableList from './immutable_list'; -import ImmutableSet from './immutable_set'; -import ImmutableMap from './immutable_map'; -import ImmutableStack from './immutable_stack'; -import ImmutableOrderedSet from './immutable_ordered_set'; -import ImmutableOrderedMap from './immutable_ordered_map'; -import ImmutableRecord from './immutable_record'; - -export default [ - ImmutableList, - ImmutableSet, - ImmutableMap, - ImmutableStack, - ImmutableOrderedSet, - ImmutableOrderedMap, - ImmutableRecord, -]; diff --git a/packages/pretty-format/src/plugins/immutable_record.js b/packages/pretty-format/src/plugins/immutable_record.js deleted file mode 100644 index fabb617e2113..000000000000 --- a/packages/pretty-format/src/plugins/immutable_record.js +++ /dev/null @@ -1,28 +0,0 @@ -/** - * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - * @flow - */ - -import type {Config, NewPlugin, Printer, Refs} from 'types/PrettyFormat'; - -import {printImmutableRecord} from './lib/immutable'; - -const IS_RECORD = '@@__IMMUTABLE_RECORD__@@'; -export const test = (maybeRecord: any) => - !!(maybeRecord && maybeRecord[IS_RECORD]); - -export const serialize = ( - val: any, - config: Config, - indentation: string, - depth: number, - refs: Refs, - printer: Printer, -) => printImmutableRecord(val, config, indentation, depth, refs, printer); - -export default ({serialize, test}: NewPlugin); diff --git a/packages/pretty-format/src/plugins/immutable_set.js b/packages/pretty-format/src/plugins/immutable_set.js deleted file mode 100644 index e81a51b0e620..000000000000 --- a/packages/pretty-format/src/plugins/immutable_set.js +++ /dev/null @@ -1,30 +0,0 @@ -/** - * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - * @flow - */ - -import type {Config, NewPlugin, Printer, Refs} from 'types/PrettyFormat'; - -import {printImmutableValues} from './lib/immutable'; - -const IS_SET = '@@__IMMUTABLE_SET__@@'; -const IS_ORDERED = '@@__IMMUTABLE_ORDERED__@@'; -export const test = (maybeSet: any) => - !!(maybeSet && maybeSet[IS_SET] && !maybeSet[IS_ORDERED]); - -export const serialize = ( - val: any, - config: Config, - indentation: string, - depth: number, - refs: Refs, - printer: Printer, -) => - printImmutableValues(val, config, indentation, depth, refs, printer, 'Set'); - -export default ({serialize, test}: NewPlugin); diff --git a/packages/pretty-format/src/plugins/immutable_stack.js b/packages/pretty-format/src/plugins/immutable_stack.js deleted file mode 100644 index fe517e71e0e3..000000000000 --- a/packages/pretty-format/src/plugins/immutable_stack.js +++ /dev/null @@ -1,28 +0,0 @@ -/** - * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - * @flow - */ - -import type {Config, NewPlugin, Printer, Refs} from 'types/PrettyFormat'; - -import {printImmutableValues} from './lib/immutable'; - -const IS_STACK = '@@__IMMUTABLE_STACK__@@'; -export const test = (maybeStack: any) => !!(maybeStack && maybeStack[IS_STACK]); - -export const serialize = ( - val: any, - config: Config, - indentation: string, - depth: number, - refs: Refs, - printer: Printer, -) => - printImmutableValues(val, config, indentation, depth, refs, printer, 'Stack'); - -export default ({serialize, test}: NewPlugin); diff --git a/packages/pretty-format/src/plugins/lib/immutable.js b/packages/pretty-format/src/plugins/lib/immutable.js deleted file mode 100644 index 984326c54d9a..000000000000 --- a/packages/pretty-format/src/plugins/lib/immutable.js +++ /dev/null @@ -1,115 +0,0 @@ -/** - * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. - * - * This source code is licensed under the BSD-style license found in the - * LICENSE file in the root directory of this source tree. An additional grant - * of patent rights can be found in the PATENTS file in the same directory. - * - * @flow - */ - -import type {Config, Printer, Refs} from 'types/PrettyFormat'; - -import {printIteratorEntries, printIteratorValues} from '../../collections'; - -const IMMUTABLE_NAMESPACE = 'Immutable.'; -const SPACE = ' '; - -export const printImmutableEntries = ( - val: any, - config: Config, - indentation: string, - depth: number, - refs: Refs, - printer: Printer, - type: string, -): string => { - const name = IMMUTABLE_NAMESPACE + type; - return ++depth > config.maxDepth - ? '[' + name + ']' - : name + - SPACE + - '{' + - (val.size !== 0 - ? printIteratorEntries( - val.entries(), - config, - indentation, - depth, - refs, - printer, - ) - : config.spacingOuter + indentation) + - '}'; -}; - -// Return an iterator for Immutable Record as if it had an entries method. -const getRecordEntries = val => { - let i = 0; - return { - next() { - if (i < val._keys.length) { - const key = val._keys[i++]; - return {done: false, value: [key, val.get(key)]}; - } - return {done: true}; - }, - }; -}; - -export const printImmutableRecord = ( - val: any, - config: Config, - indentation: string, - depth: number, - refs: Refs, - printer: Printer, -): string => { - // _name property is defined only for an Immutable Record instance - // which was constructed with a second optional descriptive name arg - const name = IMMUTABLE_NAMESPACE + (val._name || 'Record'); - return ++depth > config.maxDepth - ? '[' + name + ']' - : name + - SPACE + - '{' + - (val._keys.length !== 0 - ? printIteratorEntries( - getRecordEntries(val), - config, - indentation, - depth, - refs, - printer, - ) - : config.spacingOuter + indentation) + - '}'; -}; - -export const printImmutableValues = ( - val: any, - config: Config, - indentation: string, - depth: number, - refs: Refs, - printer: Printer, - type: string, -): string => { - const name = IMMUTABLE_NAMESPACE + type; - return ++depth > config.maxDepth - ? '[' + name + ']' - : name + - SPACE + - '[' + - (val.size !== 0 - ? printIteratorValues( - val.values(), - config, - indentation, - depth, - refs, - printer, - ) - : config.spacingOuter + indentation) + - ']'; -}; From 4affd2c60255c6b4a0e174c5f1397e5dc60466b9 Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Sat, 5 Aug 2017 16:46:29 -0400 Subject: [PATCH 08/10] Replace getName with getImmutableName --- packages/pretty-format/src/plugins/immutable.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/pretty-format/src/plugins/immutable.js b/packages/pretty-format/src/plugins/immutable.js index 23b02be8cc8a..36fc2972a0e4 100644 --- a/packages/pretty-format/src/plugins/immutable.js +++ b/packages/pretty-format/src/plugins/immutable.js @@ -25,7 +25,7 @@ const IS_STACK_SENTINEL = '@@__IMMUTABLE_STACK__@@'; const IS_ORDERED_SENTINEL = '@@__IMMUTABLE_ORDERED__@@'; -const getName = name => 'Immutable.' + name; +const getImmutableName = name => 'Immutable.' + name; const SPACE = ' '; const printImmutableEntries = ( @@ -38,8 +38,8 @@ const printImmutableEntries = ( type: string, ): string => ++depth > config.maxDepth - ? '[' + getName(type) + ']' - : getName(type) + + ? '[' + getImmutableName(type) + ']' + : getImmutableName(type) + SPACE + '{' + (val.size !== 0 @@ -78,7 +78,7 @@ const printImmutableRecord = ( ): string => { // _name property is defined only for an Immutable Record instance // which was constructed with a second optional descriptive name arg - const name = getName(val._name || 'Record'); + const name = getImmutableName(val._name || 'Record'); const size = Array.isArray(val._keys) ? val._keys.length : val.size; const entries = typeof Array.isArray(val._keys) ? getRecordEntries(val) // v4 or later @@ -111,8 +111,8 @@ const printImmutableValues = ( type: string, ): string => ++depth > config.maxDepth - ? '[' + getName(type) + ']' - : getName(type) + + ? '[' + getImmutableName(type) + ']' + : getImmutableName(type) + SPACE + '[' + (val.size !== 0 @@ -136,7 +136,7 @@ export const serialize = ( printer: Printer, ): string => { if (val[IS_SEQ_SENTINEL]) { - return '[' + getName('Seq') + ']'; + return '[' + getImmutableName('Seq') + ']'; } if (val[IS_MAP_SENTINEL]) { From 248bc02ad664eeb87e127b7fe753b02e793355dc Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Sat, 5 Aug 2017 17:05:09 -0400 Subject: [PATCH 09/10] Move Seq after all other types except Record --- .../pretty-format/src/__tests__/immutable.test.js | 14 ++++++++++++++ packages/pretty-format/src/plugins/immutable.js | 8 ++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/packages/pretty-format/src/__tests__/immutable.test.js b/packages/pretty-format/src/__tests__/immutable.test.js index c875de0691da..f0e45b913f3f 100644 --- a/packages/pretty-format/src/__tests__/immutable.test.js +++ b/packages/pretty-format/src/__tests__/immutable.test.js @@ -845,4 +845,18 @@ describe('Immutable.Seq', () => { min: false, }); }); + it('supports a sequence from Immutable.Map', () => { + expect(Immutable.Seq(Immutable.Map({key: 'value'}))).toPrettyPrintTo( + expected, + ); + }); + it('supports a sequence from Immutable.List', () => { + expect(Immutable.Seq(Immutable.List([0, 1, 2]))).toPrettyPrintTo(expected); + }); + it('supports a sequence from Immutable.Set', () => { + expect(Immutable.Seq(Immutable.Set([0, 1, 2]))).toPrettyPrintTo(expected); + }); + it('supports a sequence from Immutable.Stack', () => { + expect(Immutable.Seq(Immutable.Stack([0, 1, 2]))).toPrettyPrintTo(expected); + }); }); diff --git a/packages/pretty-format/src/plugins/immutable.js b/packages/pretty-format/src/plugins/immutable.js index 36fc2972a0e4..e9f080c1a396 100644 --- a/packages/pretty-format/src/plugins/immutable.js +++ b/packages/pretty-format/src/plugins/immutable.js @@ -135,10 +135,6 @@ export const serialize = ( refs: Refs, printer: Printer, ): string => { - if (val[IS_SEQ_SENTINEL]) { - return '[' + getImmutableName('Seq') + ']'; - } - if (val[IS_MAP_SENTINEL]) { return printImmutableEntries( val, @@ -185,6 +181,10 @@ export const serialize = ( ); } + if (val[IS_SEQ_SENTINEL]) { + return '[' + getImmutableName('Seq') + ']'; + } + // For compatibility with immutable v3 and v4, let record be the default. return printImmutableRecord(val, config, indentation, depth, refs, printer); }; From 106e8ec20ee218d60e1c9e2a55426cea32cd17a0 Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Sat, 5 Aug 2017 17:07:19 -0400 Subject: [PATCH 10/10] Delete newlines between adjacent import and sentinel lines --- packages/pretty-format/src/plugins/immutable.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/packages/pretty-format/src/plugins/immutable.js b/packages/pretty-format/src/plugins/immutable.js index e9f080c1a396..e6f6ac85e0b0 100644 --- a/packages/pretty-format/src/plugins/immutable.js +++ b/packages/pretty-format/src/plugins/immutable.js @@ -9,20 +9,16 @@ */ import type {Config, Printer, NewPlugin, Refs} from 'types/PrettyFormat'; - import {printIteratorEntries, printIteratorValues} from '../collections'; // SENTINEL constants are from https://github.com/facebook/immutable-js - const IS_ITERABLE_SENTINEL = '@@__IMMUTABLE_ITERABLE__@@'; const IS_RECORD_SENTINEL = '@@__IMMUTABLE_RECORD__@@'; // v4 or later - const IS_LIST_SENTINEL = '@@__IMMUTABLE_LIST__@@'; const IS_MAP_SENTINEL = '@@__IMMUTABLE_MAP__@@'; const IS_SEQ_SENTINEL = '@@__IMMUTABLE_SEQ__@@'; const IS_SET_SENTINEL = '@@__IMMUTABLE_SET__@@'; const IS_STACK_SENTINEL = '@@__IMMUTABLE_STACK__@@'; - const IS_ORDERED_SENTINEL = '@@__IMMUTABLE_ORDERED__@@'; const getImmutableName = name => 'Immutable.' + name;