Skip to content

Commit

Permalink
fix: avoid escape for jsx (#842)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gumichocopengin8 authored Nov 23, 2023
1 parent 45e29db commit 55e8531
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 4 deletions.
9 changes: 8 additions & 1 deletion crates/biome_formatter/src/token/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,11 @@ impl Quote {
///
/// By default the formatter uses `\n` as a newline. The function replaces
/// `\r\n` with `\n`,
pub fn normalize_string(raw_content: &str, preferred_quote: Quote) -> Cow<str> {
pub fn normalize_string(
raw_content: &str,
preferred_quote: Quote,
is_escape_preserved: bool,
) -> Cow<str> {
let alternate_quote = preferred_quote.other();

// A string should be manipulated only if its raw content contains backslash or quotes
Expand Down Expand Up @@ -196,6 +200,9 @@ pub fn normalize_string(raw_content: &str, preferred_quote: Quote) -> Cow<str> {
// escape removed: "\a" => "a"
// So we ignore the current slash and we continue
// to the next iteration
if is_escape_preserved {
reduced_string.push(current_char);
}
continue;
}
} else {
Expand Down
8 changes: 6 additions & 2 deletions crates/biome_js_formatter/src/utils/string_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,12 +309,16 @@ impl<'token> LiteralStringNormaliser<'token> {

fn normalize_string(&self, string_information: &StringInformation) -> Cow<'token, str> {
let raw_content = self.raw_content();
let is_escape_preserved = matches!(self.token.token.kind(), JSX_STRING_LITERAL);

if matches!(self.token.parent_kind, StringLiteralParentKind::Directive) {
return Cow::Borrowed(raw_content);
}

normalize_string(raw_content, string_information.preferred_quote.into())
normalize_string(
raw_content,
string_information.preferred_quote.into(),
is_escape_preserved,
)
}

fn raw_content(&self) -> &'token str {
Expand Down
15 changes: 15 additions & 0 deletions crates/biome_js_formatter/tests/specs/jsx/attribute_escape.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const a = () => {
const a = "\test";
const b = "\\test";
const c = "\\\test";
const d = "\\\\test";

return (
<>
<input name="pin" type="text" pattern="\d{4,4}" required />
<input name="\\pin" type="text" pattern="\d{4,4}" required />
<input name="\\\pin" type="text" pattern="\d{4,4}" required />
<input name="\\\\pin" type="text" pattern="\d{4,4}" required />
</>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
---
source: crates/biome_formatter_test/src/snapshot_builder.rs
assertion_line: 212
info: jsx/attribute_escape.jsx
---

# Input

```jsx
const a = () => {
const a = "\test";
const b = "\\test";
const c = "\\\test";
const d = "\\\\test";
return (
<>
<input name="pin" type="text" pattern="\d{4,4}" required />
<input name="\\pin" type="text" pattern="\d{4,4}" required />
<input name="\\\pin" type="text" pattern="\d{4,4}" required />
<input name="\\\\pin" type="text" pattern="\d{4,4}" required />
</>
);
};
```


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

# 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 comma: All
Semicolons: Always
Arrow parentheses: Always
Bracket spacing: true
Bracket same line: false
-----

```jsx
const a = () => {
const a = "\test";
const b = "\\test";
const c = "\\\test";
const d = "\\\\test";
return (
<>
<input name="pin" type="text" pattern="\d{4,4}" required />
<input name="\\pin" type="text" pattern="\d{4,4}" required />
<input name="\\\pin" type="text" pattern="\d{4,4}" required />
<input name="\\\\pin" type="text" pattern="\d{4,4}" required />
</>
);
};
```


2 changes: 1 addition & 1 deletion crates/biome_json_formatter/src/format_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl Format<JsonFormatContext> for CleanedStringLiteralText<'_> {
let content = self.token.text_trimmed();
let raw_content = &content[1..content.len() - 1];

let text = match normalize_string(raw_content, Quote::Double) {
let text = match normalize_string(raw_content, Quote::Double, false) {
Cow::Borrowed(_) => Cow::Borrowed(content),
Cow::Owned(raw_content) => Cow::Owned(std::format!(
"{}{}{}",
Expand Down

0 comments on commit 55e8531

Please sign in to comment.