diff --git a/src/language-js/comments/will-print-own-comments.js b/src/language-js/comments/will-print-own-comments.js index d1b5dc819..963120ee8 100644 --- a/src/language-js/comments/will-print-own-comments.js +++ b/src/language-js/comments/will-print-own-comments.js @@ -1,7 +1,7 @@ import { createTypeCheckFunction, hasNodeIgnoreComment, - isIifeCallee, + isIifeCalleeOrTaggedTemplateExpressionTag, isJsxElement, isUnionType, shouldUnionTypePrintOwnComments, @@ -40,7 +40,7 @@ function willPrintOwnComments(path) { isClassOrInterface(parent)) || // Not tested, don't know how to (key === "patterns" && parent.type === "MatchOrPattern") || - isIifeCallee(path) + isIifeCalleeOrTaggedTemplateExpressionTag(path) ) { return true; } diff --git a/src/language-js/print/index.js b/src/language-js/print/index.js index e0845f935..db277d5d4 100644 --- a/src/language-js/print/index.js +++ b/src/language-js/print/index.js @@ -14,7 +14,7 @@ import { CommentCheckFlags, createTypeCheckFunction, hasComment, - isIifeCallee, + isIifeCalleeOrTaggedTemplateExpressionTag, } from "../utilities/index.js"; import isIgnored from "../utilities/is-ignored.js"; import { printAngular } from "./angular.js"; @@ -85,7 +85,7 @@ function print(path, options, print, args) { return doc; } - doc = printCommentsForIifeCallee(path, options, doc); + doc = printCommentsForFunction(path, options, doc); const hasDecorators = isNonEmptyArray(node.decorators); const decoratorsDoc = printDecorators(path, options, print); @@ -112,13 +112,13 @@ function print(path, options, print, args) { ]); } -function printCommentsForIifeCallee(path, options, doc) { +function printCommentsForFunction(path, options, doc) { const { node } = path; if ( (hasComment(node, CommentCheckFlags.Leading) || hasComment(node, CommentCheckFlags.Trailing)) && - isIifeCallee(path) + isIifeCalleeOrTaggedTemplateExpressionTag(path) ) { return [indent([softline, printComments(path, doc, options)]), softline]; } diff --git a/src/language-js/utilities/index.js b/src/language-js/utilities/index.js index 8af545d0c..9d8323872 100644 --- a/src/language-js/utilities/index.js +++ b/src/language-js/utilities/index.js @@ -1103,13 +1103,13 @@ function isShorthandSpecifier(specifier) { } } -function isIifeCallee(path) { +function isIifeCalleeOrTaggedTemplateExpressionTag(path) { const { node } = path; return ( (node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression") && - path.key === "callee" && - isCallExpression(path.parent) + ((path.key === "callee" && isCallExpression(path.parent)) || + (path.key === "tag" && path.parent.type === "TaggedTemplateExpression")) ); } @@ -1142,7 +1142,7 @@ export { isFlowObjectTypePropertyAFunction, isFunctionCompositionArgs, isFunctionOrArrowExpression, - isIifeCallee, + isIifeCalleeOrTaggedTemplateExpressionTag, isIntersectionType, isJsxElement, isLiteral, diff --git a/tests/format/js/function/__snapshots__/format.test.js.snap b/tests/format/js/function/__snapshots__/format.test.js.snap index 8782123d5..560ee164c 100644 --- a/tests/format/js/function/__snapshots__/format.test.js.snap +++ b/tests/format/js/function/__snapshots__/format.test.js.snap @@ -56,6 +56,10 @@ printWidth: 80 (// prettier-ignore function () { })(), (function () {}/* trialing 2 */)(), + (// tagged + function () { })\`\`, + (// prettier-ignore + function () { })\`\`, (// leading 1 @@ -67,6 +71,10 @@ printWidth: 80 (() => {} // trialing 1 )(), + (// tagged + () => {})\`\`, + (// prettier-ignore + () => { })\`\`, ((/*dangling 1*/) =>{})(), ( () => {}/* trialing 2 */)(), @@ -98,6 +106,14 @@ printWidth: 80 function () { } )(), (function () {} /* trialing 2 */)(), + ( + // tagged + function () {} + )\`\`, + ( + // prettier-ignore + function () { } + )\`\`, ( // leading 1 @@ -112,6 +128,14 @@ printWidth: 80 () => {} // trialing 1 )(), + ( + // tagged + () => {} + )\`\`, + ( + // prettier-ignore + () => { } + )\`\`, ((/*dangling 1*/) => {})(), (() => {} /* trialing 2 */)(), diff --git a/tests/format/js/function/iife.js b/tests/format/js/function/iife.js index 32096eb26..0e4ac77c3 100644 --- a/tests/format/js/function/iife.js +++ b/tests/format/js/function/iife.js @@ -14,6 +14,10 @@ (// prettier-ignore function () { })(), (function () {}/* trialing 2 */)(), + (// tagged + function () { })``, + (// prettier-ignore + function () { })``, (// leading 1 @@ -25,6 +29,10 @@ (() => {} // trialing 1 )(), + (// tagged + () => {})``, + (// prettier-ignore + () => { })``, ((/*dangling 1*/) =>{})(), ( () => {}/* trialing 2 */)(),