Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(js_formatter): correctly handle line terminators in JSX string #3025

Merged
merged 2 commits into from
May 30, 2024
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: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 24 additions & 2 deletions crates/biome_formatter/src/token/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,13 @@ pub fn normalize_string(
) -> Cow<str> {
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);
}

Expand Down Expand Up @@ -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")
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<div className="px-1 px-2
px-3"/>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
---
source: crates/biome_formatter_test/src/snapshot_builder.rs
info: jsx/multiline_jsx_string/multiline_jsx_string.jsx
---
# Input

```jsx
<div className="px-1 px-2
px-3"/>;

```


=============================

# 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
<div
className="px-1 px-2
px-3"
/>;
```

## 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
<div<CRLF>
className="px-1 px-2<CRLF>
px-3"<CRLF>
/>;<CRLF>
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "../../../../../../packages/@biomejs/biome/configuration_schema.json",
"formatter": {
"lineEnding": "crlf"
}
}