+
Skip to content
This repository was archived by the owner on Aug 31, 2023. It is now read-only.
Merged
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
30 changes: 7 additions & 23 deletions crates/rome_js_formatter/src/utils/string_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Borrowed from prettier.

.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"
Expand Down Expand Up @@ -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;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
---
source: crates/rome_js_formatter/tests/prettier_tests.rs
info:
test_file: js/directives/escaped.js
---

# Input
Expand Down Expand Up @@ -48,7 +50,7 @@ source: crates/rome_js_formatter/tests/prettier_tests.rs
```diff
--- Prettier
+++ Rome
@@ -6,19 +6,19 @@
@@ -6,20 +6,20 @@
"\'";
"\"";
"\\";
Expand All @@ -59,19 +61,22 @@ source: crates/rome_js_formatter/tests/prettier_tests.rs
+"hola";
"hol\\a (the a is not escaped)";
"hol\\a (the a is not escaped)";
"multiple \a unnecessary \a escapes";
-"multiple \a unnecessary \a escapes";
"multiple \a unnecessary \a escapes";
+"multiple a unnecessary a escapes";
"unnecessarily escaped character preceded by escaped backslash \\\a";
"unnecessarily escaped character preceded by escaped backslash \\\a";
-"unnecessarily escaped character preceded by escaped backslash \\\a";
+"unnecessarily escaped character preceded by escaped backslash \\a";
"unescaped character preceded by two escaped backslashes \\\\a";
"unescaped character preceded by two escaped backslashes \\\\a";
"\a\a"; // consecutive unnecessarily escaped characters
-"\a\a"; // consecutive unnecessarily escaped characters
-"escaped \u2030 \‰ (should still stay escaped)";
+"aa"; // consecutive unnecessarily escaped characters
"escaped \u2030 \‰ (should still stay escaped)";
+"escaped \u2030 ‰ (should still stay escaped)";

// Meaningful escapes
// Commented out to avoid `SyntaxError: Octal literals are not allowed in strict mode.`
```

# Output
Expand All @@ -93,12 +98,12 @@ source: crates/rome_js_formatter/tests/prettier_tests.rs
"multiple \a unnecessary \a escapes";
"multiple a unnecessary a escapes";
"unnecessarily escaped character preceded by escaped backslash \\\a";
"unnecessarily escaped character preceded by escaped backslash \\\a";
"unnecessarily escaped character preceded by escaped backslash \\a";
"unescaped character preceded by two escaped backslashes \\\\a";
"unescaped character preceded by two escaped backslashes \\\\a";
"\a\a"; // consecutive unnecessarily escaped characters
"aa"; // consecutive unnecessarily escaped characters
"escaped \u2030 \‰ (should still stay escaped)";
"escaped \u2030 ‰ (should still stay escaped)";

// Meaningful escapes
// Commented out to avoid `SyntaxError: Octal literals are not allowed in strict mode.`
Expand Down

This file was deleted.

Loading
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载