Skip to content
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
1 change: 0 additions & 1 deletion crates/oxc_formatter/src/formatter/format_element/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ pub const LINE_TERMINATORS: [char; 3] = ['\r', LINE_SEPARATOR, PARAGRAPH_SEPARAT

/// Replace the line terminators matching the provided list with "\n"
/// since its the only line break type supported by the printer
#[expect(unused)]
pub fn normalize_newlines<const N: usize>(text: &str, terminators: [char; N]) -> Cow<'_, str> {
let mut result = String::new();
let mut last_end = 0;
Expand Down
27 changes: 22 additions & 5 deletions crates/oxc_formatter/src/utils/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ impl<'a> LiteralStringNormalizer<'a> {
}

fn normalize_directive(&self, string_information: StringInformation) -> Cow<'a, str> {
// In diretcives, unnecessary escapes should be preserved.
// In directives, unnecessary escapes should be preserved.
// See https://github.com/prettier/prettier/issues/1555
// Thus we don't normalize the string.
//
Expand All @@ -217,10 +217,27 @@ impl<'a> LiteralStringNormalizer<'a> {
//
// Note that we could change the quotes if the preferred quote is escaped.
// However, Prettier doesn't go that far.
if string_information.raw_content_has_quotes {
Cow::Borrowed(self.token.string)
} else {
self.swap_quotes(self.raw_content(), string_information)
let normalized = normalize_newlines(self.raw_content(), ['\r']);
match normalized {
Cow::Borrowed(string) => {
if string_information.raw_content_has_quotes {
Cow::Borrowed(self.token.string)
} else {
self.swap_quotes(string, string_information)
}
}
Cow::Owned(string) => {
let mut s = String::with_capacity(string.len() + 2);
let quote = if string_information.raw_content_has_quotes {
string_information.current_quote.as_char()
} else {
string_information.preferred_quote.as_char()
};
s.push(quote);
s.push_str(&string);
s.push(quote);
Cow::Owned(s)
}
}
}

Expand Down
Loading