Skip to content

Commit

Permalink
Fix #1205 - Use multi-line block for trailing quote
Browse files Browse the repository at this point in the history
  • Loading branch information
leebyron committed Jan 27, 2018
1 parent fa1341f commit 9239a96
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 6 deletions.
66 changes: 66 additions & 0 deletions src/utilities/__tests__/schemaPrinter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,72 @@ describe('Type System Printer', () => {
`);
});

it('One-line prints a short description', () => {
const description = 'This field is awesome';
const output = printSingleFieldSchema({
type: GraphQLString,
description,
});
expect(output).to.equal(dedent`
schema {
query: Root
}
type Root {
"""This field is awesome"""
singleField: String
}
`);
const recreatedRoot = buildSchema(output).getTypeMap()['Root'];
const recreatedField = recreatedRoot.getFields()['singleField'];
expect(recreatedField.description).to.equal(description);
});

it('Does not one-line print a description that ends with a quote', () => {
const description = 'This field is "awesome"';
const output = printSingleFieldSchema({
type: GraphQLString,
description,
});
expect(output).to.equal(dedent`
schema {
query: Root
}
type Root {
"""
This field is "awesome"
"""
singleField: String
}
`);
const recreatedRoot = buildSchema(output).getTypeMap()['Root'];
const recreatedField = recreatedRoot.getFields()['singleField'];
expect(recreatedField.description).to.equal(description);
});

it('Preserves leading spaces when printing a description', () => {
const description = ' This field is "awesome"';
const output = printSingleFieldSchema({
type: GraphQLString,
description,
});
expect(output).to.equal(dedent`
schema {
query: Root
}
type Root {
""" This field is "awesome"
"""
singleField: String
}
`);
const recreatedRoot = buildSchema(output).getTypeMap()['Root'];
const recreatedField = recreatedRoot.getFields()['singleField'];
expect(recreatedField.description).to.equal(description);
});

it('Print Introspection Schema', () => {
const Root = new GraphQLObjectType({
name: 'Root',
Expand Down
27 changes: 21 additions & 6 deletions src/utilities/schemaPrinter.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,15 +338,30 @@ function printDescription(
return printDescriptionWithComments(lines, indentation, firstInBlock);
}

let description = indentation && !firstInBlock ? '\n' : '';
if (lines.length === 1 && lines[0].length < 70) {
description += indentation + '"""' + escapeQuote(lines[0]) + '"""\n';
return description;
let description =
indentation && !firstInBlock
? '\n' + indentation + '"""'
: indentation + '"""';

// In some circumstances, a single line can be used for the description.
if (
lines.length === 1 &&
lines[0].length < 70 &&
lines[0][lines[0].length - 1] !== '"'
) {
return description + escapeQuote(lines[0]) + '"""\n';
}

description += indentation + '"""\n';
// Format a multi-line block quote to account for leading space.
const hasLeadingSpace = lines[0][0] === ' ' || lines[0][0] === '\t';
if (!hasLeadingSpace) {
description += '\n';
}
for (let i = 0; i < lines.length; i++) {
description += indentation + escapeQuote(lines[i]) + '\n';
if (i !== 0 || !hasLeadingSpace) {
description += indentation;
}
description += escapeQuote(lines[i]) + '\n';
}
description += indentation + '"""\n';
return description;
Expand Down

0 comments on commit 9239a96

Please sign in to comment.