mirror of
https://github.com/denoland/deno.git
synced 2024-11-21 15:04:11 -05:00
fix(fmt): error instead of panic on unstable format (#26859)
This commit is contained in:
parent
f091d1ad69
commit
6b5cb41545
1 changed files with 28 additions and 20 deletions
|
@ -790,28 +790,26 @@ fn format_ensure_stable(
|
||||||
return Ok(Some(current_text));
|
return Ok(Some(current_text));
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
panic!(
|
bail!(
|
||||||
concat!(
|
concat!(
|
||||||
"Formatting succeeded initially, but failed when ensuring a ",
|
"Formatting succeeded initially, but failed when ensuring a ",
|
||||||
"stable format. This indicates a bug in the formatter where ",
|
"stable format. This indicates a bug in the formatter where ",
|
||||||
"the text it produces is not syntactically correct. As a temporary ",
|
"the text it produces is not syntactically correct. As a temporary ",
|
||||||
"workaround you can ignore this file ({}).\n\n{:#}"
|
"workaround you can ignore this file.\n\n{:#}"
|
||||||
),
|
),
|
||||||
file_path.display(),
|
|
||||||
err,
|
err,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
count += 1;
|
count += 1;
|
||||||
if count == 5 {
|
if count == 5 {
|
||||||
panic!(
|
bail!(
|
||||||
concat!(
|
concat!(
|
||||||
"Formatting not stable. Bailed after {} tries. This indicates a bug ",
|
"Formatting not stable. Bailed after {} tries. This indicates a bug ",
|
||||||
"in the formatter where it formats the file ({}) differently each time. As a ",
|
"in the formatter where it formats the file differently each time. As a ",
|
||||||
"temporary workaround you can ignore this file."
|
"temporary workaround you can ignore this file."
|
||||||
),
|
),
|
||||||
count,
|
count,
|
||||||
file_path.display(),
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1215,6 +1213,8 @@ fn is_supported_ext_fmt(path: &Path) -> bool {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
|
use test_util::assert_starts_with;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -1270,12 +1270,16 @@ mod test {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic(expected = "Formatting not stable. Bailed after 5 tries.")]
|
|
||||||
fn test_format_ensure_stable_unstable_format() {
|
fn test_format_ensure_stable_unstable_format() {
|
||||||
format_ensure_stable(&PathBuf::from("mod.ts"), "1", |_, file_text| {
|
let err =
|
||||||
Ok(Some(format!("1{file_text}")))
|
format_ensure_stable(&PathBuf::from("mod.ts"), "1", |_, file_text| {
|
||||||
})
|
Ok(Some(format!("1{file_text}")))
|
||||||
.unwrap();
|
})
|
||||||
|
.unwrap_err();
|
||||||
|
assert_starts_with!(
|
||||||
|
err.to_string(),
|
||||||
|
"Formatting not stable. Bailed after 5 tries."
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -1289,16 +1293,20 @@ mod test {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic(expected = "Formatting succeeded initially, but failed when")]
|
|
||||||
fn test_format_ensure_stable_error_second() {
|
fn test_format_ensure_stable_error_second() {
|
||||||
format_ensure_stable(&PathBuf::from("mod.ts"), "1", |_, file_text| {
|
let err =
|
||||||
if file_text == "1" {
|
format_ensure_stable(&PathBuf::from("mod.ts"), "1", |_, file_text| {
|
||||||
Ok(Some("11".to_string()))
|
if file_text == "1" {
|
||||||
} else {
|
Ok(Some("11".to_string()))
|
||||||
bail!("Error formatting.")
|
} else {
|
||||||
}
|
bail!("Error formatting.")
|
||||||
})
|
}
|
||||||
.unwrap();
|
})
|
||||||
|
.unwrap_err();
|
||||||
|
assert_starts_with!(
|
||||||
|
err.to_string(),
|
||||||
|
"Formatting succeeded initially, but failed when"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
Loading…
Reference in a new issue