Skip to content

refactor(fmt): use the official SWC Next formatter plugin - #648

Draft
SoonIter wants to merge 1 commit into
mainfrom
refactor-official-swc-next-prettier-plugin
Draft

SoonIter wants to merge 1 commit into
mainfrom
refactor-official-swc-next-prettier-plugin

Conversation

@SoonIter

@SoonIter SoonIter commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

Motivation

Move JavaScript and TypeScript AST-to-document formatting into Rust while keeping Prettier's document renderer, configuration, and plugin integration. This replaces the local SWC Next parser adapter and Prettier ESTree printer with the official SWC Next v0.9.1 Prettier plugin.

Changes

  • Use the plugin's /javascript entry for standalone JS, JSX, TS and TSX and their Markdown/MDX code fences. Prettier still formats the Markdown/MDX host, including MDX import/export and JSX nodes. The upstream /markdown entry is not used.
  • Keep swc-next and swc-next-ts parser aliases, explicit Prettier parsers, and project parser-plugin precedence. Disabled embedded formatting still leaves fences untouched; Prettier preserves rejected snippets. Vue and Svelte keep their existing paths.
  • Stay on Prettier 3.10.1, which is the peer declared by v0.9.1. No peer override is needed.

Rsbuild results

Compared the parser-only v0.9.1 baseline with the official v0.9.1 formatter plugin at da57827. Both use Prettier 3.10.1. Input: Rsbuild ad8388c, with its own configuration and heading-case plugin.

Scope Files Parser only Official formatter Time saved
JS/TS 2,404 644 ms 458 ms 28.8%
Markdown/MDX 477 920 ms 879 ms 4.5%
All selected supported files 3,440 1,485 ms 1,268 ms 14.6%

Medians from nine alternating-order runs per scope after two warmups per version. Each run launches rs fmt --check --no-cache with eight workers on the same file list and unchanged inputs (Apple M5 Max, macOS 26.6.2, Node 24.20.0). These are end-to-end formatting times, not isolated formatter or Rsbuild build times. The Markdown/MDX gain is smaller and the measured ranges overlap. The previously excluded source-phase import fixture is included this time; there is no new oxfmt measurement.

Behavior and validation

  • Both versions formatted all 3,440 files and passed a second check. Only 7 standalone TypeScript files differ between outputs, all in method-chain wrapping; Babel ASTs and comments match after removing positional metadata. All 477 Markdown/MDX files are byte-identical between versions. All seven previously reported reduced layout examples now match the parser-only baseline. See the remaining layout differences.
  • import source now formats on both paths, including the full Rsbuild fixture. Ambient module import attributes still error on the parser-only path and format without dropping attributes with the official plugin.
  • Embedded HTML/CSS/GraphQL in JS/TS templates keeps its original layout on the official path. Standalone SWC formatting still rejects range, pragma, and certain layout options. Choose parser: 'babel' or parser: 'typescript' for Prettier's embedded-language support and those options.
  • Strict frozen-lockfile install, JS/native builds, pnpm check, the website build, and the full Rstack test suite passed (467 passed, 1 skipped). The Rsbuild runtime test suite was not run.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Deploying rstack-cli with  Cloudflare Pages  Cloudflare Pages

Latest commit: da57827
Status: ✅  Deploy successful!
Preview URL: https://37ae13ff.rstack-cli.pages.dev
Branch Preview URL: https://refactor-official-swc-next-p.rstack-cli.pages.dev

View logs

@SoonIter SoonIter changed the title refactor(fmt): POC official SWC Next Prettier plugin refactor(fmt): POC SWCFMT without automatic fallback Nov 16, 2026
@SoonIter SoonIter changed the title refactor(fmt): POC SWCFMT without automatic fallback refactor(fmt): use the official SWC Next formatter plugin Nov 17, 2026
@SoonIter

Copy link
Copy Markdown
Member Author

@SoonIter

SoonIter commented Nov 17, 2026 •

Copy link
Copy Markdown
Member Author

Remaining differences from the parser-only integration

Compared the parser-only integration on main (674d2fa) with this PR (da57827), both using SWC Next 0.9.1 and Prettier 3.10.1.

Case Before → After
Range formatting: positive rangeStart or any finite rangeEnd Prettier handles the range → the SWC path rejects the option.
Pragma options: requirePragma, insertPragma, checkIgnorePragma Prettier honors the option → the SWC path rejects it.
Layout options: objectWrap: 'collapse', experimentalTernaries: true, experimentalOperatorPosition: 'start' Prettier honors the option → the SWC path rejects it.
Embedded HTML/CSS/GraphQL inside JS/TS templates The contents are formatted → the contents stay as written, even with embeddedLanguageFormatting enabled. For example, css`a{color:red}` stays compact.

Formatting twice can change the output. The parser-only path is stable on the first pass for these examples; the SWCFMT path is not:

  • swc-next-issues#793: items.map((item) => item.id).join({ ... }) with a long object literal on one input line. The first pass wraps the object; the second also splits the chain (printWidth: 80).
  • swc-next-issues#740: <p>Hello Done. <b>bold</b> x</p> at printWidth: 15. The second pass puts bold on a separate line.
  • swc-next-issues#801: a parenthesized ternary used as another ternary's condition, with a long && expression and an extra pair of parentheses. The second pass changes the indentation of the && operands (printWidth: 40).

Layout-only differences in Rsbuild. Seven TypeScript files differ; their normalized ASTs and comments match, and both outputs are stable:

SoonIter commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

Follow-up to the 0.9.1 behavior comparison, excluding the template embedding issue:

  • Formatting is not stable on the first pass. The parser-only path is stable for all three reproductions, but the official plugin changes the output on a second pass: a callback chain ending in a long object (#793), JSX text with an inline element at a narrow print width (#740), and a nested ternary with extra parentheses (#801). All three are tracked upstream.
  • Existing configurations can error. Range formatting (rangeStart/rangeEnd), pragma options, and the layout options objectWrap: 'collapse', experimentalTernaries: true, and experimentalOperatorPosition: 'start' are documented limitations of the plugin. I did not find an upstream issue tracking those exact options. For this PR, we can preserve the existing Prettier path when a project explicitly requests one of them instead of failing after an upgrade.
  • Layout-only differences. Seven Rsbuild TypeScript files differ in where method chains wrap. Both outputs are stable, and their normalized ASTs and comments match. The closest upstream reports are #877 for chains ending in a multiline object and #790 for chains ending in an array. I would not block the default switch on these layout-only differences.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant