prettier/prettier

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

已關閉

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

 (3 則留言) (1 個反應) (0 位負責人)JavaScript (4,987 個分叉)batch import
area:idempotencyhelp wantedlang:javascript

倉庫指標

星標
 (52,204 顆星)
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.)

貢獻者指南