From ae2343f1bdeca1b31434355647cf96f780831f38 Mon Sep 17 00:00:00 2001 From: Zheyu Zhang Date: Wed, 29 May 2024 18:05:28 +0800 Subject: [PATCH 1/2] fix: normalize newline --- crates/biome_formatter/src/token/string.rs | 26 +++++++- .../multiline_jsx_string.jsx | 2 + .../multiline_jsx_string.jsx.snap | 66 +++++++++++++++++++ .../jsx/multiline_jsx_string/options.json | 6 ++ 4 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 crates/biome_js_formatter/tests/specs/jsx/multiline_jsx_string/multiline_jsx_string.jsx create mode 100644 crates/biome_js_formatter/tests/specs/jsx/multiline_jsx_string/multiline_jsx_string.jsx.snap create mode 100644 crates/biome_js_formatter/tests/specs/jsx/multiline_jsx_string/options.json diff --git a/crates/biome_formatter/src/token/string.rs b/crates/biome_formatter/src/token/string.rs index c7626f36d81d..66274802a089 100644 --- a/crates/biome_formatter/src/token/string.rs +++ b/crates/biome_formatter/src/token/string.rs @@ -140,8 +140,13 @@ pub fn normalize_string( ) -> Cow { let alternate_quote = preferred_quote.other(); - // A string should be manipulated only if its raw content contains backslash or quotes - if !raw_content.contains(['\\', preferred_quote.as_char(), alternate_quote.as_char()]) { + // A string should be manipulated only if its raw content contains backslash, quotes or line terminators + if !raw_content.contains([ + '\\', + '\r', + preferred_quote.as_char(), + alternate_quote.as_char(), + ]) { return Cow::Borrowed(raw_content); } @@ -264,3 +269,20 @@ pub fn normalize_string( } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn normalize_newline() { + assert_eq!( + normalize_string("a\nb", Quote::Double, true), + Cow::Borrowed("a\nb") + ); + assert_eq!( + normalize_string("a\r\nb", Quote::Double, false), + Cow::Borrowed("a\nb") + ); + } +} diff --git a/crates/biome_js_formatter/tests/specs/jsx/multiline_jsx_string/multiline_jsx_string.jsx b/crates/biome_js_formatter/tests/specs/jsx/multiline_jsx_string/multiline_jsx_string.jsx new file mode 100644 index 000000000000..44954aeb2576 --- /dev/null +++ b/crates/biome_js_formatter/tests/specs/jsx/multiline_jsx_string/multiline_jsx_string.jsx @@ -0,0 +1,2 @@ +
; diff --git a/crates/biome_js_formatter/tests/specs/jsx/multiline_jsx_string/multiline_jsx_string.jsx.snap b/crates/biome_js_formatter/tests/specs/jsx/multiline_jsx_string/multiline_jsx_string.jsx.snap new file mode 100644 index 000000000000..e7d14999a3c8 --- /dev/null +++ b/crates/biome_js_formatter/tests/specs/jsx/multiline_jsx_string/multiline_jsx_string.jsx.snap @@ -0,0 +1,66 @@ +--- +source: crates/biome_formatter_test/src/snapshot_builder.rs +info: jsx/multiline_jsx_string/multiline_jsx_string.jsx +--- +# Input + +```jsx +
; + +``` + + +============================= + +# Outputs + +## Output 1 + +----- +Indent style: Tab +Indent width: 2 +Line ending: LF +Line width: 80 +Quote style: Double Quotes +JSX quote style: Double Quotes +Quote properties: As needed +Trailing commas: All +Semicolons: Always +Arrow parentheses: Always +Bracket spacing: true +Bracket same line: false +Attribute Position: Auto +----- + +```jsx +
; +``` + +## Output 1 + +----- +Indent style: Tab +Indent width: 2 +Line ending: CRLF +Line width: 80 +Quote style: Double Quotes +JSX quote style: Double Quotes +Quote properties: As needed +Trailing commas: All +Semicolons: Always +Arrow parentheses: Always +Bracket spacing: true +Bracket same line: false +Attribute Position: Auto +----- + +```jsx + + className="px-1 px-2 +px-3" +/>; +``` diff --git a/crates/biome_js_formatter/tests/specs/jsx/multiline_jsx_string/options.json b/crates/biome_js_formatter/tests/specs/jsx/multiline_jsx_string/options.json new file mode 100644 index 000000000000..ac75c6e7281f --- /dev/null +++ b/crates/biome_js_formatter/tests/specs/jsx/multiline_jsx_string/options.json @@ -0,0 +1,6 @@ +{ + "$schema": "../../../../../../packages/@biomejs/biome/configuration_schema.json", + "formatter": { + "lineEnding": "crlf" + } +} From 5b0951b2e3fb9a82ee95d572de253cef6923efc1 Mon Sep 17 00:00:00 2001 From: Zheyu Zhang Date: Thu, 30 May 2024 10:35:30 +0800 Subject: [PATCH 2/2] add a changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index cba72da77339..667e814896ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -236,6 +236,7 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b - Fix [#2470](https://github.com/biomejs/biome/issues/2470) by avoid introducing linebreaks in single line string interpolations. Contributed by @ah-yu - Resolve deadlocks by narrowing the scope of locks. Contributed by @mechairoi - Fix [#2782](https://github.com/biomejs/biome/issues/2782) by computing the enabled rules by taking the override settings into consideration. Contributed by @ematipico +- Fix [https://github.com/biomejs/biome/issues/2877] by correctly handling line terminators in JSX string. Contributed by @ah-yu ### JavaScript APIs