From e10f09e81492dbe91dc868726b3349faf280bad0 Mon Sep 17 00:00:00 2001 From: Carson McManus Date: Sat, 1 Jun 2024 09:29:24 -0400 Subject: [PATCH] fix(formatter): escape double quotes when printing formatter IR --- .../src/format_element/document.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/crates/biome_formatter/src/format_element/document.rs b/crates/biome_formatter/src/format_element/document.rs index 3363858f269f..53372cc8e9ae 100644 --- a/crates/biome_formatter/src/format_element/document.rs +++ b/crates/biome_formatter/src/format_element/document.rs @@ -250,7 +250,23 @@ impl Format for &[FormatElement] { FormatElement::Space | FormatElement::HardSpace => { write!(f, [text(" ")])?; } - element if element.is_text() => f.write_element(element.clone())?, + element if element.is_text() => { + // escape quotes + let new_element = match element { + // except for static text because source_position is unknown + FormatElement::StaticText { .. } => element.clone(), + FormatElement::DynamicText { text, source_position } => { + let text = text.to_string().replace("\"", "\\\""); + FormatElement::DynamicText { text: text.into(), source_position: *source_position } + }, + FormatElement::LocatedTokenText { slice, source_position } => { + let text = slice.to_string().replace("\"", "\\\""); + FormatElement::DynamicText { text: text.into(), source_position: *source_position } + }, + _ => unreachable!(), + }; + f.write_element(new_element)?; + }, _ => unreachable!(), }