Repository navigation
feat(formatter): support range formatting for JS/TS and Markdown - #1819
magic-akari wants to merge 8 commits into
Conversation
Expand a selection to statements in the original program so range edits preserve attached comments, ignore directives, and semicolon boundaries. Return replacement edits from the Rust source and AST APIs. Pass the selected Doc and its UTF-16 bounds between npm bindings so the native host can print it at its original column. Use the same selection with Prettier hosts that implement getRangeNodes.
The JavaScript parser wraps the source text, so quote style changes appear as AST differences. Return a constant comparison value until the plugin provides a normalized ESTree program.
Expand a selection to complete Markdown blocks so list markers and fence delimiters stay consistent. Resolve references against the full document and dispatch only embedded blocks within the selection. Expose source and AST range edits in Rust. Pass the selected Doc and its UTF-16 bounds to the native and Prettier hosts.
Deploying swc-next with
|
| Latest commit: |
8aea26b
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://08561392.swc-next.pages.dev |
| Branch Preview URL: | https://feat-js-formatter-range.swc-next.pages.dev |
Binary size diffLinux x64 GNU · release · uncompressed, stripped bindings. Merge:
|
Gungraun benchmark diffBase SHA: f6f4151 Values are shown as Download the raw Gungraun and Valgrind data (retained for 90 days). Callgrind metric changesCallgrind instruction counts (Ir) for the compare and base revisions. Values are shown as compare / base in K (1,000), truncated to whole thousands. Multi-part profiles use the part numbers from Gungraun and include an aggregated Total. New and Removed identify benchmarks that exist on only one side; the missing value is shown as a dash. Percentages are rounded to at most three decimal places. Lower is better: 🟢 improvement, 🔴 regression, ⚪ unchanged. Formatter
Generator
Minifier
Parser
Semantic
Transform
Type strip
Visit
Benchmark results |
JavaScript API Gungraun benchmark diffBase: Values are shown as Download the raw Gungraun and Valgrind data (retained for 90 days). Callgrind metric changesMedian instruction counts from three processes, shown as compare / base in K (1,000), truncated to whole thousands. Percentages are rounded to at most three decimal places. Lower is better: 🟢 improvement, 🔴 regression, ⚪ unchanged or overlapping ranges (inconclusive). JIT disabled (--jitless); predictable mode enabled (--predictable). Counts are not wall time. Exact counts, ranges and profiles are retained in artifacts. Generate
Lang
Parse
Source type
Walk
Benchmark resultsExact instruction counts: median (min–max).
|
Highlighted code frames contain escape sequences between source tokens. Strip them before comparing the reported source line for whole-file and range formatting.
Specialize JavaScript statement traversal for whole-file sequences without source-line constraints. Handle the first selected Markdown block at the range entry point and render ordinary blocks directly during traversal.
Select the enclosing block when a range touches its braces or contains only whitespace between its statements. Keep comments outside the edit and preserve class members when only their indentation is selected. Handle Prettier range offsets and markers in JS, TS, and Markdown conformance. Compare the full file after applying each edit and record the intentional differences in class indentation and line endings.
Codex Review Summary: ❌ FailedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits. |
Include the starting line and column in syntax error messages. Preserve the full source location on the error for programmatic callers.
Range selections use offsets from the original source. Only whole-file cases that produce output run a second pass, so use the whole-file case count as the upper bound for the reported denominator.
Motivation
Editors need to format a selection while preserving the surrounding text. Selecting the first of two declarations should produce a single edit to that declaration. The next statement can still affect the edit. For example, even with
semi: false, a declaration needs its trailing semicolon when the next line starts with[0].forEach(log).Changes
format_rangeandformat_ast_rangeto the Rust JS/TS and Markdown APIs. Both accept a single half-open range in UTF-8 bytes and return at most one text edit.formatRangefrom the native npm language plugins. It returns a replacement Doc and its UTF-16 bounds. The native and Wasm hosts useprintRangeto render the edit at its original column.For JS/TS, Prettier’s
--debug-checkstill checks that formatting twice produces the same output. AST comparison is deferred until the plugin can return a normalized ESTree program.Host compatibility
Rust and native/Wasm npm hosts. These hosts call SWC’s range formatter directly. They support JS/TS and Markdown selections independently of Prettier.
Prettier plugin. After this PR merges,
@swc-next/formatter-prettier-pluginusers on Prettier 3.10.1 or 3.10.2 will still have whole-file formatting only. Range formatting through Prettier requires theprinter.getRangeNodeshook proposed in prettier/prettier#20264.The adapter uses the proposed hook to pass the full source and the user’s original selection to Rust. We verified this behavior with the preview build at prettier/prettier@51839f643. If the hook’s signature or range-selection behavior changes before release, we will need to update the SWC Prettier plugin to match the final API.
Related Links