Skip to content

Commit 40ac851

Browse files
authored
fix(html-in-js): do not break empty content (#5355)
1 parent a4e8aaf commit 40ac851

3 files changed

Lines changed: 13 additions & 1 deletion

File tree

src/language-js/embed.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,10 @@ function printHtmlTemplateLiteral(path, print, textToDoc, parser) {
582582

583583
const expressionDocs = path.map(print, "expressions");
584584

585+
if (expressionDocs.length === 0 && text.trim().length === 0) {
586+
return "``";
587+
}
588+
585589
const contentDoc = mapDoc(
586590
stripTrailingHardline(textToDoc(text, { parser })),
587591
doc => {
@@ -626,7 +630,9 @@ function printHtmlTemplateLiteral(path, print, textToDoc, parser) {
626630
}
627631
);
628632

629-
return concat(["`", indent(concat([hardline, contentDoc])), softline, "`"]);
633+
return group(
634+
concat(["`", indent(concat([hardline, group(contentDoc)])), softline, "`"])
635+
);
630636
}
631637

632638
module.exports = embed;

tests/multiparser_js_html/__snapshots__/jsfmt.spec.js.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ customElements.define('my-element', MyElement);
4545
4646
const someHtml1 = html\`<div > hello \${world} </div >\`;
4747
const someHtml2 = /* HTML */ \`<div > hello \${world} </div >\`;
48+
49+
html\`\`
4850
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4951
import { LitElement, html } from "@polymer/lit-element";
5052
@@ -82,4 +84,6 @@ const someHtml2 = /* HTML */ \`
8284
<div>hello \${world}</div>
8385
\`;
8486
87+
html\`\`;
88+
8589
`;

tests/multiparser_js_html/lit-html.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,5 @@ customElements.define('my-element', MyElement);
4242

4343
const someHtml1 = html`<div > hello ${world} </div >`;
4444
const someHtml2 = /* HTML */ `<div > hello ${world} </div >`;
45+
46+
html``

0 commit comments

Comments
 (0)