Skip to content

feat(BREAKING): drop the deno_ast dependency - #810

Merged
dsherret merged 7 commits into
mainfrom
drop_deno_ast
Aug 22, 2026
Merged

dsherret merged 7 commits into
mainfrom
drop_deno_ast

Conversation

@dsherret

@dsherret dsherret commented Aug 21, 2026 •

Copy link
Copy Markdown
Member

This crate only used deno_ast for parsing and its parse diagnostics. Everything else it needed — the ast view, SourcePos, SourceTextInfo, the comment and token containers — already came from dprint-swc-ext, which deno_ast re-exports.

It now parses with swc directly and owns its diagnostics, which drops 17 crates from the dependency graph (159 → 142): url, idna, the icu_* family, deno_error, deno_media_type, deno_terminal, unicode-width, and friends. deno_ast is gone as a dev dependency too.

A deno_ast user can still hand over an AST

That was the main thing to preserve, and it works because both crates share dprint-swc-ext's view::Program. dprint/dprint-swc-ext#78 added a ProgramInfoProvider trait, format_parsed_source is generic over it, and denoland/deno_ast#335 implements it for ParsedSource — so deno fmt never parses a file twice:

format_parsed_source(FormatParsedSourceOptions {
  source: &parsed_source, // a deno_ast::ParsedSource
  syntax: deno_ast::get_syntax(parsed_source.media_type()),
  config: &config,
  external_formatter: None,
})

Until that lands and is released, format_program takes an ast view directly. tests/foreign_source_test.rs covers the same path by implementing the trait on a local type standing in for another crate, so no dependency is needed to test it.

Breaking changes

  • format_parsed_source takes a FormatParsedSourceOptions struct and is generic over ProgramInfoProvider, so it accepts a parsed source from any crate.
    It no longer checks the parse diagnostics, because it can't for a source it didn't parse. parse_program does that check instead, so anything this crate parsed is still safe by construction; callers who parse elsewhere use the now-public is_unsupported_syntax_error.
  • The formatting entrypoints take the swc Syntax the file was parsed with. That's not a new concept — it's the value the caller already handed to the lexer, and ParsedSource::syntax() returns it for sources this crate parsed. There's deliberately no public MediaType: the formatter only needs to know whether jsx was enabled and whether jsx-like syntax is reserved (.cts/.mts), and both are fields on Syntax. Resolving a syntax from a file extension stays a private detail of the parsing module.
  • format_text's behaviour and all diagnostic messages are unchanged — the existing diagnostic tests pass with their expected strings untouched, file:/// specifiers included (built without url).

Review notes

  • src/parsing/ is the new module: parse.rs (parse + the unsupported-syntax-error check), parse_from_path.rs (the public parse_program, path handling, and the retry-as-jsx fallback — this is the old src/swc.rs), parsed_source.rs, diagnostics.rs, and a private media_type.rs.
  • get_range_text_highlight is lifted from deno_ast along with all six of its tests. Two changes to it: the catch_unwind is gone in favour of clamping the range to the text up front (swc hands back dummy/out-of-range spans, and a panicking Display on an error type is nasty), and the max(1, a - b) is now saturating_sub(..).max(1) since cmp::max doesn't short-circuit.
  • parse_program strips a byte order mark rather than assuming one isn't there — otherwise SourceTextInfo strips it while the AST positions don't, and everything is off by three bytes.
  • The old is_jsx() also matched MediaType::JavaScript. That was unreachable: it's only consulted where type parameters exist, and those can't be parsed under Syntax::Es. So mapping it onto Syntax loses nothing.

Testing

cargo test passes: 648 spec tests, 47 unit tests, 2 foreign-source tests. wasm and tracing builds check, clippy is clean on the new code.

Unrelated but worth knowing: parsing "---" as TypeScript panics inside swc_ecma_lexer on an inverted span. deno_ast panics identically on the same input, so it's upstream and pre-existing rather than anything this PR introduces.

This crate only used `deno_ast` for parsing and its parse diagnostics —
everything else it needed (the ast view, `SourcePos`, `SourceTextInfo`,
comment and token containers) already came from dprint-swc-ext, which
`deno_ast` re-exports.

It now parses with swc directly and owns its diagnostics, which drops 17
crates from the dependency graph (`url`, `idna`, the `icu_*` family,
`deno_error`, `deno_media_type`, `deno_terminal`, `unicode-width`, ...).

A `deno_ast` user can still hand over an already parsed AST with its
comments, so nothing has to be parsed twice. That works through
`ProgramInfoProvider` in dprint-swc-ext, which `deno_ast::ParsedSource`
can implement in about a dozen lines. Until it does, `format_program`
takes an ast view directly.

