Skip to content

Commit ca33d8a

Browse files
authored
feat: strip BOM on files without any changes otherwise (#638)
1 parent 14c0641 commit ca33d8a

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

src/format_text.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,16 @@ pub fn format_text(file_path: &Path, file_text: &str, config: &Configuration) ->
4343
Ok(None)
4444
} else {
4545
let parsed_source = parse_swc_ast(file_path, file_text)?;
46-
inner_format(&parsed_source, config)
46+
match inner_format(&parsed_source, config)? {
47+
Some(new_text) => Ok(Some(new_text)),
48+
None => {
49+
if let Some(stripped) = file_text.strip_prefix("\u{FEFF}") {
50+
Ok(Some(stripped.to_string()))
51+
} else {
52+
Ok(None)
53+
}
54+
}
55+
}
4756
}
4857
}
4958

@@ -89,3 +98,17 @@ fn config_to_print_options(file_text: &str, config: &Configuration) -> PrintOpti
8998
new_line_text: resolve_new_line_kind(file_text, config.new_line_kind),
9099
}
91100
}
101+
102+
#[cfg(test)]
103+
mod test {
104+
use super::*;
105+
106+
#[test]
107+
fn strips_bom() {
108+
for input_text in ["\u{FEFF}const t = 5;\n", "\u{FEFF}const t = 5;"] {
109+
let config = crate::configuration::ConfigurationBuilder::new().build();
110+
let result = format_text(&std::path::PathBuf::from("test.ts"), input_text, &config).unwrap().unwrap();
111+
assert_eq!(result, "const t = 5;\n");
112+
}
113+
}
114+
}

0 commit comments

Comments
 (0)