From 1a88fe7f45514da476ce8c9471d0581db02726a1 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Sun, 26 May 2024 23:26:14 -0400 Subject: [PATCH] fix: strip utf-8 bom --- src/format_text.rs | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/format_text.rs b/src/format_text.rs index dbd688b..fdce865 100644 --- a/src/format_text.rs +++ b/src/format_text.rs @@ -10,9 +10,7 @@ use std::path::Path; use taplo::syntax::SyntaxNode; pub fn format_text(file_path: &Path, text: &str, config: &Configuration) -> Result> { - let node = parse_and_process_node(file_path, text, config)?; - - let result = dprint_core::formatting::format(|| generate(node, text, config), config_to_print_options(text, config)); + let result = format_text_inner(file_path, text, config)?; if result == text { Ok(None) } else { @@ -20,6 +18,16 @@ pub fn format_text(file_path: &Path, text: &str, config: &Configuration) -> Resu } } +fn format_text_inner(file_path: &Path, text: &str, config: &Configuration) -> Result { + let text = strip_bom(text); + let node = parse_and_process_node(file_path, text, config)?; + + Ok(dprint_core::formatting::format( + || generate(node, text, config), + config_to_print_options(text, config), + )) +} + #[cfg(feature = "tracing")] pub fn trace_file(file_path: &Path, text: &str, config: &Configuration) -> dprint_core::formatting::TracingResult { let node = parse_and_process_node(file_path, text, config).unwrap(); @@ -27,6 +35,10 @@ pub fn trace_file(file_path: &Path, text: &str, config: &Configuration) -> dprin dprint_core::formatting::trace_printing(|| generate(node, text, config), config_to_print_options(text, config)) } +fn strip_bom(text: &str) -> &str { + text.strip_prefix("\u{FEFF}").unwrap_or(text) +} + fn parse_and_process_node(file_path: &Path, text: &str, config: &Configuration) -> Result { let node = parse_taplo(text)?; @@ -58,3 +70,14 @@ fn config_to_print_options(text: &str, config: &Configuration) -> PrintOptions { new_line_text: resolve_new_line_kind(text, config.new_line_kind), } } + +#[cfg(test)] +mod test { + #[test] + fn strips_bom() { + let config = crate::configuration::ConfigurationBuilder::new().build(); + let file_text = crate::format_text::format_text(&std::path::PathBuf::from("file.toml"), "\u{FEFF}# 1\n# 2\n", &config).unwrap(); + + assert_eq!(file_text.unwrap(), "# 1\n# 2\n"); + } +}