From 828434fcf07ea2f35df73672b8966b94d43c0086 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Sun, 26 May 2024 23:21:05 -0400 Subject: [PATCH] fix: strip bom (#37) --- src/format_text.rs | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/src/format_text.rs b/src/format_text.rs index 0f7b319..4f5a8b4 100644 --- a/src/format_text.rs +++ b/src/format_text.rs @@ -12,12 +12,7 @@ use super::configuration::Configuration; use super::generation::generate; pub fn format_text(path: &Path, text: &str, config: &Configuration) -> Result> { - let parse_result = parse(text)?; - let is_jsonc = is_jsonc_file(path, config); - let result = dprint_core::formatting::format( - || generate(parse_result, text, config, is_jsonc), - config_to_print_options(text, config), - ); + let result = format_text_inner(path, text, config)?; if result == text { Ok(None) } else { @@ -25,6 +20,16 @@ pub fn format_text(path: &Path, text: &str, config: &Configuration) -> Result Result { + let text = strip_bom(text); + let parse_result = parse(text)?; + let is_jsonc = is_jsonc_file(path, config); + Ok(dprint_core::formatting::format( + || generate(parse_result, text, config, is_jsonc), + config_to_print_options(text, config), + )) +} + #[cfg(feature = "tracing")] pub fn trace_file(text: &str, config: &Configuration) -> dprint_core::formatting::TracingResult { let parse_result = parse(text).unwrap(); @@ -35,6 +40,10 @@ pub fn trace_file(text: &str, config: &Configuration) -> dprint_core::formatting ) } +fn strip_bom(text: &str) -> &str { + text.strip_prefix("\u{FEFF}").unwrap_or(text) +} + fn parse(text: &str) -> Result> { let parse_result = parse_to_ast( text, @@ -150,4 +159,14 @@ mod tests { assert!(is_jsonc_file(&PathBuf::from("test\\.vscode\\settings.json"), &config)); } } + + #[test] + fn should_strip_bom() { + for input_text in ["\u{FEFF}{}", "\u{FEFF}{ }"] { + let global_config = GlobalConfiguration::default(); + let config = resolve_config(ConfigKeyMap::new(), &global_config).config; + let output_text = format_text(Path::new("."), input_text, &config).unwrap().unwrap(); + assert_eq!(output_text, "{}\n"); + } + } }