From fdc10bb918e5d84ce6b34e122069851ca3e6f6a4 Mon Sep 17 00:00:00 2001 From: Lee Byron Date: Fri, 26 Jan 2018 16:43:19 -0800 Subject: [PATCH 1/2] Fix #1205 - Use multi-line block for trailing quote --- src/utilities/__tests__/schemaPrinter-test.js | 66 +++++++++++++++++++ src/utilities/schemaPrinter.js | 27 ++++++-- 2 files changed, 87 insertions(+), 6 deletions(-) diff --git a/src/utilities/__tests__/schemaPrinter-test.js b/src/utilities/__tests__/schemaPrinter-test.js index ce728bd180..c410623739 100644 --- a/src/utilities/__tests__/schemaPrinter-test.js +++ b/src/utilities/__tests__/schemaPrinter-test.js @@ -545,6 +545,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 Query = new GraphQLObjectType({ name: 'Query', diff --git a/src/utilities/schemaPrinter.js b/src/utilities/schemaPrinter.js index 8515bafef8..f52785e9d0 100644 --- a/src/utilities/schemaPrinter.js +++ b/src/utilities/schemaPrinter.js @@ -336,15 +336,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; From 11510ed6250eb351e78a9ad4331f8860d385515e Mon Sep 17 00:00:00 2001 From: Ivan Goncharov Date: Mon, 29 Jan 2018 21:46:58 +0200 Subject: [PATCH 2/2] Fix Travis build on #1212 (#1213) Made the exact same changes as in #1211 --- src/utilities/__tests__/schemaPrinter-test.js | 24 +++++-------------- 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/src/utilities/__tests__/schemaPrinter-test.js b/src/utilities/__tests__/schemaPrinter-test.js index c410623739..66c8baabf5 100644 --- a/src/utilities/__tests__/schemaPrinter-test.js +++ b/src/utilities/__tests__/schemaPrinter-test.js @@ -552,16 +552,12 @@ describe('Type System Printer', () => { description, }); expect(output).to.equal(dedent` - schema { - query: Root - } - - type Root { + type Query { """This field is awesome""" singleField: String } `); - const recreatedRoot = buildSchema(output).getTypeMap()['Root']; + const recreatedRoot = buildSchema(output).getTypeMap()['Query']; const recreatedField = recreatedRoot.getFields()['singleField']; expect(recreatedField.description).to.equal(description); }); @@ -573,18 +569,14 @@ describe('Type System Printer', () => { description, }); expect(output).to.equal(dedent` - schema { - query: Root - } - - type Root { + type Query { """ This field is "awesome" """ singleField: String } `); - const recreatedRoot = buildSchema(output).getTypeMap()['Root']; + const recreatedRoot = buildSchema(output).getTypeMap()['Query']; const recreatedField = recreatedRoot.getFields()['singleField']; expect(recreatedField.description).to.equal(description); }); @@ -596,17 +588,13 @@ describe('Type System Printer', () => { description, }); expect(output).to.equal(dedent` - schema { - query: Root - } - - type Root { + type Query { """ This field is "awesome" """ singleField: String } `); - const recreatedRoot = buildSchema(output).getTypeMap()['Root']; + const recreatedRoot = buildSchema(output).getTypeMap()['Query']; const recreatedField = recreatedRoot.getFields()['singleField']; expect(recreatedField.description).to.equal(description); });