Breaking changes:

- `format_parsed_source` takes `FormatParsedSourceOptions` and is generic
  over `ProgramInfoProvider`, so it accepts a parsed source from any
  crate. It no longer checks the parse diagnostics, since it can't for a
  source it didn't parse — `parse_program` now does that instead, and
  `is_unsupported_syntax_error` is public for callers that parse
  elsewhere.
- Everything now needs a `MediaType`, which mirrors `deno_ast::MediaType`
  for the media types this crate understands.

Diagnostic messages are unchanged.
The formatter only ever needed two things from the media type: whether
the file was parsed with jsx enabled, and whether jsx-like syntax is
reserved (`.cts` and `.mts`). Both are fields on the swc `Syntax` that
the file was parsed with, so take that instead of reinventing a media
type in the public API.

`MediaType` stays as a private detail of the parsing module, where it
still resolves the syntax and parse mode from a file extension.

Also removes the deno_ast dev dependency. The test it supported now
implements `ProgramInfoProvider` on a local type standing in for another
crate, which covers the same path without the dependency.

Note the previous `is_jsx()` also matched `MediaType::JavaScript`, which
was unreachable — it is only consulted where type parameters exist, and
those can't be parsed under `Syntax::Es`.
The text itself was never deep copied — it's an `Arc<str>` shared by the
parsed source, the text info, and every diagnostic — but there were
several allocations around it that weren't needed:

- Every `ParseDiagnostic` allocated its own copy of the specifier. It's
  the same string for all of them, so share one `Arc<str>`.
- The unsupported syntax error check cloned every matching diagnostic
  only to return them in an error. Partition the diagnostics instead so
  they're moved.
- Resolving a media type allocated a lowercased copy of the extension,
  and of the file stem for declaration files. Compare ascii case
  insensitively instead.
- Building the specifier allocated a `String` per path component plus a
  `Vec` to join them. Write into a single `String`.
- Stripping a byte order mark copied the whole file text twice.
Drops `MediaType`. It was only ever an intermediate step between a file
extension and the swc `Syntax` and parse mode, so resolve those directly
and skip inventing a media type concept in between.

Also swaps `OnceLock` for `OnceCell` in `ParsedSource`. It doesn't need
to be thread safe — it's only ever used from the thread that parsed it —
and it stays `Send`, so it can still be parsed on one thread and
formatted on another.

Resolving the extension no longer allocates: the jsx retry overwrites the
extension rather than building a new path, which also keeps the original
file name in any diagnostics it produces.
@dsherret
dsherret marked this pull request as ready for review August 21, 2026 23:50
The text has to end up in an `Arc<str>` in order to be shared with the
ast view, so taking a `String` meant always copying it. A caller that
already holds an `Arc<str>` now doesn't copy at all.

This doesn't change the Wasm plugin, which starts from an owned
`Vec<u8>`: `String::from_utf8` reuses that buffer, but there's no way to
reach an `Arc<str>` without one copy, because the allocation needs room
for the reference count. Stripping the byte order mark from the bytes
first wouldn't avoid it either — it would just add a memmove before the
same copy.
A review of the deno_ast removal turned up four ways the resolving had
drifted from `deno_media_type::MediaType::from_path`:

- `.d.mts` and `.d.cts` were given `disallow_ambiguous_jsx_like`, which
  `deno_ast` only sets for non-declaration `.mts` and `.cts`. That feeds
  the trailing comma written after a single identifier type parameter, so
  it would have changed formatter output.
- A file whose whole name is an extension (`.ts`) resolved to javascript,
  because `Path::extension` returns nothing for those. Split the file
  name on its last dot instead, which is what `deno_media_type` does.
- An extension overwrite containing a dot (`d.ts`) no longer resolved.
  Overwrite the extension on the file name so it behaves like
  `Path::with_extension` did.
- A declaration file is any file with `.d.` in its name, case sensitively
  — not just one whose stem ends in `.d`.

Also restores the percent encoding that was lost along with the `url`
crate, so a diagnostic for a path containing a space or a `#` is still a
valid url, and a relative path falls back to just the file name like it
used to.

`format_program` now returns an error rather than panicking when a
`ProgramInfoProvider` doesn't capture the text info, tokens, or comments.
@dsherret
dsherret merged commit 215f100 into main Aug 22, 2026
2 checks passed
@dsherret
dsherret deleted the drop_deno_ast branch August 22, 2026 00:22
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