summaryrefslogtreecommitdiff
path: root/cli/tools/fmt.rs
diff options
context:
space:
mode:
authorhaturau <135221985+haturatu@users.noreply.github.com>2024-11-20 01:20:47 +0900
committerGitHub <noreply@github.com>2024-11-20 01:20:47 +0900
commit85719a67e59c7aa45bead26e4942d7df8b1b42d4 (patch)
treeface0aecaac53e93ce2f23b53c48859bcf1a36ec /cli/tools/fmt.rs
parent67697bc2e4a62a9670699fd18ad0dd8efc5bd955 (diff)
parent186b52731c6bb326c4d32905c5e732d082e83465 (diff)
Merge branch 'denoland:main' into main
Diffstat (limited to 'cli/tools/fmt.rs')
-rw-r--r--cli/tools/fmt.rs69
1 files changed, 48 insertions, 21 deletions
diff --git a/cli/tools/fmt.rs b/cli/tools/fmt.rs
index 8a4bc4e6c..d40abd5f5 100644
--- a/cli/tools/fmt.rs
+++ b/cli/tools/fmt.rs
@@ -83,6 +83,7 @@ pub async fn format(
file_watcher::PrintConfig::new("Fmt", !watch_flags.no_clear_screen),
move |flags, watcher_communicator, changed_paths| {
let fmt_flags = fmt_flags.clone();
+ watcher_communicator.show_path_changed(changed_paths.clone());
Ok(async move {
let factory = CliFactory::from_flags(flags);
let cli_options = factory.cli_options()?;
@@ -227,6 +228,7 @@ fn collect_fmt_files(
})
.ignore_git_folder()
.ignore_node_modules()
+ .use_gitignore()
.set_vendor_folder(cli_options.vendor_dir_path().map(ToOwned::to_owned))
.collect_file_patterns(&deno_config::fs::RealDenoConfigFs, files)
}
@@ -353,6 +355,21 @@ fn format_yaml(
file_text: &str,
fmt_options: &FmtOptionsConfig,
) -> Result<Option<String>, AnyError> {
+ let ignore_file = file_text
+ .lines()
+ .take_while(|line| line.starts_with('#'))
+ .any(|line| {
+ line
+ .strip_prefix('#')
+ .unwrap()
+ .trim()
+ .starts_with("deno-fmt-ignore-file")
+ });
+
+ if ignore_file {
+ return Ok(None);
+ }
+
let formatted_str =
pretty_yaml::format_text(file_text, &get_resolved_yaml_config(fmt_options))
.map_err(AnyError::from)?;
@@ -775,28 +792,26 @@ fn format_ensure_stable(
return Ok(Some(current_text));
}
Err(err) => {
- panic!(
+ bail!(
concat!(
"Formatting succeeded initially, but failed when ensuring a ",
"stable format. This indicates a bug in the formatter where ",
"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,
)
}
}
count += 1;
if count == 5 {
- panic!(
+ bail!(
concat!(
"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."
),
count,
- file_path.display(),
)
}
}
@@ -978,6 +993,7 @@ fn get_resolved_malva_config(
single_line_top_level_declarations: false,
selector_override_comment_directive: "deno-fmt-selector-override".into(),
ignore_comment_directive: "deno-fmt-ignore".into(),
+ ignore_file_comment_directive: "deno-fmt-ignore-file".into(),
};
FormatOptions {
@@ -1016,7 +1032,7 @@ fn get_resolved_markup_fmt_config(
max_attrs_per_line: None,
prefer_attrs_single_line: false,
html_normal_self_closing: None,
- html_void_self_closing: Some(true),
+ html_void_self_closing: None,
component_self_closing: None,
svg_self_closing: None,
mathml_self_closing: None,
@@ -1036,6 +1052,7 @@ fn get_resolved_markup_fmt_config(
svelte_directive_shorthand: Some(true),
astro_attr_shorthand: Some(true),
ignore_comment_directive: "deno-fmt-ignore".into(),
+ ignore_file_comment_directive: "deno-fmt-ignore-file".into(),
};
FormatOptions {
@@ -1198,6 +1215,8 @@ fn is_supported_ext_fmt(path: &Path) -> bool {
#[cfg(test)]
mod test {
+ use test_util::assert_starts_with;
+
use super::*;
#[test]
@@ -1253,12 +1272,16 @@ mod test {
}
#[test]
- #[should_panic(expected = "Formatting not stable. Bailed after 5 tries.")]
fn test_format_ensure_stable_unstable_format() {
- format_ensure_stable(&PathBuf::from("mod.ts"), "1", |_, file_text| {
- Ok(Some(format!("1{file_text}")))
- })
- .unwrap();
+ let err =
+ format_ensure_stable(&PathBuf::from("mod.ts"), "1", |_, file_text| {
+ Ok(Some(format!("1{file_text}")))
+ })
+ .unwrap_err();
+ assert_starts_with!(
+ err.to_string(),
+ "Formatting not stable. Bailed after 5 tries."
+ );
}
#[test]
@@ -1272,16 +1295,20 @@ mod test {
}
#[test]
- #[should_panic(expected = "Formatting succeeded initially, but failed when")]
fn test_format_ensure_stable_error_second() {
- format_ensure_stable(&PathBuf::from("mod.ts"), "1", |_, file_text| {
- if file_text == "1" {
- Ok(Some("11".to_string()))
- } else {
- bail!("Error formatting.")
- }
- })
- .unwrap();
+ let err =
+ format_ensure_stable(&PathBuf::from("mod.ts"), "1", |_, file_text| {
+ if file_text == "1" {
+ Ok(Some("11".to_string()))
+ } else {
+ bail!("Error formatting.")
+ }
+ })
+ .unwrap_err();
+ assert_starts_with!(
+ err.to_string(),
+ "Formatting succeeded initially, but failed when"
+ );
}
#[test]