From 2529b928181d4b0c47518fbcbc307c3c8e51dbc9 Mon Sep 17 00:00:00 2001 From: Konv Suu Date: Sat, 12 Sep 2026 20:31:10 +0800 Subject: [PATCH] Format ` + +{{! Prettier stable }} + + +{{! Prettier main }} + +``` diff --git a/src/language-handlebars/embed.js b/src/language-handlebars/embed.js index ff41c0beb..4d46e2cab 100644 --- a/src/language-handlebars/embed.js +++ b/src/language-handlebars/embed.js @@ -1,4 +1,40 @@ -import { isPlainTextStyleElement } from "./utilities.js"; +import { isPlainTextStyleOrScriptElement } from "./utilities.js"; + +function getAttribute(node, name) { + return node.attributes.find( + (attribute) => attribute.type === "AttrNode" && attribute.name === name, + ); +} + +function getTextValue(attribute) { + return attribute.value.type === "TextNode" + ? attribute.value.chars + : undefined; +} + +function getScriptTextToDocOptions(node) { + if (getAttribute(node, "src")) { + return; + } + + const typeAttribute = getAttribute(node, "type"); + const type = typeAttribute ? getTextValue(typeAttribute) : undefined; + + if (!typeAttribute || type === "module" || type === "text/javascript") { + return { + parser: "babel", + __embeddedInHtml: true, + __babelSourceType: type === "module" ? "module" : "script", + }; + } +} + +function getStyleTextToDocOptions(node) { + const langAttribute = getAttribute(node, "lang"); + if (!langAttribute || getTextValue(langAttribute) === "css") { + return { parser: "css" }; + } +} function embed(path /* , options*/) { const { node } = path; @@ -9,30 +45,27 @@ function embed(path /* , options*/) { const { parent } = path; - if (!(isPlainTextStyleElement(parent) && parent.children[0] === node)) { + if (!( + isPlainTextStyleOrScriptElement(parent) && parent.children[0] === node + )) { return; } - const languageAttribute = parent.attributes.find( - (attribute) => attribute.type === "AttrNode" && attribute.name === "lang", - ); - if ( - languageAttribute && - !( - languageAttribute.value.type === "TextNode" && - languageAttribute.value.chars === "css" - ) - ) { + const textToDocOptions = + parent.tag === "style" + ? getStyleTextToDocOptions(parent) + : getScriptTextToDocOptions(parent); + + if (!textToDocOptions) { return; } return async (textToDoc) => { - const context = node.chars; - if (!context.trim()) { + const content = node.chars; + if (!content.trim()) { return ""; } - const doc = await textToDoc(context, { parser: "css" }); - return doc; + return await textToDoc(content, textToDocOptions); }; } diff --git a/src/language-handlebars/massage-ast/index.js b/src/language-handlebars/massage-ast/index.js index 894e17dc9..226439819 100644 --- a/src/language-handlebars/massage-ast/index.js +++ b/src/language-handlebars/massage-ast/index.js @@ -1,5 +1,5 @@ import htmlWhitespace from "../../utilities/html-whitespace.js"; -import { isPlainTextStyleElement } from "../utilities.js"; +import { isPlainTextStyleOrScriptElement } from "../utilities.js"; function massageAstNode(original, cloned, parent) { // (Glimmer/HTML) ignore TextNode @@ -9,8 +9,11 @@ function massageAstNode(original, cloned, parent) { return null; } - // CSS will be formatted - if (isPlainTextStyleElement(parent) && parent.children[0] === original) { + // CSS/JS will be formatted + if ( + isPlainTextStyleOrScriptElement(parent) && + parent.children[0] === original + ) { cloned.chars = ""; } else { cloned.chars = htmlWhitespace.split(trimmed).join(" "); diff --git a/src/language-handlebars/printer-glimmer.js b/src/language-handlebars/printer-glimmer.js index bda72ee3c..f771f50ab 100644 --- a/src/language-handlebars/printer-glimmer.js +++ b/src/language-handlebars/printer-glimmer.js @@ -20,7 +20,7 @@ import { locEnd, locStart } from "./loc.js"; import { massageAstNode } from "./massage-ast/index.js"; import { hasPrettierIgnore, - isPlainTextStyleElement, + isPlainTextStyleOrScriptElement, isVoidElement, isWhitespaceNode, } from "./utilities.js"; @@ -60,11 +60,11 @@ function print(path, options, print) { } const endingTag = [""]; - const isPlainStyle = isPlainTextStyleElement(node); + const isPlainStyleOrScript = isPlainTextStyleOrScriptElement(node); if ( node.children.length === 0 || - ((!isWhitespaceSensitive || isPlainStyle) && + ((!isWhitespaceSensitive || isPlainStyleOrScript) && node.children.every((node) => isWhitespaceNode(node))) ) { return [startingTag, endingTag]; @@ -72,7 +72,7 @@ function print(path, options, print) { const parts = path.map(print, "children"); - if (isPlainStyle || !isWhitespaceSensitive) { + if (!isWhitespaceSensitive || isPlainStyleOrScript) { return [startingTag, indent([softline, ...parts]), softline, endingTag]; } @@ -163,7 +163,8 @@ function print(path, options, print) { case "TextNode": { // Don't format content: // 1. in `
`,
-      // 2. in `