Skip to content

Commit 9cd4229

Browse files
authored
fix: use diagnostic display from deno_ast (#464)
1 parent 0dbbe17 commit 9cd4229

File tree

4 files changed

+66
-27
lines changed

4 files changed

+66
-27
lines changed

Cargo.lock

Lines changed: 46 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ tracing = ["dprint-core/tracing"]
2626

2727
[dependencies]
2828
anyhow = "1.0.64"
29-
deno_ast = { version = "0.22.0", features = ["view"] }
29+
deno_ast = { version = "0.23.1", features = ["view"] }
3030
dprint-core = { version = "0.60.0", features = ["formatting"] }
3131
rustc-hash = "1.1.0"
3232
serde = { version = "1.0.144", features = ["derive"] }
@@ -35,4 +35,5 @@ serde_json = { version = "1.0", optional = true }
3535
[dev-dependencies]
3636
debug-here = "0.2"
3737
dprint-development = "0.9.2"
38+
pretty_assertions = "1.3.0"
3839
serde_json = { version = "1.0" }

src/generation/generate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6436,7 +6436,7 @@ fn gen_js_doc_or_multiline_block(comment: &Comment, _context: &mut Context) -> P
64366436
let mut text = String::new();
64376437
// leading asterisk on the first line for jsdoc only
64386438
if is_js_doc && i == 0 {
6439-
text.push_str("*");
6439+
text.push('*');
64406440
} else if i > 0 {
64416441
text.push_str(" *");
64426442
}

src/swc.rs

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use anyhow::anyhow;
22
use anyhow::bail;
33
use anyhow::Result;
44
use deno_ast::swc::parser::error::SyntaxError;
5-
use deno_ast::Diagnostic;
65
use deno_ast::ParsedSource;
76
use deno_ast::SourceTextInfo;
87
use std::path::Path;
@@ -40,9 +39,9 @@ fn parse_inner_no_diagnostic_check(file_path: &Path, text_info: SourceTextInfo)
4039
maybe_syntax: None,
4140
media_type: file_path.into(),
4241
scope_analysis: false,
43-
text_info: text_info.clone(),
42+
text_info,
4443
})
45-
.map_err(|diagnostic| anyhow!("{}", format_diagnostic(&diagnostic, &text_info)))
44+
.map_err(|diagnostic| anyhow!("{:#}", &diagnostic))
4645
}
4746

