diff --git a/changelog_unreleased/handlebars/20030.md b/changelog_unreleased/handlebars/20030.md new file mode 100644 index 000000000..119a21469 --- /dev/null +++ b/changelog_unreleased/handlebars/20030.md @@ -0,0 +1,24 @@ +#### Fix invalid output for ` + +{{! Prettier stable }} + + +{{! Prettier main }} + +``` diff --git a/src/language-handlebars/embed.js b/src/language-handlebars/embed.js index 035cf942c..ff41c0beb 100644 --- a/src/language-handlebars/embed.js +++ b/src/language-handlebars/embed.js @@ -1,3 +1,5 @@ +import { isPlainTextStyleElement } from "./utilities.js"; + function embed(path /* , options*/) { const { node } = path; @@ -7,12 +9,7 @@ function embed(path /* , options*/) { const { parent } = path; - if (!( - parent.type === "ElementNode" && - parent.tag === "style" && - parent.children.length === 1 && - parent.children[0] === node - )) { + if (!(isPlainTextStyleElement(parent) && parent.children[0] === node)) { return; } diff --git a/src/language-handlebars/massage-ast/index.js b/src/language-handlebars/massage-ast/index.js index 76aa5f3f8..894e17dc9 100644 --- a/src/language-handlebars/massage-ast/index.js +++ b/src/language-handlebars/massage-ast/index.js @@ -1,4 +1,5 @@ import htmlWhitespace from "../../utilities/html-whitespace.js"; +import { isPlainTextStyleElement } from "../utilities.js"; function massageAstNode(original, cloned, parent) { // (Glimmer/HTML) ignore TextNode @@ -9,11 +10,7 @@ function massageAstNode(original, cloned, parent) { } // CSS will be formatted - if ( - parent.tag === "style" && - parent.children.length === 1 && - parent.children[0] === original - ) { + if (isPlainTextStyleElement(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 01abc509f..bda72ee3c 100644 --- a/src/language-handlebars/printer-glimmer.js +++ b/src/language-handlebars/printer-glimmer.js @@ -20,6 +20,7 @@ import { locEnd, locStart } from "./loc.js"; import { massageAstNode } from "./massage-ast/index.js"; import { hasPrettierIgnore, + isPlainTextStyleElement, isVoidElement, isWhitespaceNode, } from "./utilities.js"; @@ -59,11 +60,11 @@ function print(path, options, print) { } const endingTag = [""]; - const isStyle = node.tag === "style"; + const isPlainStyle = isPlainTextStyleElement(node); if ( node.children.length === 0 || - ((!isWhitespaceSensitive || isStyle) && + ((!isWhitespaceSensitive || isPlainStyle) && node.children.every((node) => isWhitespaceNode(node))) ) { return [startingTag, endingTag]; @@ -71,7 +72,7 @@ function print(path, options, print) { const parts = path.map(print, "children"); - if (isStyle || !isWhitespaceSensitive) { + if (isPlainStyle || !isWhitespaceSensitive) { return [startingTag, indent([softline, ...parts]), softline, endingTag]; } @@ -172,7 +173,7 @@ function print(path, options, print) { return replaceEndOfLine(text); } - if (parent.tag === "style") { + if (isPlainTextStyleElement(parent)) { text = text.replaceAll(/^\n+/g, ""); text = htmlWhitespace.trimEnd(text); text = htmlWhitespace.dedentString(text); diff --git a/src/language-handlebars/utilities.js b/src/language-handlebars/utilities.js index 8bccd6421..fd9c6c796 100644 --- a/src/language-handlebars/utilities.js +++ b/src/language-handlebars/utilities.js @@ -34,6 +34,18 @@ function isWhitespaceNode(node) { return node.type === "TextNode" && !/\S/.test(node.chars); } +function isPlainTextElement(node) { + return ( + node.type === "ElementNode" && + node.children.length === 1 && + node.children[0].type === "TextNode" + ); +} + +function isPlainTextStyleElement(node) { + return isPlainTextElement(node) && node.tag === "style"; +} + function isPrettierIgnoreNode(node) { return ( node?.type === "MustacheCommentStatement" && @@ -53,4 +65,9 @@ function hasPrettierIgnore(path) { ); } -export { hasPrettierIgnore, isVoidElement, isWhitespaceNode }; +export { + hasPrettierIgnore, + isPlainTextStyleElement, + isVoidElement, + isWhitespaceNode, +}; diff --git a/tests/format/handlebars/style-tag/__snapshots__/format.test.js.snap b/tests/format/handlebars/style-tag/__snapshots__/format.test.js.snap index 117fc5dbc..c9ec2a8cb 100644 --- a/tests/format/handlebars/style-tag/__snapshots__/format.test.js.snap +++ b/tests/format/handlebars/style-tag/__snapshots__/format.test.js.snap @@ -13,10 +13,14 @@ parsers: ["glimmer"] + + =====================================output=====================================
+ + ================================================================================ `; @@ -195,3 +199,38 @@ parsers: ["glimmer"] ================================================================================ `; + +exports[`with-mustache.hbs format 1`] = ` +====================================options===================================== +parsers: ["glimmer"] + printWidth: 80 (default) | +=====================================input====================================== + + + + + + +=====================================output===================================== + + + + + +================================================================================ +`; diff --git a/tests/format/handlebars/style-tag/empty.hbs b/tests/format/handlebars/style-tag/empty.hbs index 6647277e5..350609817 100644 --- a/tests/format/handlebars/style-tag/empty.hbs +++ b/tests/format/handlebars/style-tag/empty.hbs @@ -5,3 +5,5 @@ + + diff --git a/tests/format/handlebars/style-tag/with-mustache.hbs b/tests/format/handlebars/style-tag/with-mustache.hbs new file mode 100644 index 000000000..c8b9b8506 --- /dev/null +++ b/tests/format/handlebars/style-tag/with-mustache.hbs @@ -0,0 +1,12 @@ + + + + +