From 20b018ccc24e99da47cf5d5ec6164e22892ea10d Mon Sep 17 00:00:00 2001 From: Shannon Rothe Date: Mon, 27 Feb 2023 11:30:47 +1100 Subject: [PATCH] remove unnecessary escape sequences --- src/generation/generate.rs | 19 ++++++++++++------- .../StringLiteral/StringLiteral_All.txt | 10 ++++++++++ .../StringLiteral_QuoteProps_AsNeeded.txt | 2 +- .../StringLiteral_QuoteProps_Preserve.txt | 2 +- 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/generation/generate.rs b/src/generation/generate.rs index 8479a6b7..ebae3bce 100644 --- a/src/generation/generate.rs +++ b/src/generation/generate.rs @@ -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), @@ -3937,15 +3939,16 @@ 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; @@ -3953,8 +3956,10 @@ fn gen_string_literal<'a>(node: &'a Str, context: &mut Context<'a>) -> PrintItem 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; diff --git a/tests/specs/literals/StringLiteral/StringLiteral_All.txt b/tests/specs/literals/StringLiteral/StringLiteral_All.txt index efdc2a68..bf15e71c 100644 --- a/tests/specs/literals/StringLiteral/StringLiteral_All.txt +++ b/tests/specs/literals/StringLiteral/StringLiteral_All.txt @@ -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 "; diff --git a/tests/specs/literals/StringLiteral/StringLiteral_QuoteProps_AsNeeded.txt b/tests/specs/literals/StringLiteral/StringLiteral_QuoteProps_AsNeeded.txt index a465a9a7..74fd1c20 100644 --- a/tests/specs/literals/StringLiteral/StringLiteral_QuoteProps_AsNeeded.txt +++ b/tests/specs/literals/StringLiteral/StringLiteral_QuoteProps_AsNeeded.txt @@ -41,7 +41,7 @@ const objectLiteral = { "1foo": true, "💩": true, "a💩": true, - "c\d": true, + cd: true, "1": true, "1foo"() {}, get "2foo"() {}, diff --git a/tests/specs/literals/StringLiteral/StringLiteral_QuoteProps_Preserve.txt b/tests/specs/literals/StringLiteral/StringLiteral_QuoteProps_Preserve.txt index 3b7aad64..2566ed27 100644 --- a/tests/specs/literals/StringLiteral/StringLiteral_QuoteProps_Preserve.txt +++ b/tests/specs/literals/StringLiteral/StringLiteral_QuoteProps_Preserve.txt @@ -75,7 +75,7 @@ const objectLiteral = { "1foo": true, "💩": true, "a💩": true, - "c\d": true, + "cd": true, "1": true, "1foo"() {}, get "2foo"() {},