From b55997eb3d735c59b0edd114461df93e61e3503c Mon Sep 17 00:00:00 2001 From: Ika Date: Fri, 1 Jun 2018 00:38:39 +0800 Subject: [PATCH] fix(multiparser): no additional trailing newline for graphql in js (#4616) Fixes #4615 The root cause is the output doc from graphql printer does not match the desired input from `stripTrailingHardline`. https://github.com/prettier/prettier/blob/a9b21a01e2d352aab06332e8e545e33efacc4f97/src/doc/doc-utils.js#L167-L180 --- src/language-graphql/printer-graphql.js | 13 ++++++----- .../__snapshots__/jsfmt.spec.js.snap | 23 +++++++++++++++++++ tests/multiparser_js_graphql/definitions.js | 9 ++++++++ 3 files changed, 39 insertions(+), 6 deletions(-) create mode 100644 tests/multiparser_js_graphql/definitions.js diff --git a/src/language-graphql/printer-graphql.js b/src/language-graphql/printer-graphql.js index 66085561..778da04c 100644 --- a/src/language-graphql/printer-graphql.js +++ b/src/language-graphql/printer-graphql.js @@ -28,15 +28,16 @@ function genericPrint(path, options, print) { const parts = []; path.map((pathChild, index) => { parts.push(concat([pathChild.call(print)])); - parts.push(hardline); - if ( - index !== n.definitions.length - 1 && - isNextLineEmpty(options.originalText, pathChild.getValue(), options) - ) { + if (index !== n.definitions.length - 1) { parts.push(hardline); + if ( + isNextLineEmpty(options.originalText, pathChild.getValue(), options) + ) { + parts.push(hardline); + } } }, "definitions"); - return concat(parts, hardline); + return concat([concat(parts), hardline]); } case "OperationDefinition": { const hasOperation = options.originalText[options.locStart(n)] !== "{"; diff --git a/tests/multiparser_js_graphql/__snapshots__/jsfmt.spec.js.snap b/tests/multiparser_js_graphql/__snapshots__/jsfmt.spec.js.snap index b8ca5bb6..aa917b82 100644 --- a/tests/multiparser_js_graphql/__snapshots__/jsfmt.spec.js.snap +++ b/tests/multiparser_js_graphql/__snapshots__/jsfmt.spec.js.snap @@ -40,6 +40,29 @@ const query = /* GraphQL */ \` `; +exports[`definitions.js 1`] = ` +graphql\` + fragment x on y { + z + } + + fragment a on b { + c + } +\`; +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +graphql\` + fragment x on y { + z + } + + fragment a on b { + c + } +\`; + +`; + exports[`expressions.js 1`] = ` graphql(schema, \` query allPartsByManufacturerName($name: String!) { diff --git a/tests/multiparser_js_graphql/definitions.js b/tests/multiparser_js_graphql/definitions.js new file mode 100644 index 00000000..e2f6ca95 --- /dev/null +++ b/tests/multiparser_js_graphql/definitions.js @@ -0,0 +1,9 @@ +graphql` + fragment x on y { + z + } + + fragment a on b { + c + } +`;