diff --git a/changelog_unreleased/graphql/pr-8006.md b/changelog_unreleased/graphql/pr-8006.md new file mode 100644 index 000000000..29892f724 --- /dev/null +++ b/changelog_unreleased/graphql/pr-8006.md @@ -0,0 +1,33 @@ +#### Fix comments between interfaces ([#8006](https://github.com/prettier/prettier/pull/8006) by [@fisker](https://github.com/fisker)) + + +```graphql +# Input +type Type1 implements + A & +# comment 1 + B +# comment 2 + & C {a: a} + +# Prettier stable +type Type1 implements A & # comment 1 +B & # comment 2 +C { + a: a +} + +# Prettier stable (Second format) +type Type1 implements A & B & C { # comment 1 # comment 2 + a: a +} + +# Prettier master +type Type1 implements A & +# comment 1 +B & +# comment 2 +C { + a: a +} +``` diff --git a/src/language-graphql/printer-graphql.js b/src/language-graphql/printer-graphql.js index 8daa52160..5da58a97b 100644 --- a/src/language-graphql/printer-graphql.js +++ b/src/language-graphql/printer-graphql.js @@ -662,14 +662,6 @@ function printComment(commentPath) { throw new Error("Not a comment: " + JSON.stringify(comment)); } -function determineInterfaceSeparatorBetween(first, second, options) { - const textBetween = options.originalText - .slice(first.loc.end, second.loc.start) - .replace(/#.*/g, "") - .trim(); - - return textBetween === "," ? ", " : " & "; -} function printInterfaces(path, options, print) { const node = path.getNode(); const parts = []; @@ -678,18 +670,21 @@ function printInterfaces(path, options, print) { for (let index = 0; index < interfaces.length; index++) { const interfaceNode = interfaces[index]; - if (index > 0) { - parts.push( - determineInterfaceSeparatorBetween( - interfaces[index - 1], - interfaceNode, - options - ) - ); - } - parts.push(printed[index]); + const nextInterfaceNode = interfaces[index + 1]; + if (nextInterfaceNode) { + const textBetween = options.originalText.slice( + interfaceNode.loc.end, + nextInterfaceNode.loc.start + ); + const hasComment = textBetween.includes("#"); + const separator = textBetween.replace(/#.*/g, "").trim(); + + parts.push(separator === "," ? "," : " &"); + parts.push(hasComment ? line : " "); + } } + return parts; } diff --git a/tests/graphql_comments/__snapshots__/jsfmt.spec.js.snap b/tests/graphql_comments/__snapshots__/jsfmt.spec.js.snap index 3f1dc01f7..14163614a 100644 --- a/tests/graphql_comments/__snapshots__/jsfmt.spec.js.snap +++ b/tests/graphql_comments/__snapshots__/jsfmt.spec.js.snap @@ -45,6 +45,32 @@ query { ================================================================================ `; +exports[`interfaces.graphql format 1`] = ` +====================================options===================================== +parsers: ["graphql"] +printWidth: 80 + | printWidth +=====================================input====================================== +type Type1 +implements +A & B & +# comment 1 + C & D & +# comment 2 + E {a: a} + +=====================================output===================================== +type Type1 implements A & B & +# comment 1 +C & D & +# comment 2 +E { + a: a +} + +================================================================================ +`; + exports[`tokens.graphql format 1`] = ` ====================================options===================================== parsers: ["graphql"] diff --git a/tests/graphql_comments/interfaces.graphql b/tests/graphql_comments/interfaces.graphql new file mode 100644 index 000000000..4698822df --- /dev/null +++ b/tests/graphql_comments/interfaces.graphql @@ -0,0 +1,7 @@ +type Type1 +implements +A & B & +# comment 1 + C & D & +# comment 2 + E {a: a} diff --git a/tests/graphql_interface/__snapshots__/jsfmt.spec.js.snap b/tests/graphql_interface/__snapshots__/jsfmt.spec.js.snap index cd136b49d..19dd8a7cf 100644 --- a/tests/graphql_interface/__snapshots__/jsfmt.spec.js.snap +++ b/tests/graphql_interface/__snapshots__/jsfmt.spec.js.snap @@ -305,19 +305,22 @@ type Type2 implements A, B, C, D { a: a } -type Type3 implements A, # &&&&&&&& comment line 1 +type Type3 implements A, +# &&&&&&&& comment line 1 # &&&&&&&& comment line 2 B & C, D { a: a } -type Type4 implements A, # &&&&&&&& comment line 1 +type Type4 implements A, +# &&&&&&&& comment line 1 # &&&&&&&& comment line 2 B & C, D { a: a } -type Type5 implements A, # &&&&&&&& comment line 1 +type Type5 implements A, +# &&&&&&&& comment line 1 # &&&&&&&& comment line 2 B & C, D { a: a diff --git a/tests_config/run_spec.js b/tests_config/run_spec.js index 35db4ffa4..81d811b5e 100644 --- a/tests_config/run_spec.js +++ b/tests_config/run_spec.js @@ -28,7 +28,6 @@ const unstableTests = new Map( "comments/return-statement.js", "comments/tagged-template-literal.js", "comments_closure_typecast/iife.js", - "graphql_interface/separator-detection.graphql", "markdown_footnoteDefinition/multiline.md", "markdown_spec/example-234.md", "markdown_spec/example-235.md",