diff --git a/src/language-js/embed/css.js b/src/language-js/embed/css.js index 65c0c0c35..a9ac037f3 100644 --- a/src/language-js/embed/css.js +++ b/src/language-js/embed/css.js @@ -157,15 +157,10 @@ function isCssProp({ parent, grandparent }) { ); } -function printCss(path /* , options*/) { - if ( - isStyledJsx(path) || - isStyledComponents(path) || - isCssProp(path) || - isAngularComponentStyles(path) - ) { - return printEmbedCss; - } -} +const isEmbedCss = (path /* , options*/) => + isStyledJsx(path) || + isStyledComponents(path) || + isCssProp(path) || + isAngularComponentStyles(path); -export default printCss; +export { isEmbedCss, printEmbedCss }; diff --git a/src/language-js/embed/graphql.js b/src/language-js/embed/graphql.js index 347f70257..02871060c 100644 --- a/src/language-js/embed/graphql.js +++ b/src/language-js/embed/graphql.js @@ -105,7 +105,7 @@ function printGraphqlComments(lines) { * This intentionally excludes Relay Classic tags, as Prettier does not * support Relay Classic formatting. */ -function isGraphQL({ node, parent }) { +function isEmbedGraphQL({ node, parent }) { return ( hasLanguageComment({ node, parent }, "GraphQL") || (parent && @@ -121,10 +121,4 @@ function isGraphQL({ node, parent }) { ); } -function printGraphql(path /* , options*/) { - if (isGraphQL(path)) { - return printEmbedGraphQL; - } -} - -export default printGraphql; +export { isEmbedGraphQL, printEmbedGraphQL }; diff --git a/src/language-js/embed/html.js b/src/language-js/embed/html.js index 21abc8c71..7a2044fc3 100644 --- a/src/language-js/embed/html.js +++ b/src/language-js/embed/html.js @@ -106,7 +106,7 @@ async function printEmbedHtmlLike(parser, textToDoc, print, path, options) { * - html`...` * - HTML comment block */ -function isHtml(path) { +function isEmbedHtml(path) { return ( hasLanguageComment(path, "HTML") || path.match( @@ -123,14 +123,9 @@ function isHtml(path) { const printEmbedHtml = printEmbedHtmlLike.bind(undefined, "html"); const printEmbedAngular = printEmbedHtmlLike.bind(undefined, "angular"); -function printHtml(path /* , options*/) { - if (isHtml(path)) { - return printEmbedHtml; - } - - if (isAngularComponentTemplate(path)) { - return printEmbedAngular; - } -} - -export default printHtml; +export { + isAngularComponentTemplate, + isEmbedHtml, + printEmbedAngular, + printEmbedHtml, +}; diff --git a/src/language-js/embed/index.js b/src/language-js/embed/index.js index 3af16fe40..0db04c197 100644 --- a/src/language-js/embed/index.js +++ b/src/language-js/embed/index.js @@ -1,8 +1,24 @@ import { label } from "../../document/builders.js"; -import printCss from "./css.js"; -import printGraphQL from "./graphql.js"; -import printHtml from "./html.js"; -import printMarkdown from "./markdown.js"; +import { isEmbedCss, printEmbedCss } from "./css.js"; +import { isEmbedGraphQL, printEmbedGraphQL } from "./graphql.js"; +import { + isAngularComponentTemplate, + isEmbedHtml, + printEmbedAngular, + printEmbedHtml, +} from "./html.js"; +import { isEmbedMarkdown, printEmbedMarkdown } from "./markdown.js"; + +const printers = [ + { test: isEmbedCss, print: printEmbedCss }, + { test: isEmbedGraphQL, print: printEmbedGraphQL }, + { test: isEmbedHtml, print: printEmbedHtml }, + { test: isAngularComponentTemplate, print: printEmbedAngular }, + { test: isEmbedMarkdown, print: printEmbedMarkdown }, +].map(({ test, print }) => ({ + test, + print: createTemplateLiteralPrint(print), +})); function embed(path) { const { node } = path; @@ -16,29 +32,28 @@ function embed(path) { return; } - let embedder; - for (const getEmbedder of [ - printCss, - printGraphQL, - printHtml, - printMarkdown, - ]) { - embedder = getEmbedder(path); + const printer = printers.find(({ test }) => test(path)); - if (!embedder) { - continue; - } - - // Special case: whitespace-only template literals - if (node.quasis.length === 1 && node.quasis[0].value.raw.trim() === "") { - return "``"; - } - - return async (...args) => { - const doc = await embedder(...args); - return doc && label({ embed: true, ...doc.label }, doc); - }; + if (!printer) { + return; } + + if ( + // Special case: whitespace-only template literals + node.quasis.length === 1 && + node.quasis[0].value.raw.trim() === "" + ) { + return "``"; + } + + return printer.print; +} + +function createTemplateLiteralPrint(print) { + return async (...args) => { + const doc = await print(...args); + return doc && label({ embed: true, ...doc.label }, doc); + }; } function hasInvalidCookedValue({ quasis }) { diff --git a/src/language-js/embed/markdown.js b/src/language-js/embed/markdown.js index 35de137fb..91c6d2bdd 100644 --- a/src/language-js/embed/markdown.js +++ b/src/language-js/embed/markdown.js @@ -34,17 +34,11 @@ function getIndentation(str) { return firstMatchedIndent === null ? "" : firstMatchedIndent[1]; } -function printMarkdown(path /* , options*/) { - if (isMarkdown(path)) { - return printEmbedMarkdown; - } -} - /** * md`...` * markdown`...` */ -function isMarkdown({ node, parent }) { +function isEmbedMarkdown({ node, parent }) { return ( parent?.type === "TaggedTemplateExpression" && node.quasis.length === 1 && @@ -53,4 +47,4 @@ function isMarkdown({ node, parent }) { ); } -export default printMarkdown; +export { isEmbedMarkdown, printEmbedMarkdown };