Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: strip bom #6

Merged
merged 1 commit into from
May 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions src/format_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ use jsonc_parser::ParseOptions;
pub fn format_text(
input_text: &str,
format_with_host: impl FnMut(&Path, String) -> Result<Option<String>>,
) -> Result<Option<String>> {
let had_bom = input_text.starts_with("\u{FEFF}");
let input_text = if had_bom { &input_text[3..] } else { input_text };
let result = format_inner(input_text, format_with_host)?;
if result.is_none() && had_bom {
Ok(Some(input_text.to_string()))
} else {
Ok(result)
}
}

fn format_inner(
input_text: &str,
format_with_host: impl FnMut(&Path, String) -> Result<Option<String>>,
) -> Result<Option<String>> {
let parse_result = jsonc_parser::parse_to_ast(
input_text,
Expand Down Expand Up @@ -254,4 +268,48 @@ mod test {
assert_eq!(get_indent_text("\nhello", 1), "");
assert_eq!(get_indent_text("\nhello", 2), "");
}

#[test]
fn formats_with_bom() {
// no changes to code other than bom
{
let input_text = "\u{FEFF}{\"cells\":[{\"cell_type\":\"code\",\"source\":\"let x = 5;\"}]}";
let formatted_text = format_text(input_text, |_, text| Ok(Some(text))).unwrap().unwrap();
assert_eq!(
formatted_text,
"{\"cells\":[{\"cell_type\":\"code\",\"source\":\"let x = 5;\"}]}"
);
}
// other changes as well
let input_text = "\u{FEFF}{
\"cells\":[{
\"cell_type\":\"code\",
\"metadata\": {
\"vscode\": {
\"languageId\": \"typescript\"
}
},
\"source\": \"let x = 5;\"
}]
}
";
let formatted_text = format_text(input_text, |_, text| Ok(Some(format!("{}_formatted", text))))
.unwrap()
.unwrap();
assert_eq!(
formatted_text,
"{
\"cells\":[{
\"cell_type\":\"code\",
\"metadata\": {
\"vscode\": {
\"languageId\": \"typescript\"
}
},
\"source\": \"let x = 5;_formatted\"
}]
}
"
);
}
}
Loading