Skip to content

Commit a722af7

Browse files
authored
Fix #1205 - Use multi-line block for trailing quote (#1212)
* Fix #1205 - Use multi-line block for trailing quote * Fix Travis build on #1212 (#1213) Made the exact same changes as in #1211
1 parent 6dc634b commit a722af7

File tree

2 files changed

+75
-6
lines changed

2 files changed

+75
-6
lines changed

src/utilities/__tests__/schemaPrinter-test.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,60 @@ 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+
type Query {
556+
"""This field is awesome"""
557+
singleField: String
558+
}
559+
`);
560+
const recreatedRoot = buildSchema(output).getTypeMap()['Query'];
561+
const recreatedField = recreatedRoot.getFields()['singleField'];
562+
expect(recreatedField.description).to.equal(description);
563+
});
564+
565+
it('Does not one-line print a description that ends with a quote', () => {
566+
const description = 'This field is "awesome"';
567+
const output = printSingleFieldSchema({
568+
type: GraphQLString,
569+
description,
570+
});
571+
expect(output).to.equal(dedent`
572+
type Query {
573+
"""
574+
This field is "awesome"
575+
"""
576+
singleField: String
577+
}
578+
`);
579+
const recreatedRoot = buildSchema(output).getTypeMap()['Query'];
580+
const recreatedField = recreatedRoot.getFields()['singleField'];
581+
expect(recreatedField.description).to.equal(description);
582+
});
583+
584+
it('Preserves leading spaces when printing a description', () => {
585+
const description = ' This field is "awesome"';
586+
const output = printSingleFieldSchema({
587+
type: GraphQLString,
588+
description,
589+
});
590+
expect(output).to.equal(dedent`
591+
type Query {
592+
""" This field is "awesome"
593+
"""
594+
singleField: String
595+
}
596+
`);
597+
const recreatedRoot = buildSchema(output).getTypeMap()['Query'];
598+
const recreatedField = recreatedRoot.getFields()['singleField'];
599+
expect(recreatedField.description).to.equal(description);
600+
});
601+
548602
it('Print Introspection Schema', () => {
549603
const Query = new GraphQLObjectType({
550604
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)