1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-21 23:04:45 -05:00

feat(fmt): better error on malfored HTML files (#25853)

Improves syntax errors for HTML formatter.

`broken.html`

```html
<div class=container > content 
```

```
$ deno fmt broken.html
Error formatting: /Users/ib/dev/deno/tests/specs/fmt/html/broken.html
   syntax error 'expect close tag' at line 3, column 0
Checked 1 file
```

```
$ ./target/debug/deno fmt broken.html
Error formatting: /Users/ib/dev/deno/tests/specs/fmt/html/broken.html
   Syntax error (expect close tag) at file:///Users/ib/dev/deno/tests/specs/fmt/html/broken.html:3:0

Checked 1 file
```
This commit is contained in:
Bartek Iwańczuk 2024-09-25 02:00:16 +01:00 committed by GitHub
parent c4f7b2ac00
commit 6c26c1ed96
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 44 additions and 1 deletions

View file

@ -33,6 +33,7 @@ use deno_core::error::AnyError;
use deno_core::futures;
use deno_core::parking_lot::Mutex;
use deno_core::unsync::spawn_blocking;
use deno_core::url::Url;
use log::debug;
use log::info;
use log::warn;
@ -435,7 +436,39 @@ pub fn format_html(
},
)
.map_err(|error| match error {
markup_fmt::FormatError::Syntax(error) => AnyError::from(error),
markup_fmt::FormatError::Syntax(error) => {
// TODO(bartlomieju): rework when better error support in `markup_fmt` lands
fn inner(
error: &markup_fmt::SyntaxError,
file_path: &Path,
) -> Option<String> {
let error_str = format!("{}", error);
let error_str = error_str.strip_prefix("syntax error '")?;
let reason = error_str
.split("' at")
.collect::<Vec<_>>()
.first()
.map(|s| s.to_string())?;
let url = Url::from_file_path(file_path).ok()?;
let error_msg = format!(
"Syntax error ({}) at {}:{}:{}\n",
reason,
url.as_str(),
error.line,
error.column
);
Some(error_msg)
}
if let Some(error_msg) = inner(&error, file_path) {
AnyError::from(generic_error(error_msg))
} else {
AnyError::from(error)
}
}
markup_fmt::FormatError::External(errors) => {
let last = errors.len() - 1;
AnyError::msg(

View file

@ -8,6 +8,10 @@
"well_formatted": {
"args": "fmt --check well_formatted.html",
"output": "Checked 1 file\n"
},
"broken": {
"args": "fmt broken.html",
"output": "broken.out"
}
}
}

View file

@ -0,0 +1,2 @@
<div class=container > content

View file

@ -0,0 +1,4 @@
Error formatting: [WILDCARD]broken.html
Syntax error (expect close tag) at file://[WILDCARD]broken.html:3:0
Checked 1 file