Skip to content

Commit 4af9c79

Browse files
committed
fix: resolve the syntax the same way deno_media_type does
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.
1 parent d632f3b commit 4af9c79

5 files changed

Lines changed: 229 additions & 109 deletions

File tree

src/format_text.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::sync::Arc;
33

44
use dprint_core::configuration::resolve_new_line_kind;
55
use dprint_core::formatting::*;
6+
use dprint_swc_ext::common::RootNode;
67
use dprint_swc_ext::common::SourceTextInfoProvider;
78
use dprint_swc_ext::swc::parser::Syntax;
89
use dprint_swc_ext::view::Program;
@@ -110,9 +111,12 @@ pub struct FormatParsedSourceOptions<'a, TSource: ProgramInfoProvider> {
110111
/// Any parsed source implementing `ProgramInfoProvider` works here, including
111112
/// `deno_ast::ParsedSource`, so that the text does not need to be parsed twice.
112113
///
113-
/// Note that for a source this crate did not parse, it's up to the caller to first
114-
/// check the parse diagnostics with
114+
/// Note that for a source this crate did not parse, it's up to the caller to
115+
/// first check its parse diagnostics with
115116
/// [`is_unsupported_syntax_error`](crate::is_unsupported_syntax_error).
117+
/// Formatting a program that swc recovered text-losing errors from will mangle
118+
/// the file. A [`ParsedSource`](crate::parsing::ParsedSource) from this crate is
119+
/// already checked at parse time.
116120
pub fn format_parsed_source<TSource: ProgramInfoProvider>(options: FormatParsedSourceOptions<TSource>) -> Result<Option<String>> {
117121
let FormatParsedSourceOptions {
118122
source,
@@ -144,9 +148,14 @@ pub struct FormatProgramOptions<'a> {
144148
/// Use this when the source was parsed elsewhere (ex. with `deno_ast`). The
145149
/// program must have been parsed with tokens and comments captured.
146150
///
147-
/// Note that unlike the other entrypoints, this does not check for syntax
148-
/// errors that swc recovered from. Use [`is_unsupported_syntax_error`](crate::is_unsupported_syntax_error)
149-
/// on the parse diagnostics beforehand in order to do that.
151+
/// The program must have been parsed with the text info, tokens, and comments
152+
/// all captured, otherwise this errors.
153+
///
154+
/// Note that unlike [`format_text`] this does not check for syntax errors that
155+
/// swc recovered from, because it has no diagnostics to check. Use
156+
/// [`is_unsupported_syntax_error`](crate::is_unsupported_syntax_error) on the
157+
/// parse diagnostics beforehand in order to do that — formatting a program that
158+
/// swc recovered text-losing errors from will mangle the file.
150159
///
151160
/// # Example
152161
///
@@ -167,6 +176,15 @@ pub fn format_program(options: FormatProgramOptions) -> Result<Option<String>> {
167176
config,
168177
external_formatter,
169178
} = options;
179+
if program.maybe_text_info().is_none() {
180+
return Err("The text info must be provided in order to format a program.".into());
181+
}
182+
if program.maybe_token_container().is_none() {
183+
return Err("The tokens must be captured in order to format a program.".into());
184+
}
185+
if program.maybe_comment_container().is_none() {
186+
return Err("The comments must be captured in order to format a program.".into());
187+
}
170188
let file_text = program.text_info().text_str();
171189
if super::utils::file_text_has_ignore_comment(file_text, &config.ignore_file_comment_text) {
172190
return Ok(None);

src/parsing/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,3 @@ pub use diagnostics::*;
77
pub use parse::*;
88
pub use parse_from_path::*;
99
pub use parsed_source::*;
10-
11-
use parse::ParseMode;

0 commit comments

Comments
 (0)