Skip to content

Commit

Permalink
fix(js_formatter): correctly handle line terminators in JSX string (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ah-yu committed May 30, 2024
1 parent 9a7d790 commit 13811df
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 2 deletions.
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"
}
}

0 comments on commit 13811df

Please sign in to comment.