Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions src/generation/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3829,6 +3829,8 @@ fn gen_reg_exp_literal(node: &Regex, _: &mut Context) -> PrintItems {
items
}

const VALID_STRING_ESCAPES: &[char; 9] = &['\\', 'n', 'r', 'v', 't', 'b', 'f', 'u', 'x'];

fn gen_string_literal<'a>(node: &'a Str, context: &mut Context<'a>) -> PrintItems {
return gen_from_raw_string(&get_string_literal_text(
get_string_value(node, context),
Expand Down Expand Up @@ -3937,24 +3939,27 @@ fn gen_string_literal<'a>(node: &'a Str, context: &mut Context<'a>) -> PrintItem
let string_value = &raw_string_text[1..raw_string_text.len() - 1];
let is_double_quote = raw_string_text.starts_with('"');

return match is_double_quote {
true => remove_needless_quote_backslashes(string_value.replace("\\\"", "\"")),
false => remove_needless_quote_backslashes(string_value.replace("\\'", "'")),
};
return remove_needless_backslashes(match is_double_quote {
true => string_value.replace("\\\"", "\""),
false => string_value.replace("\\'", "'"),
});

fn remove_needless_quote_backslashes(text: String) -> String {
fn remove_needless_backslashes(text: String) -> String {
// People may write string literals that look like the following:
// * "test \' test"
// * 'test \" test'
// * 'test \a test'
// ...if so, remove these backslashes
let mut new_string = String::with_capacity(text.len());
let mut was_last_backslash = false;
for c in text.chars() {
if c == '\\' && !was_last_backslash {
was_last_backslash = true;
} else {
if was_last_backslash && c != '\'' && c != '"' {
new_string.push('\\');
if was_last_backslash {
if (c != '\'' && c != '"' && VALID_STRING_ESCAPES.contains(&c)) || c == '\n' {
new_string.push('\\');
}
}
new_string.push(c);
was_last_backslash = false;
Expand Down
10 changes: 10 additions & 0 deletions tests/specs/literals/StringLiteral/StringLiteral_All.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,13 @@ const t = " ";

[expect]
const t = " ";

== should remove unnecessary escapes ==
const t = "\t";
const u = "\abcd";
const v = ' \'\"\`\a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\v\w\y\z ';

[expect]
const t = "\t";
const u = "abcd";
const v = " '\"`a\bcde\fghijklm\nopq\rs\t\vwyz ";
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const objectLiteral = {
"1foo": true,
"💩": true,
"a💩": true,
"c\d": true,
cd: true,
"1": true,
"1foo"() {},
get "2foo"() {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const objectLiteral = {
"1foo": true,
"💩": true,
"a💩": true,
"c\d": true,
"cd": true,
"1": true,
"1foo"() {},
get "2foo"() {},
Expand Down