feat(rome_js_formatter): Remove unnecessary string escapes
MODIFY
crates/rome_js_formatter/src/utils/string_utils.rs
crates/rome_js_formatter/src/utils/string_utils.rs
@@ -453,24 +453,16 @@ impl<'token> LiteralStringNormaliser<'token> {
}
} else if signal == CharSignal::Keep {
reduced_string.push(current_char);
+ signal = CharSignal::None;
}
// The next character is another backslash, or
// a character that should be kept in the next iteration
- else if matches!(
- next_character,
- '\\' | 'v' | 'b' | 'f' | 'n' | 't' | 'r' | 'u' | 'x'
- ) {
+ else if "^\n\r\"'01234567\\bfnrtuvx\u{2028}\u{2029}"
+ .contains(*next_character)
+ {
signal = CharSignal::Keep;
// fallback, keep the backslash
reduced_string.push(current_char);
- }
- // these are character that should stay, but
- // the next iteration should decide if to keep them or not
- else if !next_character.is_alphabetic()
- && *next_character != alternate_quote.as_char()
- && *next_character != preferred_quote.as_char()
- {
- reduced_string.push(current_char);
} else {
// these, usually characters that can have their
// escape removed: "\a" => "a"
@@ -512,23 +504,15 @@ impl<'token> LiteralStringNormaliser<'token> {
}
} else if current_char == alternate_quote.as_char() {
match signal {
- CharSignal::None => {
+ CharSignal::None | CharSignal::Keep => {
reduced_string.push(alternate_quote.as_char());
}
- CharSignal::Keep => {
- reduced_string.push(alternate_quote.as_char());
- signal = CharSignal::None;
- }
- AlreadyPrinted(the_char) => {
- if the_char == alternate_quote.as_char() {
- signal = CharSignal::None;
- continue;
- }
- }
+ AlreadyPrinted(_) => (),
}
} else {
reduced_string.push(current_char);
}
+ signal = CharSignal::None;
}
}
}

DELETE
crates/rome_js_formatter/tests/specs/prettier/js/strings/non-octal-eight-and-nine.js.snap
----
-source: crates/rome_js_formatter/tests/prettier_tests.rs
----
-
-# Input
-
-```js
-// https://github.com/babel/babel/pull/11852
-
-"\8","\9";
-() => {
- "use strict";
- "\8", "\9";
-}
-```
-
-
-# Prettier differences
-
-```diff
---- Prettier
-+++ Rome
-@@ -1,7 +1,7 @@
- // https://github.com/babel/babel/pull/11852
-
--"8", "9";
-+"\8", "\9";
- () => {
- "use strict";
-- "8", "9";
-+ "\8", "\9";
- };
-```
-
-# Output
-
-```js
-// https://github.com/babel/babel/pull/11852
-
-"\8", "\9";
-() => {
- "use strict";
- "\8", "\9";
-};
-```
-
-
-

feat(rome_js_formatter): Remove unnecessary string escapes (#3555)