Skip to content

Commit 67886e7

Browse files
authored
fix: strip bom (#14)
1 parent 292af9d commit 67886e7

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

src/format_text.rs

+34-3
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,24 @@ use crate::configuration::Configuration;
88
use crate::generation::generate;
99

1010
pub fn format_text(_file_path: &Path, text: &str, config: &Configuration) -> Result<Option<String>> {
11-
let node = parse_node(text)?;
12-
13-
let result = dprint_core::formatting::format(|| generate(&node, text, config), config_to_print_options(text, config));
11+
let result = format_inner(text, config)?;
1412
if result == text {
1513
Ok(None)
1614
} else {
1715
Ok(Some(result))
1816
}
1917
}
2018

19+
fn format_inner(text: &str, config: &Configuration) -> Result<String> {
20+
let text = strip_bom(text);
21+
let node = parse_node(text)?;
22+
23+
Ok(dprint_core::formatting::format(
24+
|| generate(&node, text, config),
25+
config_to_print_options(text, config),
26+
))
27+
}
28+
2129
#[cfg(feature = "tracing")]
2230
pub fn trace_file(_file_path: &Path, text: &str, config: &Configuration) -> dprint_core::formatting::TracingResult {
2331
let node = parse_node(text).unwrap();
@@ -29,6 +37,10 @@ fn parse_node(text: &str) -> Result<Dockerfile> {
2937
Ok(Dockerfile::parse(text)?)
3038
}
3139

40+
fn strip_bom(text: &str) -> &str {
41+
text.strip_prefix("\u{FEFF}").unwrap_or(text)
42+
}
43+
3244
fn config_to_print_options(text: &str, config: &Configuration) -> PrintOptions {
3345
PrintOptions {
3446
indent_width: 1,
@@ -37,3 +49,22 @@ fn config_to_print_options(text: &str, config: &Configuration) -> PrintOptions {
3749
new_line_text: resolve_new_line_kind(text, config.new_line_kind),
3850
}
3951
}
52+
53+
#[cfg(test)]
54+
mod test {
55+
use super::*;
56+
57+
#[test]
58+
fn strips_bom() {
59+
for input_text in ["\u{FEFF}FROM example:12.16.1\n", "\u{FEFF}FROM example:12.16.1\n"] {
60+
let text = format_text(
61+
&std::path::PathBuf::from("test.dockerfile"),
62+
input_text,
63+
&crate::configuration::ConfigurationBuilder::new().build(),
64+
)
65+
.unwrap()
66+
.unwrap();
67+
assert_eq!(text, "FROM example:12.16.1\n");
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)