diff --git a/changelog_unreleased/javascript/16064.md b/changelog_unreleased/javascript/16064.md new file mode 100644 index 000000000..a01066d1f --- /dev/null +++ b/changelog_unreleased/javascript/16064.md @@ -0,0 +1,21 @@ +#### Format embedded GQL in template literal statements (#16064 by @keithlayne) + + +```jsx +// Input +/* GraphQL */ ` + query foo { id } +`; + +// Prettier stable +/* GraphQL */ ` + query foo { id } +`; + +// Prettier main +/* GraphQL */ ` + query foo { + id + } +`; +``` diff --git a/src/language-js/embed/utils.js b/src/language-js/embed/utils.js index ca9c41c02..e63e64b3a 100644 --- a/src/language-js/embed/utils.js +++ b/src/language-js/embed/utils.js @@ -80,6 +80,8 @@ function hasLanguageComment({ node, parent }, languageName) { return ( hasLeadingBlockCommentWithName(node, languageName) || (isAsConstExpression(parent) && + hasLeadingBlockCommentWithName(parent, languageName)) || + (parent.type === "ExpressionStatement" && hasLeadingBlockCommentWithName(parent, languageName)) ); } diff --git a/tests/format/js/multiparser-graphql/__snapshots__/jsfmt.spec.js.snap b/tests/format/js/multiparser-graphql/__snapshots__/jsfmt.spec.js.snap index 2c9dfd240..8397b6fca 100644 --- a/tests/format/js/multiparser-graphql/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/js/multiparser-graphql/__snapshots__/jsfmt.spec.js.snap @@ -16,6 +16,17 @@ const query = /* GraphQL */\` } \`; +/* GraphQL */\` + { + user( id : 5 , type: + "without variable assignment" ) { + firstName + + lastName + } + } +\`; + =====================================output===================================== const query = /* GraphQL */ \` { @@ -27,6 +38,16 @@ const query = /* GraphQL */ \` } \`; +/* GraphQL */ \` + { + user(id: 5, type: "without variable assignment") { + firstName + + lastName + } + } +\`; + ================================================================================ `; diff --git a/tests/format/js/multiparser-graphql/comment-tag.js b/tests/format/js/multiparser-graphql/comment-tag.js index f8a0c18ee..626ef3918 100644 --- a/tests/format/js/multiparser-graphql/comment-tag.js +++ b/tests/format/js/multiparser-graphql/comment-tag.js @@ -7,3 +7,14 @@ const query = /* GraphQL */` } } `; + +/* GraphQL */` + { + user( id : 5 , type: + "without variable assignment" ) { + firstName + + lastName + } + } +`;