Skip to content

Commit

Permalink
fix: strip utf-8 bom
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed May 27, 2024
1 parent 6501441 commit 1a88fe7
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions src/format_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,35 @@ use std::path::Path;
use taplo::syntax::SyntaxNode;

pub fn format_text(file_path: &Path, text: &str, config: &Configuration) -> Result<Option<String>> {
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 {
Ok(Some(result))
}
}

fn format_text_inner(file_path: &Path, text: &str, config: &Configuration) -> Result<String> {
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();

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<SyntaxNode> {
let node = parse_taplo(text)?;

Expand Down Expand Up @@ -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");
}
}

0 comments on commit 1a88fe7

Please sign in to comment.