prettier/prettier

Embedded template-literal formatting is not idempotent for nested `.map()` inside a ternary (regression in 3.9.0)

Open

#19,518 建立於 2026年7月1日

在 GitHub 查看
 (1 留言) (0 反應) (0 負責人)JavaScript (4,716 fork)batch import
area:idempotencyhelp wantedlang:javascript

倉庫指標

Star
 (51,868 star)
PR 合併指標
 (平均合併 6天) (30 天內合併 186 個 PR)

描述

1st pass

Prettier v3.9.4 Playground link

--parser babel

Input:

const t = html`
  <ol>
    ${items.map(
      (entry) => html`
        <li>
          ${entry.children
            ? html`
                <ol>
                  ${entry.children.map(
                    (child) => html`<li>${child.title}</li>`,
                  )}
                </ol>
              `
            : entry.title}
        </li>
      `,
    )}
  </ol>
`;

Output:

const t = html`
  <ol>
    ${items.map(
      (entry) => html`
        <li>
          ${
            entry.children
              ? html`
                  <ol>
                    ${entry.children.map(
                    (child) => html`<li>${child.title}</li>`,
                  )}
                  </ol>
                `
              : entry.title
          }
        </li>
      `,
    )}
  </ol>
`;

Diff: https://www.diffchecker.com/TRI5AAxf/


2nd pass

Prettier v3.9.4 Playground link

--parser babel

Input:

const t = html`
  <ol>
    ${items.map(
      (entry) => html`
        <li>
          ${
            entry.children
              ? html`
                  <ol>
                    ${entry.children.map(
                    (child) => html`<li>${child.title}</li>`,
                  )}
                  </ol>
                `
              : entry.title
          }
        </li>
      `,
    )}
  </ol>
`;

Output:

const t = html`
  <ol>
    ${items.map(
      (entry) => html`
        <li>
          ${
            entry.children
              ? html`
                  <ol>
                    ${entry.children.map(
                      (child) => html`<li>${child.title}</li>`,
                    )}
                  </ol>
                `
              : entry.title
          }
        </li>
      `,
    )}
  </ol>
`;

Diff: https://www.diffchecker.com/ciW9nQQn/


Expected output: to be idempotent

Instead, output stabilizes only from the 3rd pass.

Why?

Running first prettier -w and then prettier -c on any file should succeed.

(Noticed via https://github.com/mdn/fred/pull/1686.)

貢獻者指南