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 add quotes for interpolation-only attributes #5544

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
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>`;