Skip to content

refactor!: parse with oxc instead of swc - #908

Draft
dsherret wants to merge 1 commit into
mainfrom
oxc
Draft

dsherret wants to merge 1 commit into
mainfrom
oxc

Conversation

@dsherret

@dsherret dsherret commented Nov 9, 2026

Copy link
Copy Markdown
Member

Replaces swc and dprint-swc-ext with oxc 0.158, since Deno wants to switch to oxc.

This is a draft until the oxc fixes below are done. 9 spec files fail on syntax oxc can't parse yet, and those spec cases are kept as they are on main.

Changes

  • src/ast is a small view layer like dprint-swc-ext, inlined into this repo:
    • Node is oxc's AstKind.
    • Parent lookups, tokens, comments, and line/column positions.
    • Conversions from every oxc struct and enum to Node, generated by scripts/generate_ast_nodes.ts (rerun it after upgrading oxc).
  • Comments use the same semantics as dprint-swc-ext: leading and trailing comments are every comment between two tokens.
  • generate.rs is split into sections under src/generation/generate. The formatting logic is unchanged apart from the AST access.
  • The Rust toolchain is updated to 1.99.1 (oxc needs 1.97+).

Breaking API changes

  • format_program takes an oxc &Program and the tokens from parsing with TokensParserConfig. Programs parsed elsewhere should use parsing::parse_options() and check their diagnostics with is_unsupported_syntax_error before formatting.
  • format_parsed_source no longer takes a syntax.
  • The dprint_swc_ext re-export is removed.

Behavior changes

  • oxc recovers from some syntax errors by dropping text (ex. 10._1;). These now error instead of formatting. Formatting also errors whenever text other than whitespace isn't covered by a token or comment.
  • Fixes - -1 being formatted as --1.

Results

  • All other spec files, the unit tests, and the foreign source tests pass.
  • On the ~91k files in the Deno repo:
    • No panics, compared to 3 on main.
    • Files that change when formatted again or stop parsing after formatting went from 166 to 2 (tests/corpus_baselines/deno.txt).
    • 160 files format differently than on main. Most are fixes for main changing the code, such as dropping array holes in [x, ,] = it, dropping static from static constructor(), and turning void yield into void ;.
  • About 37% faster end to end.

Needed in oxc before landing

  • H1: { a = 1 } in an object literal loses the = 1. The parser recovers with "Invalid assignment in object literal", but the initializer only stays in the parser's internal cover_initialized_name state and isn't in the AST, so formatting would drop it. It needs to be in the AST along with the recoverable error. Specs: ObjectExpression_All and 3 cases in Range_Objects.
  • H2: this as a parameter that isn't the first one (ex. function test(testing, this) {}) is a fatal error ("'this' is a reserved word that cannot be used here"). TypeScript parses it and only reports it in the type checker. Specs: ClassMethod_All, Function_All, FunctionExpression_All, FunctionExpression_PreferHanging_Never, and ObjectMethod_All.
  • H3: Computed enum member names other than string literals and templates without substitutions (ex. [e] = 1, [f + g] = 4) are a fatal error ("Computed property names are not allowed in enums"). TypeScript parses them. TSEnumMemberName has no variant that can hold them, so this needs an AST change. Spec: EnumMember_All.
  • M1: export v from "mod" (the export default from proposal) isn't supported ("Unexpected token"). swc parsed it. Spec: Range_FileTypes_Js.
  • L1: Not blocking: is_unsupported_syntax_error finds the recovered errors that lose text by matching message prefixes, which breaks if oxc rewords a message. Error codes or a flag on these diagnostics would make that check reliable.

Replaces swc and dprint-swc-ext with oxc 0.158. A small view layer like
dprint-swc-ext is now in `src/ast` (parents, tokens, comments, and
positions over oxc's `AstKind`), and generate.rs is split into sections
under `src/generation/generate`. The Rust toolchain is updated to 1.99.1.

`format_program` now takes an oxc `Program` and its tokens, and
`format_parsed_source` no longer takes a syntax.
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