Skip to content

Commit bb50537

Browse files
committed
fix: handle backslashes in embedded template language reasonably
1 parent 38984e0 commit bb50537

3 files changed

Lines changed: 15 additions & 3 deletions

File tree

src/format_text.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ mod test {
165165
}),
166166
});
167167
assert!(result.is_err());
168-
assert_eq!(result.unwrap_err().to_string(), "Error formatting tagged template literal at line 0: Syntax error from external formatter");
168+
assert_eq!(
169+
result.unwrap_err().to_string(),
170+
"Error formatting tagged template literal at line 0: Syntax error from external formatter"
171+
);
169172
}
170173
}

src/generation/generate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3038,8 +3038,8 @@ fn maybe_gen_tagged_tpl_with_external_formatter<'a>(node: &TaggedTpl<'a>, contex
30383038
.unwrap();
30393039

30403040
// Then formats the text with the external formatter.
3041-
let formatted_tpl = match external_formatter(media_type, text, context.config) {
3042-
Ok(formatted_tpl) => formatted_tpl?,
3041+
let formatted_tpl = match external_formatter(media_type, text.replace(r"\\", "\\"), context.config) {
3042+
Ok(formatted_tpl) => formatted_tpl?.replace("\\", r"\\"),
30433043
Err(err) => {
30443044
context.diagnostics.push(context::GenerateDiagnostic {
30453045
message: format!("Error formatting tagged template literal at line {}: {}", node.start_line(), err),

tests/specs/external_formatter/css.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,12 @@ styled.div`color:red;`;
122122
[expect]
123123
// dprint-ignore
124124
styled.div`color:red;`;
125+
126+
== should format css that includes \\ ==
127+
css`.bg-\\[\\#86efac\\] { background-color: #86efac; }`
128+
[expect]
129+
css`
130+
.bg-\\[\\#86efac\\] {
131+
background-color: #86efac;
132+
}
133+
`;

0 commit comments

Comments
 (0)