Skip to content

Commit 3f5758d

Browse files
committed
fix: strip bom
1 parent 3df017f commit 3f5758d

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

src/format_text.rs

+25-6
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,24 @@ use super::configuration::Configuration;
1212
use super::generation::generate;
1313

1414
pub fn format_text(path: &Path, text: &str, config: &Configuration) -> Result<Option<String>> {
15-
let parse_result = parse(text)?;
16-
let is_jsonc = is_jsonc_file(path, config);
17-
let result = dprint_core::formatting::format(
18-
|| generate(parse_result, text, config, is_jsonc),
19-
config_to_print_options(text, config),
20-
);
15+
let result = format_text_inner(path, text, config)?;
2116
if result == text {
2217
Ok(None)
2318
} else {
2419
Ok(Some(result))
2520
}
2621
}
2722

23+
fn format_text_inner(path: &Path, text: &str, config: &Configuration) -> Result<String> {
24+
let text = strip_bom(text);
25+
let parse_result = parse(text)?;
26+
let is_jsonc = is_jsonc_file(path, config);
27+
Ok(dprint_core::formatting::format(
28+
|| generate(parse_result, text, config, is_jsonc),
29+
config_to_print_options(text, config),
30+
))
31+
}
32+
2833
#[cfg(feature = "tracing")]
2934
pub fn trace_file(text: &str, config: &Configuration) -> dprint_core::formatting::TracingResult {
3035
let parse_result = parse(text).unwrap();
@@ -35,6 +40,10 @@ pub fn trace_file(text: &str, config: &Configuration) -> dprint_core::formatting
3540
)
3641
}
3742

43+
fn strip_bom(text: &str) -> &str {
44+
text.strip_prefix("\u{FEFF}").unwrap_or(text)
45+
}
46+
3847
fn parse(text: &str) -> Result<ParseResult<'_>> {
3948
let parse_result = parse_to_ast(
4049
text,
@@ -150,4 +159,14 @@ mod tests {
150159
assert!(is_jsonc_file(&PathBuf::from("test\\.vscode\\settings.json"), &config));
151160
}
152161
}
162+
163+
#[test]
164+
fn should_strip_bom() {
165+
for input_text in ["\u{FEFF}{}", "\u{FEFF}{ }"] {
166+
let global_config = GlobalConfiguration::default();
167+
let config = resolve_config(ConfigKeyMap::new(), &global_config).config;
168+
let output_text = format_text(Path::new("."), input_text, &config).unwrap().unwrap();
169+
assert_eq!(output_text, "{}\n");
170+
}
171+
}
153172
}

0 commit comments

Comments
 (0)