Skip to content

Commit

Permalink
fix(formatter): escape double quotes when printing formatter IR
Browse files Browse the repository at this point in the history
  • Loading branch information
dyc3 committed Jun 1, 2024
1 parent bc30892 commit 5c4e3d0
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion crates/biome_formatter/src/format_element/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,35 @@ impl Format<IrFormatContext> 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!(),
}

Expand Down

0 comments on commit 5c4e3d0

Please sign in to comment.