Skip to content

feat(fmt): use SWC Next parser - #464

Merged
chenjiahan merged 9 commits into
mainfrom
codex/poc-fmt-swc-next-npm
Sep 22, 2026
Merged

chenjiahan merged 9 commits into
mainfrom
codex/poc-fmt-swc-next-npm

Conversation

@SoonIter

@SoonIter SoonIter commented Sep 7, 2026

Copy link
Copy Markdown
Member

Motivation

Use @swc-next/parser@0.2.2 as the default JavaScript, JSX and TypeScript parser in rs fmt.

Changes

  • Reuse the existing Prettier adapter and ESTree printer, with SWC Next's flat comments and parser diagnostics.
  • Preserve explicit Prettier parser selection and project plugin precedence.
  • Replace Yuku dependencies and update Renovate, release-age exclusions, documentation and attribution.

Validation

Check Result
Full test suite 388 passed, one existing skipped test
Builds, lint, types, formatting and spelling Passed
Packed package Resolves @swc-next/parser to 0.2.2
Rsbuild JS/TS corpus 2,321 files match both main's Yuku adapter and native Prettier; all results are idempotent

The corpus check used Rsbuild f69eb5a, Yuku 0.10.2 and Prettier 3.9.7, honoring its existing JS/TS fixture exclusions. It did not modify repository files or measure performance.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Sep 7, 2026

Copy link
Copy Markdown

Deploying rstack-cli with  Cloudflare Pages  Cloudflare Pages

Latest commit: 889d13e
Status: ✅  Deploy successful!
Preview URL: https://727ecb63.rstack-cli.pages.dev
Branch Preview URL: https://codex-poc-fmt-swc-next-npm.rstack-cli.pages.dev

View logs

SoonIter commented Sep 7, 2026

Copy link
Copy Markdown
Member Author

Known compatibility difference in swc-next 0.2.0, found by the existing rs fmt tests for .d.ts, .d.mts, and .d.cts:

export function value() { return 1; }

This is valid in a normal .ts file, but declaration files should contain a signature such as export function value(): number;. swc-next 0.2.0 currently returns no diagnostics for the implementation even with lang: "dts" explicitly selected, so this is not a filename-to-language mapping issue.

The previous Yuku adapter reported this with semanticErrors: false, and Babel reports it with its TypeScript dts: true option. TypeScript reports TS1183 during semantic checking when declaration checking is enabled (skipLibCheck: false); its syntactic diagnostics alone do not report it.

Upstream draft fix: https://github.com/swc-project/swc-next/pull/631. It proposes extending the existing ambient-implementation diagnostic to inherited ambient contexts, with regression tests and four additional passing conformance cases.

This PoC remains pinned to 0.2.0, and its tests explicitly record the current acceptance difference. The draft fix is not included in that published version; the rejection expectation can be restored after upgrading to a release containing it.

@SoonIter
SoonIter force-pushed the codex/poc-fmt-swc-next-npm branch from 0016242 to dae1f31 Compare September 22, 2026 03:54
@SoonIter SoonIter changed the title feat(fmt): prototype SWC Next 0.2.0 via npm feat(fmt): prototype SWC Next 0.2.2 via npm Sep 22, 2026
@SoonIter SoonIter changed the title feat(fmt): prototype SWC Next 0.2.2 via npm feat(fmt): use SWC Next parser Sep 22, 2026
@SoonIter
SoonIter marked this pull request as ready for review September 22, 2026 04:18
Copilot AI lite review requested due to automatic review settings September 22, 2026 04:18
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 22, 2026

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review Completed 2026-09-22T07:58:20.674886Z 6269c42 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

