Repository navigation
feat(BREAKING): drop the deno_ast dependency - #810
Merged
Merged
Conversation
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
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This crate only used
deno_astfor parsing and its parse diagnostics. Everything else it needed — the ast view,SourcePos,SourceTextInfo, the comment and token containers — already came fromdprint-swc-ext, whichdeno_astre-exports.It now parses with swc directly and owns its diagnostics, which drops 17 crates from the dependency graph (159 → 142):
url,idna, theicu_*family,deno_error,deno_media_type,deno_terminal,unicode-width, and friends.deno_astis gone as a dev dependency too.A
deno_astuser can still hand over an ASTThat was the main thing to preserve, and it works because both crates share
dprint-swc-ext'sview::Program. dprint/dprint-swc-ext#78 added aProgramInfoProvidertrait,format_parsed_sourceis generic over it, and denoland/deno_ast#335 implements it forParsedSource— sodeno fmtnever parses a file twice:Until that lands and is released,
format_programtakes an ast view directly.tests/foreign_source_test.rscovers 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_sourcetakes aFormatParsedSourceOptionsstruct and is generic overProgramInfoProvider, 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_programdoes that check instead, so anything this crate parsed is still safe by construction; callers who parse elsewhere use the now-publicis_unsupported_syntax_error.Syntaxthe file was parsed with. That's not a new concept — it's the value the caller already handed to the lexer, andParsedSource::syntax()returns it for sources this crate parsed. There's deliberately no publicMediaType: the formatter only needs to know whether jsx was enabled and whether jsx-like syntax is reserved (.cts/.mts), and both are fields onSyntax. 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 withouturl).Review notes
src/parsing/is the new module:parse.rs(parse + the unsupported-syntax-error check),parse_from_path.rs(the publicparse_program, path handling, and the retry-as-jsx fallback — this is the oldsrc/swc.rs),parsed_source.rs,diagnostics.rs, and a privatemedia_type.rs.get_range_text_highlightis lifted fromdeno_astalong with all six of its tests. Two changes to it: thecatch_unwindis gone in favour of clamping the range to the text up front (swc hands back dummy/out-of-range spans, and a panickingDisplayon an error type is nasty), and themax(1, a - b)is nowsaturating_sub(..).max(1)sincecmp::maxdoesn't short-circuit.parse_programstrips a byte order mark rather than assuming one isn't there — otherwiseSourceTextInfostrips it while the AST positions don't, and everything is off by three bytes.is_jsx()also matchedMediaType::JavaScript. That was unreachable: it's only consulted where type parameters exist, and those can't be parsed underSyntax::Es. So mapping it ontoSyntaxloses nothing.Testing
cargo testpasses: 648 spec tests, 47 unit tests, 2 foreign-source tests. wasm andtracingbuilds check, clippy is clean on the new code.Unrelated but worth knowing: parsing
"---"as TypeScript panics insideswc_ecma_lexeron an inverted span.deno_astpanics identically on the same input, so it's upstream and pre-existing rather than anything this PR introduces.