Skip to content

Commit b7faeaa

Browse files
committed
fix: Handle needless quote backslashes.
1 parent aef403e commit b7faeaa

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

src/parsing/parser.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2855,9 +2855,30 @@ fn parse_string_literal<'a>(node: &'a Str, context: &mut Context<'a>) -> PrintIt
28552855
let string_value = raw_string_text.chars().skip(1).take(raw_string_text.chars().count() - 2).collect::<String>();
28562856
let is_double_quote = raw_string_text.chars().next().unwrap() == '"';
28572857

2858-
match is_double_quote {
2859-
true => string_value.replace("\\\"", "\""),
2860-
false => string_value.replace("\\'", "'"),
2858+
return match is_double_quote {
2859+
true => remove_needless_quote_backslashes(string_value.replace("\\\"", "\"")),
2860+
false => remove_needless_quote_backslashes(string_value.replace("\\'", "'")),
2861+
};
2862+
2863+
fn remove_needless_quote_backslashes(text: String) -> String {
2864+
// People may write string literals that look like the following:
2865+
// * "test \' test"
2866+
// * 'test \" test'
2867+
// ...if so, remove these backslashes
2868+
let mut new_string = String::with_capacity(text.len());
2869+
let mut was_last_backslash = false;
2870+
for c in text.chars() {
2871+
if c == '\\' && !was_last_backslash {
2872+
was_last_backslash = true;
2873+
} else {
2874+
if was_last_backslash && c != '\'' && c != '"' {
2875+
new_string.push('\\');
2876+
}
2877+
new_string.push(c);
2878+
was_last_backslash = false;
2879+
}
2880+
}
2881+
new_string
28612882
}
28622883
}
28632884
}

tests/specs/issues/issue0094.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,9 @@ x = <div>
4242
<first>f</first> <first>f</first> <first>f</first> <first>f</first>{" "}
4343
<first>f</first> <first>f</first>
4444
</div>;
45+
46+
== should use the proper number of backslashes when converting this scenario ==
47+
<div id={'\'\"\\\''} />;
48+
49+
[expect]
50+
<div id={"'\"\\'"} />;

0 commit comments

Comments
 (0)