fix(formatter_toml): handle BOM like other languages (#27775)

Use `split_bom` and restore as like other formatters.
This commit is contained in:
leaysgur committed 2026-10-16 04:04:05 +00:00
1 parent 97c9fc4f5e
commit 3a980c922b
3 files changed
+7 -3

No files matched your search

+1
View File
@@ -11,6 +11,7 @@ describe("BOM handling", () => {
["a.yaml", "\uFEFFkey: value", "\uFEFFkey: value\n"],
["a.graphql", "\uFEFF{ a }", "\uFEFF{\n a\n}\n"],
["a.json", '\uFEFF{"a":1}', '\uFEFF{ "a": 1 }\n'],
["a.toml", "\uFEFFa = 1", "\uFEFFa = 1\n"],
];
it.each(cases)("preserves a leading BOM in %s", async (filename, source, expected) => {
-1
View File
@@ -30,7 +30,6 @@ Strong motivations, since these break rules every other language follows:
- Other languages' IR is printed by the parent's printer, so their width counts from the embedding column (e.g. inside a list item)
- Prettier with the plugin behaves the same as now, but consistency across our languages comes first
- Pinned in `apps/oxfmt/test/api/markdown.test.ts`
- No BOM split, which the other crates do
Known limits of the string embedding (text with literal lines, the same as Prettier's `replaceEndOfLine()` on the plugin's string):
+6 -2
View File
@@ -1,6 +1,8 @@
use oxc_allocator::ArenaVec;
use oxc_diagnostics::OxcDiagnostic;
use oxc_formatter_core::{EmbeddedIr, FormatSession, push_text_with_literal_lines};
use oxc_formatter_core::{
EmbeddedIr, FormatSession, push_text_with_literal_lines, spec::split_bom,
};
use oxc_span::Span;
use crate::options::TomlFormatOptions;
@@ -10,8 +12,10 @@ use crate::options::TomlFormatOptions;
/// # Errors
/// Returns an [`OxcDiagnostic`] when the parse produces any error.
pub fn format(source_text: &str, options: TomlFormatOptions) -> Result<String, OxcDiagnostic> {
let (has_bom, source_text) = split_bom(source_text);
let parse = parse_for_format(source_text)?;
Ok(oxc_toml::format(&parse, options.to_oxc_toml()))
let formatted = oxc_toml::format(&parse, options.to_oxc_toml());
Ok(if has_bom { format!("\u{feff}{formatted}") } else { formatted })
}
/// Format `source_text` into IR for embedding into another formatter's document