From 69edf3baa18b326dd423ed978f23e5038cc7edb5 Mon Sep 17 00:00:00 2001 From: Dylan Owen Date: Thu, 9 Mar 2023 17:38:28 -0500 Subject: [PATCH 1/2] Make print() break long List and Object Values over multiple line --- src/language/__tests__/printer-test.ts | 58 ++++++++++++++++++++++++++ src/language/printer.ts | 18 +++++++- 2 files changed, 74 insertions(+), 2 deletions(-) diff --git a/src/language/__tests__/printer-test.ts b/src/language/__tests__/printer-test.ts index 0585cae6d9..04586834cc 100644 --- a/src/language/__tests__/printer-test.ts +++ b/src/language/__tests__/printer-test.ts @@ -110,6 +110,64 @@ describe('Printer: Query document', () => { `); }); + it('puts large object values on multiple lines if line is long (> 80 chars)', () => { + const printed = print( + parse( + '{trip(obj:{wheelchair:false,smallObj:{a: 1},largeObj:{wheelchair:false,smallObj:{a: 1},arriveBy:false,includePlannedCancellations:true,transitDistanceReluctance:2000,anotherLongFieldName:"Lots and lots and lots and lots of text"},arriveBy:false,includePlannedCancellations:true,transitDistanceReluctance:2000,anotherLongFieldName:"Lots and lots and lots and lots of text"}){dateTime}}', + ), + ); + + expect(printed).to.equal(dedent` + { + trip( + obj: { + wheelchair: false, + smallObj: { a: 1 }, + largeObj: { + wheelchair: false, + smallObj: { a: 1 }, + arriveBy: false, + includePlannedCancellations: true, + transitDistanceReluctance: 2000, + anotherLongFieldName: "Lots and lots and lots and lots of text" + }, + arriveBy: false, + includePlannedCancellations: true, + transitDistanceReluctance: 2000, + anotherLongFieldName: "Lots and lots and lots and lots of text" + } + ) { + dateTime + } + } + `); + }); + + it('puts large list values on multiple lines if line is long (> 80 chars)', () => { + const printed = print( + parse( + '{trip(list:[["small array", "small", "small"], ["Lots and lots and lots and lots of text", "Lots and lots and lots and lots of text", "Lots and lots and lots and lots of text"]]){dateTime}}', + ), + ); + + expect(printed).to.equal(dedent` + { + trip( + list: [ + ["small array", "small", "small"], + [ + "Lots and lots and lots and lots of text", + "Lots and lots and lots and lots of text", + "Lots and lots and lots and lots of text" + ] + ] + ) { + dateTime + } + } + `); + }); + it('Legacy: prints fragment with variable directives', () => { const queryASTWithVariableDirective = parse( 'fragment Foo($foo: TestType @test) on TestType @testDirective { id }', diff --git a/src/language/printer.ts b/src/language/printer.ts index 9363c6ec46..c8c4ea6864 100644 --- a/src/language/printer.ts +++ b/src/language/printer.ts @@ -148,8 +148,22 @@ const printDocASTReducer: ASTReducer = { BooleanValue: { leave: ({ value }) => (value ? 'true' : 'false') }, NullValue: { leave: () => 'null' }, EnumValue: { leave: ({ value }) => value }, - ListValue: { leave: ({ values }) => '[' + join(values, ', ') + ']' }, - ObjectValue: { leave: ({ fields }) => '{ ' + join(fields, ', ') + ' }' }, + ListValue: { + leave: ({ values }) => { + if (values.reduce((t, v) => t + v.length + 2, 0) > MAX_LINE_LENGTH) { + return '[\n' + indent(join(values, ',\n')) + '\n]'; + } + return '[' + join(values, ', ') + ']'; + }, + }, + ObjectValue: { + leave: ({ fields }) => { + if (fields.reduce((t, f) => t + f.length + 2, 0) > MAX_LINE_LENGTH) { + return '{\n' + indent(join(fields, ',\n')) + '\n}'; + } + return '{ ' + join(fields, ', ') + ' }'; + }, + }, ObjectField: { leave: ({ name, value }) => name + ': ' + value }, // Directive From cfde0cc180afcd9b809a1988cc415f83553aea21 Mon Sep 17 00:00:00 2001 From: Ivan Goncharov Date: Wed, 5 Apr 2023 17:37:59 +0300 Subject: [PATCH 2/2] review changes --- src/language/__tests__/printer-test.ts | 28 +++++++++++++------------- src/language/printer.ts | 14 ++++++------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/language/__tests__/printer-test.ts b/src/language/__tests__/printer-test.ts index 04586834cc..7eea508458 100644 --- a/src/language/__tests__/printer-test.ts +++ b/src/language/__tests__/printer-test.ts @@ -121,19 +121,19 @@ describe('Printer: Query document', () => { { trip( obj: { - wheelchair: false, - smallObj: { a: 1 }, + wheelchair: false + smallObj: { a: 1 } largeObj: { - wheelchair: false, - smallObj: { a: 1 }, - arriveBy: false, - includePlannedCancellations: true, - transitDistanceReluctance: 2000, + wheelchair: false + smallObj: { a: 1 } + arriveBy: false + includePlannedCancellations: true + transitDistanceReluctance: 2000 anotherLongFieldName: "Lots and lots and lots and lots of text" - }, - arriveBy: false, - includePlannedCancellations: true, - transitDistanceReluctance: 2000, + } + arriveBy: false + includePlannedCancellations: true + transitDistanceReluctance: 2000 anotherLongFieldName: "Lots and lots and lots and lots of text" } ) { @@ -154,10 +154,10 @@ describe('Printer: Query document', () => { { trip( list: [ - ["small array", "small", "small"], + ["small array", "small", "small"] [ - "Lots and lots and lots and lots of text", - "Lots and lots and lots and lots of text", + "Lots and lots and lots and lots of text" + "Lots and lots and lots and lots of text" "Lots and lots and lots and lots of text" ] ] diff --git a/src/language/printer.ts b/src/language/printer.ts index c8c4ea6864..28bb25da17 100644 --- a/src/language/printer.ts +++ b/src/language/printer.ts @@ -150,18 +150,18 @@ const printDocASTReducer: ASTReducer = { EnumValue: { leave: ({ value }) => value }, ListValue: { leave: ({ values }) => { - if (values.reduce((t, v) => t + v.length + 2, 0) > MAX_LINE_LENGTH) { - return '[\n' + indent(join(values, ',\n')) + '\n]'; + const valuesLine = '[' + join(values, ', ') + ']'; + + if (valuesLine.length > MAX_LINE_LENGTH) { + return '[\n' + indent(join(values, '\n')) + '\n]'; } - return '[' + join(values, ', ') + ']'; + return valuesLine; }, }, ObjectValue: { leave: ({ fields }) => { - if (fields.reduce((t, f) => t + f.length + 2, 0) > MAX_LINE_LENGTH) { - return '{\n' + indent(join(fields, ',\n')) + '\n}'; - } - return '{ ' + join(fields, ', ') + ' }'; + const fieldsLine = '{ ' + join(fields, ', ') + ' }'; + return fieldsLine.length > MAX_LINE_LENGTH ? block(fields) : fieldsLine; }, }, ObjectField: { leave: ({ name, value }) => name + ': ' + value },