Skip to content

Make print() break long List and Object Values over multiple line #3860

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions src/language/__tests__/printer-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }',
Expand Down
18 changes: 16 additions & 2 deletions src/language/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,22 @@ const printDocASTReducer: ASTReducer<string> = {
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 }) => {
const valuesLine = '[' + join(values, ', ') + ']';

if (valuesLine.length > MAX_LINE_LENGTH) {
return '[\n' + indent(join(values, '\n')) + '\n]';
}
return valuesLine;
},
},
ObjectValue: {
leave: ({ fields }) => {
const fieldsLine = '{ ' + join(fields, ', ') + ' }';
return fieldsLine.length > MAX_LINE_LENGTH ? block(fields) : fieldsLine;
},
},
ObjectField: { leave: ({ name, value }) => name + ': ' + value },

// Directive
Expand Down