Skip to content

Commit fdc10bb

Browse files
committed
Fix #1205 - Use multi-line block for trailing quote
1 parent 6dc634b commit fdc10bb

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
@@ -545,6 +545,72 @@ describe('Type System Printer', () => {
545545
`);
546546
});
547547

548+
it('One-line prints a short description', () => {
549+
const description = 'This field is awesome';
550+
const output = printSingleFieldSchema({
551+
type: GraphQLString,
552+
description,
553+
});
554+
expect(output).to.equal(dedent`
555+
schema {
556+
query: Root
557+
}
558+
559+
type Root {
560+
"""This field is awesome"""
561+
singleField: String
562+
}
563+
`);
564+
const recreatedRoot = buildSchema(output).getTypeMap()['Root'];
565+
const recreatedField = recreatedRoot.getFields()['singleField'];
566+
expect(recreatedField.description).to.equal(description);
567+
});
568+
569+
it('Does not one-line print a description that ends with a quote', () => {
570+
const description = 'This field is "awesome"';
571+
const output = printSingleFieldSchema({
572+
type: GraphQLString,
573+
description,
574+
});
575+
expect(output).to.equal(dedent`
576+
schema {
577+
query: Root
578+
}
579+
580+
type Root {
581+
"""
582+
This field is "awesome"
583+
"""
584+
singleField: String
585+
}
586+
`);
587+
const recreatedRoot = buildSchema(output).getTypeMap()['Root'];
588+
const recreatedField = recreatedRoot.getFields()['singleField'];
589+
expect(recreatedField.description).to.equal(description);
590+
});
591+
592+
it('Preserves leading spaces when printing a description', () => {
593+
const description = ' This field is "awesome"';
594+
const output = printSingleFieldSchema({
595+
type: GraphQLString,
596+
description,
597+
});
598+
expect(output).to.equal(dedent`
599+
schema {
600+
query: Root
601+
}
602+
603+
type Root {
604+
""" This field is "awesome"
605+
"""
606+
singleField: String
607+
}
608+
`);
609+
const recreatedRoot = buildSchema(output).getTypeMap()['Root'];
610+
const recreatedField = recreatedRoot.getFields()['singleField'];
611+
expect(recreatedField.description).to.equal(description);
612+
});
613+
548614
it('Print Introspection Schema', () => {
549615
const Query = new GraphQLObjectType({
550616
name: 'Query',

src/utilities/schemaPrinter.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -336,15 +336,30 @@ function printDescription(
336336
return printDescriptionWithComments(lines, indentation, firstInBlock);
337337
}
338338
339-
let description = indentation && !firstInBlock ? '\n' : '';
340-
if (lines.length === 1 && lines[0].length < 70) {
341-
description += indentation + '"""' + escapeQuote(lines[0]) + '"""\n';
342-
return description;
339+
let description =
340+
indentation && !firstInBlock
341+
? '\n' + indentation + '"""'
342+
: indentation + '"""';
343+
344+
// In some circumstances, a single line can be used for the description.
345+
if (
346+
lines.length === 1 &&
347+
lines[0].length < 70 &&
348+
lines[0][lines[0].length - 1] !== '"'
349+
) {
350+
return description + escapeQuote(lines[0]) + '"""\n';
343351
}
344352
345-
description += indentation + '"""\n';
353+
// Format a multi-line block quote to account for leading space.
354+
const hasLeadingSpace = lines[0][0] === ' ' || lines[0][0] === '\t';
355+
if (!hasLeadingSpace) {
356+
description += '\n';
357+
}
346358
for (let i = 0; i < lines.length; i++) {
347-
description += indentation + escapeQuote(lines[i]) + '\n';
359+
if (i !== 0 || !hasLeadingSpace) {
360+
description += indentation;
361+
}
362+
description += escapeQuote(lines[i]) + '\n';
348363
}
349364
description += indentation + '"""\n';
350365
return description;

0 commit comments

Comments
 (0)