Refactor js embed print to use test/print pattern (#17963)

This commit is contained in:
fisker Cheung authored and GitHub committed 2025-09-25 09:00:10 +08:00
1 parent be9adeb38b
commit 9e60e840dd
5 files changed
+57 -64

No files matched your search

+6 -11
View File
@@ -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 };
+2 -8
View File
@@ -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 };
+7 -12
View File
@@ -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,
};
+40 -25
View File
@@ -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 }) {
+2 -8
View File
@@ -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 };