From b9cf5f3456d0cc3e3182a95f185c45489da5e677 Mon Sep 17 00:00:00 2001 From: Mats Byrkjeland Date: Mon, 21 Sep 2020 12:21:59 +0200 Subject: [PATCH] Make print() break arguments over multiple lines (#2797) --- src/language/__tests__/printer-test.js | 49 ++++++++++++++++++++++++-- src/language/printer.js | 21 ++++++----- 2 files changed, 58 insertions(+), 12 deletions(-) diff --git a/src/language/__tests__/printer-test.js b/src/language/__tests__/printer-test.js index 0c852757e0..0f259e5e63 100644 --- a/src/language/__tests__/printer-test.js +++ b/src/language/__tests__/printer-test.js @@ -79,6 +79,45 @@ describe('Printer: Query document', () => { `); }); + it('keeps arguments on one line if line is short (<= 80 chars)', () => { + const printed = print( + parse('{trip(wheelchair:false arriveBy:false){dateTime}}'), + ); + + expect(printed).to.equal( + dedent` + { + trip(wheelchair: false, arriveBy: false) { + dateTime + } + } + `, + ); + }); + + it('puts arguments on multiple lines if line is long (> 80 chars)', () => { + const printed = print( + parse( + '{trip(wheelchair:false arriveBy:false includePlannedCancellations:true transitDistanceReluctance:2000){dateTime}}', + ), + ); + + expect(printed).to.equal( + dedent` + { + trip( + wheelchair: false + arriveBy: false + includePlannedCancellations: true + transitDistanceReluctance: 2000 + ) { + dateTime + } + } + `, + ); + }); + it('Experimental: prints fragment with variable directives', () => { const queryASTWithVariableDirective = parse( 'fragment Foo($foo: TestType @test) on TestType @testDirective { id }', @@ -158,9 +197,13 @@ describe('Printer: Query document', () => { } fragment frag on Friend @onFragmentDefinition { - foo(size: $size, bar: $b, obj: {key: "value", block: """ - block string uses \""" - """}) + foo( + size: $size + bar: $b + obj: {key: "value", block: """ + block string uses \""" + """} + ) } { diff --git a/src/language/printer.js b/src/language/printer.js index 3b30b7a659..8ca8fb2dad 100644 --- a/src/language/printer.js +++ b/src/language/printer.js @@ -11,6 +11,8 @@ export function print(ast: ASTNode): string { return visit(ast, { leave: printDocASTReducer }); } +const MAX_LINE_LENGTH = 80; + // TODO: provide better type coverage in future const printDocASTReducer: any = { Name: (node) => node.value, @@ -41,15 +43,16 @@ const printDocASTReducer: any = { wrap(' ', join(directives, ' ')), SelectionSet: ({ selections }) => block(selections), - Field: ({ alias, name, arguments: args, directives, selectionSet }) => - join( - [ - wrap('', alias, ': ') + name + wrap('(', join(args, ', '), ')'), - join(directives, ' '), - selectionSet, - ], - ' ', - ), + Field: ({ alias, name, arguments: args, directives, selectionSet }) => { + const prefix = wrap('', alias, ': ') + name; + let argsLine = prefix + wrap('(', join(args, ', '), ')'); + + if (argsLine.length > MAX_LINE_LENGTH) { + argsLine = prefix + wrap('(\n', indent(join(args, '\n')), '\n)'); + } + + return join([argsLine, join(directives, ' '), selectionSet], ' '); + }, Argument: ({ name, value }) => name + ': ' + value,