4847
pub fn ensure_no_specific_syntax_errors(parsed_source: &ParsedSource) -> Result<()> {
@@ -78,11 +77,11 @@ pub fn ensure_no_specific_syntax_errors(parsed_source: &ParsedSource) -> Result<
7877
Ok(())
7978
} else {
8079
let mut final_message = String::new();
81-
for error in diagnostics {
80+
for diagnostic in diagnostics {
8281
if !final_message.is_empty() {
8382
final_message.push_str("\n\n");
8483
}
85-
final_message.push_str(&format_diagnostic(error, parsed_source.text_info()));
84+
final_message.push_str(&format!("{}", diagnostic));
8685
}
8786
bail!("{}", final_message)
8887
}
@@ -92,15 +91,10 @@ fn get_lowercase_extension(file_path: &Path) -> Option<String> {
9291
file_path.extension().and_then(|e| e.to_str()).map(|f| f.to_lowercase())
9392
}
9493

95-
fn format_diagnostic(error: &Diagnostic, text_info: &SourceTextInfo) -> String {
96-
let file_text = text_info.text_str();
97-
let range = error.range.as_byte_range(text_info.range().start);
98-
dprint_core::formatting::utils::string_utils::format_diagnostic(Some((range.start, range.end)), &error.message(), file_text)
99-
}
100-
10194
#[cfg(test)]
10295
mod tests {
10396
use crate::configuration::ConfigurationBuilder;
97+
use pretty_assertions::assert_eq;
10498

10599
use super::*;
106100
use std::path::PathBuf;
@@ -110,7 +104,7 @@ mod tests {
110104
run_fatal_diagnostic_test(
111105
"./test.ts",
112106
"test;\nas#;",
113-
concat!("Line 2, column 3: Expected ';', '}' or <eof>\n", "\n", " as#;\n", " ~"),
107+
concat!("Expected ';', '}' or <eof> at ./test.ts:2:3\n", "\n", " as#;\n", " ~"),
114108
);
115109
}
116110

@@ -121,7 +115,7 @@ mod tests {
121115
"./test.ts",
122116
"+value.",
123117
concat!(
124-
"Line 1, column 8: Unexpected eof\n\n",
118+
"Unexpected eof at ./test.ts:1:8\n\n",
125119
" +value.\n",
126120
// this excess whitespace is a bug, but not a big deal
127121
" "
@@ -135,7 +129,7 @@ mod tests {
135129
run_fatal_diagnostic_test(
136130
"./test.ts",
137131
"+value.;",
138-
concat!("Line 1, column 8: Expected ident\n\n", " +value.;\n", " ~"),
132+
concat!("Expected ident at ./test.ts:1:8\n\n", " +value.;\n", " ~"),
139133
);
140134
}
141135

@@ -145,13 +139,13 @@ mod tests {
145139
"./test.ts",
146140
concat!(
147141
"test;\n",
148-
r#"console.log("x", `duration ${d} not in range - ${min} ≥ ${d} && ${max} ≥ ${d}`),;"#,
142+
r#"console.log('x', `duration ${d} not in range - ${min} ≥ ${d} && ${max} ≥ ${d}`),;"#,
149143
),
150144
concat!(
151-
"Line 2, column 81: Expression expected\n",
145+
"Expression expected at ./test.ts:2:81\n",
152146
"\n",
153-
" && ${max} ≥ ${d}`),;\n",
154-
" ~"
147+
" console.log('x', `duration ${d} not in range - ${min} ≥ ${d} && ${max} ≥ ${d}`),;\n",
148+
" ~",
155149
),
156150
);
157151
}
@@ -166,7 +160,7 @@ mod tests {
166160
run_non_fatal_diagnostic_test(
167161
"./test.ts",
168162
"const Methods {\nf: (x, y) => x + y,\n};",
169-
concat!("Line 1, column 15: Expected a semicolon\n", "\n", " const Methods {\n", " ~"),
163+
concat!("Expected a semicolon at ./test.ts:1:15\n", "\n", " const Methods {\n", " ~"),
170164
);
171165
}
172166

@@ -176,7 +170,7 @@ mod tests {
176170
"./test.ts",
177171
"let a = 0, let b = 1;",
178172
concat!(
179-
"Line 1, column 16: Expected a semicolon\n",
173+
"Expected a semicolon at ./test.ts:1:16\n",
180174
"\n",
181175
" let a = 0, let b = 1;\n",
182176
" ~"
@@ -189,7 +183,7 @@ mod tests {
189183
run_non_fatal_diagnostic_test(
190184
"./test.ts",
191185
"type T =\n | unknown\n { } & unknown;",
192-
concat!("Line 3, column 7: Expression expected\n\n", " { } & unknown;\n", " ~"),
186+
concat!("Expression expected at ./test.ts:3:7\n\n", " { } & unknown;\n", " ~"),
193187
);
194188
}
195189

@@ -201,13 +195,13 @@ mod tests {
201195
run_non_fatal_diagnostic_test(
202196
"./test.ts",
203197
"class Test {",
204-
concat!("Line 1, column 12: Expected '}', got '<eof>'\n\n", " class Test {\n", " ~"),
198+
concat!("Expected '}', got '<eof>' at ./test.ts:1:12\n\n", " class Test {\n", " ~"),
205199
);
206200
}
207201

208202
fn run_non_fatal_diagnostic_test(file_path: &str, text: &str, expected: &str) {
209203
let file_path = PathBuf::from(file_path);
210-
assert_eq!(parse_swc_ast(&file_path, text).err().unwrap().to_string(), expected);
204+
assert_eq!(format!("{}", parse_swc_ast(&file_path, text).err().unwrap()), expected);
211205

212206
// this error should also be surfaced in `format_parsed_source` if someone provides
213207
// a source file that had a non-fatal diagnostic

0 commit comments

Comments
 (0)