Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(html-in-js): do not break empty content #5355

Merged
merged 3 commits into from Nov 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/language-js/embed.js
Expand Up @@ -582,6 +582,10 @@ function printHtmlTemplateLiteral(path, print, textToDoc, parser) {

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

if (expressionDocs.length === 0 && text.trim().length === 0) {
return "``";
}

const contentDoc = mapDoc(
stripTrailingHardline(textToDoc(text, { parser })),
doc => {
Expand Down Expand Up @@ -626,7 +630,9 @@ function printHtmlTemplateLiteral(path, print, textToDoc, parser) {
}
);

return concat(["`", indent(concat([hardline, contentDoc])), softline, "`"]);
return group(
concat(["`", indent(concat([hardline, group(contentDoc)])), softline, "`"])
);
}

module.exports = embed;
4 changes: 4 additions & 0 deletions tests/multiparser_js_html/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -45,6 +45,8 @@ customElements.define('my-element', MyElement);

const someHtml1 = html\`<div > hello \${world} </div >\`;
const someHtml2 = /* HTML */ \`<div > hello \${world} </div >\`;

html\`\`
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import { LitElement, html } from "@polymer/lit-element";

Expand Down Expand Up @@ -82,4 +84,6 @@ const someHtml2 = /* HTML */ \`
<div>hello \${world}</div>
\`;

html\`\`;

`;
2 changes: 2 additions & 0 deletions tests/multiparser_js_html/lit-html.js
Expand Up @@ -42,3 +42,5 @@ customElements.define('my-element', MyElement);

const someHtml1 = html`<div > hello ${world} </div >`;
const someHtml2 = /* HTML */ `<div > hello ${world} </div >`;

html``