Skip to content

Commit

Permalink
fix(html-in-js): do not add quotes for interpolation-only attributes (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ikatyang committed Nov 25, 2018
1 parent c788842 commit b6a04ab
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
12 changes: 12 additions & 0 deletions src/language-html/printer-html.js
Expand Up @@ -101,6 +101,18 @@ function embed(path, print, textToDoc, options) {
break;
}

// lit-html: html`<my-element obj=${obj}></my-element>`
if (
/^PRETTIER_PLACEHOLDER_\d+$/.test(
options.originalText.slice(
node.valueSpan.start.offset,
node.valueSpan.end.offset
)
)
) {
return concat([node.rawName, "=", node.value]);
}

const embeddedAttributeValueDoc = printEmbeddedAttributeValue(
node,
(code, opts) =>
Expand Down
5 changes: 2 additions & 3 deletions src/language-js/embed.js
Expand Up @@ -567,10 +567,9 @@ function isHtml(path) {
function printHtmlTemplateLiteral(path, print, textToDoc, parser) {
const node = path.getValue();

const placeholderPattern =
"prettierhtmlplaceholder(\\d+)redlohecalplmthreitterp";
const placeholderPattern = "PRETTIER_PLACEHOLDER_(\\d+)";
const placeholders = node.expressions.map(
(_, i) => `prettierhtmlplaceholder${i}redlohecalplmthreitterp`
(_, i) => `PRETTIER_PLACEHOLDER_${i}`
);

const text = node.quasis
Expand Down
6 changes: 6 additions & 0 deletions tests/multiparser_js_html/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -47,6 +47,8 @@ const someHtml1 = html\`<div > hello \${world} </div >\`;
const someHtml2 = /* HTML */ \`<div > hello \${world} </div >\`;
html\`\`
html\`<my-element obj=\${obj}></my-element>\`;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import { LitElement, html } from "@polymer/lit-element";
Expand Down Expand Up @@ -86,4 +88,8 @@ const someHtml2 = /* HTML */ \`
html\`\`;
html\`
<my-element obj=\${obj}></my-element>
\`;
`;
2 changes: 2 additions & 0 deletions tests/multiparser_js_html/lit-html.js
Expand Up @@ -44,3 +44,5 @@ const someHtml1 = html`<div > hello ${world} </div >`;
const someHtml2 = /* HTML */ `<div > hello ${world} </div >`;

html``

html`<my-element obj=${obj}></my-element>`;

0 comments on commit b6a04ab

Please sign in to comment.