The broad @swc-next/* release-age exemption must be scoped to the pinned packages and version.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 1 High severity

Open (1)
What changed in this PR

Migrates rs fmt to use @swc-next/parser@0.2.2 by default while retaining Prettier compatibility and replacing Yuku.

Changes:

  • Added SWC Next parser integration and tests.
  • Updated dependencies, lockfile, Renovate rules, and attribution.
  • Updated English and Chinese formatting documentation.
File Reviewed changes
website/​docs/​zh/​guide/​formatting.mdx Updated Chinese formatting documentation.
website/​docs/​en/​guide/​formatting.mdx Updated English formatting documentation.
pnpm-workspace.yaml Updated dependency and release-age configuration. Critical: the @swc-next/* exemption is broader than the pinned dependency and should be scoped.
pnpm-lock.yaml Replaced Yuku packages with SWC Next dependencies.
packages/​rstack/​THIRD_PARTY_NOTICES.md Updated third-party attribution.
packages/​rstack/​tests/​fmt/​swcNextPlugin.test.ts Added and updated parser integration coverage.
packages/​rstack/​src/​fmt/​swcNextPlugin.ts Implemented the SWC Next parser adapter.
packages/​rstack/​src/​fmt/​prettierPlugins.ts Registered SWC Next as the default parser plugin.
packages/​rstack/​package.json Updated runtime dependencies.
.github/​renovate.json Updated dependency maintenance rules.
Files not reviewed (1)
  • pnpm-lock.yaml: Generated file

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread pnpm-workspace.yaml
@SoonIter

SoonIter commented Sep 22, 2026

Copy link
Copy Markdown
Member Author

The upstream fixes included in @swc-next/parser@0.2.2 improve rs fmt as follows:

Fix Actual improvement for rs fmt
swc-next#635 — Typed arrows in nested conditionals Compared with Yuku 0.10.2: formats valid expressions that previously failed, with output matching native Prettier TypeScript.
swc-next#636 — Bare call signatures Compared with Yuku 0.10.2: reports an error for standalone TS instead of returning empty output; preserves MDX code blocks instead of clearing them.
swc-next#631 — Ambient function bodies Compared with SWC Next 0.2.0: correctly rejects function implementations in declaration files. Recovery also preserves surrounding scopes; relative to Yuku, both formatters already reject the invalid input.
Verified inputs, outputs, and implementation details

Re-tested the three upstream fixes included in @swc-next/parser@0.2.2 against Yuku 0.10.2, using this PR at 7195815 and the adapter from base e8210f0, with Prettier 3.9.7. Parser comparisons use module mode, the matching language mode, and Yuku's semanticErrors: false.

These fixes are implemented upstream; this PR brings them into rs fmt.

1. Nested conditional + typed arrow — swc-next#635

Input (input.ts):

cond ? inner ? items.map((item): Item => item) : (a) : b => b;
Implementation Actual result
Yuku / main Expected ')' after function arguments, but found ':' (1:32); formatting fails.
SWC Next / this PR No diagnostics; preserves the typed callback and the outer alternate b => b.

Formatted output with this PR, also identical to native Prettier TypeScript:

cond ? (inner ? items.map((item): Item => item) : a) : (b) => b;

The simpler cond ? items.map((item): Item => item) : undefined already works in Yuku 0.10.2. The nested case above demonstrates the additional improvement. Yuku's conditional retry applies the return-type restriction to nested expressions; #635 restores permission at independent expression boundaries such as call arguments.

2. Bare call signature: prevent silent content loss — swc-next#636

Input (input.ts):

(name: string): void;
Implementation Parser output Formatter output
Yuku / main diagnostics: [], program.body: [] "" — the signature disappears.
SWC Next / this PR Expected '=>' in arrow function, but found ';' Throws at (1:21) instead of producing empty output.

The same content inside an MDX code fence:

```ts
(name: string): void;
```

Main produces an empty fence:

```ts

```

This PR preserves the original fence and signature unchanged. Prettier retains embedded source when its parser rejects it. Yuku's missing-arrow branch returns no node without a diagnostic; #636 adds the diagnostic for a committed arrow head.

3. Ambient function bodies: preserve scope during recovery — swc-next#631

Compared with SWC Next 0.2.0, #631 adds the missing diagnostic for inputs such as export function value() { return 1; } in input.d.ts, so rs fmt rejects the invalid declaration file. Yuku already reports that error; the example below demonstrates the additional AST recovery improvement relative to Yuku.

Input (input.d.ts):

// Keep the namespace on one line to exercise closing-brace recovery.
declare namespace N { function value() { return 1; } const after: number; }
declare const outside: number;
Implementation Diagnostics Recovered AST structure
Yuku 0.10.2 Ambient implementation error plus Unexpected token '}' N.body is empty; after is incorrectly placed at the top level next to outside.
SWC Next 0.2.2 Only the ambient implementation error N.body contains value as TSDeclareFunction with body: null, followed by after; outside stays at the top level.

Yuku returns immediately at the invalid body's opening brace. #631 consumes that body before recovering a bodyless signature, so its } is not mistaken for the namespace's closing brace.

This is a parser recovery improvement: both formatter adapters still reject this invalid declaration file. The function body is deliberately omitted from SWC Next's recovered AST; it is not formatted as valid code.

Comment thread packages/rstack/tests/fmt/swcNextPlugin.test.ts
@SoonIter
SoonIter requested review from Timeless0911 and removed request for Timeless0911 September 22, 2026 05:56
@SoonIter
SoonIter requested a review from chenjiahan September 22, 2026 06:09
@SoonIter

Copy link
Copy Markdown
Member Author

Benchmarked the SWC Next and Yuku adapters bundled in rs fmt against the same Rsbuild checkout.

Corpus Files Yuku 0.10.2 median SWC Next 0.2.2 median SWC Next paired time change¹
Whole repository 3,349 1.495 s 1.538 s +2.9%
JS/TS only 2,320 0.663 s 0.678 s +4.0%

SWC Next was slightly slower in this run. It was faster in 6/20 whole-repository pairs and 5/20 JS/TS pairs. These results describe this machine and corpus, not parser performance in general.

Setup

  • Apple M5 Max, macOS arm64, Node 24.20.0.
  • Both variants use Prettier 3.9.7 and the same native Rstack binding.
  • SWC Next: this PR at 6269c42; Yuku: the common base e8210f0.
  • Rsbuild: f69eb5a, using its existing configuration and exclusions. packages/core/src/inspectConfig.ts is included.
  • 3 warmup rounds and 20 measured rounds per variant and corpus. Fresh CLI processes ran serially, with balanced, shuffled variant order. Node's compile cache was disabled.
node <variant>/packages/rstack/bin/rs.js fmt --check --no-cache --parallel-workers 8 .
node <variant>/packages/rstack/bin/rs.js fmt --check --no-cache --parallel-workers 8 '**/*.{js,jsx,mjs,cjs,ts,tsx,mts,cts}'

All 92 runs, including warmups, passed. Both variants discovered identical file manifests and produced output identical to the source files; source hashes were unchanged afterward. Times include CLI startup, discovery, worker startup, parsing and Prettier printing, but exclude file write-back.

¹ Median of the 20 per-round percentage changes; it is not the percentage difference between the two independently calculated medians. Positive values mean SWC Next took longer.

@SoonIter

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: 6269c427d8

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread website/docs/en/guide/formatting.mdx Outdated
@SoonIter
SoonIter requested a review from chenjiahan September 22, 2026 08:12

@chenjiahan chenjiahan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@chenjiahan
chenjiahan merged commit 126694f into main Sep 22, 2026
5 checks passed
@chenjiahan
chenjiahan deleted the codex/poc-fmt-swc-next-npm branch September 22, 2026 08:30
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.

3 participants