Skip to content

Commit 9239a96

Browse files
committed
Fix #1205 - Use multi-line block for trailing quote
1 parent fa1341f commit 9239a96

File tree

2 files changed

+87
-6
lines changed

2 files changed

+87
-6
lines changed

src/utilities/__tests__/schemaPrinter-test.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,72 @@ describe('Type System Printer', () => {
612612
`);
613613
});
614614

615+
it('One-line prints a short description', () => {
616+
const description = 'This field is awesome';
617+
const output = printSingleFieldSchema({
618+
type: GraphQLString,
619+
description,
620+
});
621+
expect(output).to.equal(dedent`
622+
schema {
623+
query: Root
624+
}
625+
626+
type Root {
627+
"""This field is awesome"""
628+
singleField: String
629+
}
630+
`);
631+
const recreatedRoot = buildSchema(output).getTypeMap()['Root'];
632+
const recreatedField = recreatedRoot.getFields()['singleField'];
633+
expect(recreatedField.description).to.equal(description);
634+
});
635+
636+
it('Does not one-line print a description that ends with a quote', () => {
637+
const description = 'This field is "awesome"';
638+
const output = printSingleFieldSchema({
639+
type: GraphQLString,
640+
description,
641+
});
642+
expect(output).to.equal(dedent`
643+
schema {
644+
query: Root
645+
}
646+
647+
type Root {
648+
"""
649+
This field is "awesome"
650+
"""
651+
singleField: String
652+
}
653+
`);
654+
const recreatedRoot = buildSchema(output).getTypeMap()['Root'];
655+
const recreatedField = recreatedRoot.getFields()['singleField'];
656+
expect(recreatedField.description).to.equal(description);
657+
});
658+
659+
it('Preserves leading spaces when printing a description', () => {
660+
const description = ' This field is "awesome"';
661+
const output = printSingleFieldSchema({
662+
type: GraphQLString,
663+
description,
664+
});
665+
expect(output).to.equal(dedent`
666+
schema {
667+
query: Root
668+
}
669+
670+
type Root {
671+
""" This field is "awesome"
672+
"""
673+
singleField: String
674+
}
675+
`);
676+
const recreatedRoot = buildSchema(output).getTypeMap()['Root'];
677+
const recreatedField = recreatedRoot.getFields()['singleField'];
678+
expect(recreatedField.description).to.equal(description);
679+
});
680+
615681
it('Print Introspection Schema', () => {
616682
const Root = new GraphQLObjectType({
617683
name: 'Root',

src/utilities/schemaPrinter.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -338,15 +338,30 @@ function printDescription(
338338
return printDescriptionWithComments(lines, indentation, firstInBlock);
339339
}
340340
341-
let description = indentation && !firstInBlock ? '\n' : '';
342-
if (lines.length === 1 && lines[0].length < 70) {
343-
description += indentation + '"""' + escapeQuote(lines[0]) + '"""\n';
344-
return description;
341+
let description =
342+
indentation && !firstInBlock
343+
? '\n' + indentation + '"""'
344+
: indentation + '"""';
345+
346+
// In some circumstances, a single line can be used for the description.
347+
if (
348+
lines.length === 1 &&
349+
lines[0].length < 70 &&
350+
lines[0][lines[0].length - 1] !== '"'
351+
) {
352+
return description + escapeQuote(lines[0]) + '"""\n';
345353
}
346354
347-
description += indentation + '"""\n';
355+
// Format a multi-line block quote to account for leading space.
356+
const hasLeadingSpace = lines[0][0] === ' ' || lines[0][0] === '\t';
357+
if (!hasLeadingSpace) {
358+
description += '\n';
359+
}
348360
for (let i = 0; i < lines.length; i++) {
349-
description += indentation + escapeQuote(lines[i]) + '\n';
361+
if (i !== 0 || !hasLeadingSpace) {
362+
description += indentation;
363+
}
364+
description += escapeQuote(lines[i]) + '\n';
350365
}
351366
description += indentation + '"""\n';
352367
return description;

0 commit comments

Comments
 (0)