Skip to content

Commit e943457

Browse files
committed
fix(linter): prevent useConsistentCurlyBraces from producing invalid JSX
Add '{', '<', and '&' to FORBIDDEN_CHARS to prevent the rule from suggesting to remove curly braces from strings containing characters that cannot appear unescaped in JSX text, which would produce invalid or semantically different JSX. Fixes #8011
1 parent 0f7171a commit e943457

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@biomejs/biome": patch
3+
---
4+
5+
Fixed [#8011](https://github.com/biomejs/biome/issues/8011): [`useConsistentCurlyBraces`](https://biomejs.dev/linter/rules/use-consistent-curly-braces/) no longer suggests removing curly braces from JSX expression children containing characters that would cause parsing issues or semantic changes when converted to plain JSX text (`{`, `}`, `<`, `>`, `&`).

crates/biome_js_analyze/src/lint/style/use_consistent_curly_braces.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,17 @@ fn contains_only_spaces(literal: &JsStringLiteralExpression) -> bool {
410410
.is_ok_and(|text| text.bytes().all(|b| b == b' '))
411411
}
412412

413-
const FORBIDDEN_CHARS: [char; 4] = ['>', '"', '\'', '}'];
413+
/// Characters that would cause parsing issues or semantic changes if a JSX expression
414+
/// child is converted to plain JSX text. When a string literal contains any of these,
415+
/// we must NOT suggest removing the curly braces.
416+
///
417+
/// - `{` `}` - would be parsed as expression delimiters
418+
/// - `<` `>` - would be parsed as tag delimiters
419+
/// - `&` - would be parsed as HTML entity start
420+
/// - `"` `'` - included for consistency, though mainly relevant in attribute contexts
421+
///
422+
/// See: https://github.com/biomejs/biome/issues/8011
423+
const FORBIDDEN_CHARS: [char; 7] = ['{', '}', '<', '>', '&', '"', '\''];
414424

415425
fn contains_forbidden_chars(str_literal: &JsStringLiteralExpression) -> bool {
416426
str_literal.inner_string_text().is_ok_and(|text| {

crates/biome_js_analyze/tests/specs/style/useConsistentCurlyBraces/valid.jsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,22 @@ let baz = 4;
2828

2929
<Foo>{'Invalid closing tag }'}</Foo>
3030

31+
<Foo>Invalid opening tag {'{'}</Foo>
32+
33+
<Foo>{'Invalid opening tag {'}</Foo>
34+
35+
<Foo>{'start {{'}</Foo>
36+
37+
<Foo>{'}} end'}</Foo>
38+
39+
<Foo>{'<script>'}</Foo>
40+
41+
<Foo>{'a < b'}</Foo>
42+
43+
<Foo>{'&amp;'}</Foo>
44+
45+
<Foo>{'Tom & Jerry'}</Foo>
46+
3147
<Foo>Jupiter {">"} Venus</Foo>
3248

3349
<Foo>Jupiter {'>'} Venus</Foo>

crates/biome_js_analyze/tests/specs/style/useConsistentCurlyBraces/valid.jsx.snap

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,22 @@ let baz = 4;
3434
3535
<Foo>{'Invalid closing tag }'}</Foo>
3636
37+
<Foo>Invalid opening tag {'{'}</Foo>
38+
39+
<Foo>{'Invalid opening tag {'}</Foo>
40+
41+
<Foo>{'start {{'}</Foo>
42+
43+
<Foo>{'}} end'}</Foo>
44+
45+
<Foo>{'<script>'}</Foo>
46+
47+
<Foo>{'a < b'}</Foo>
48+
49+
<Foo>{'&amp;'}</Foo>
50+
51+
<Foo>{'Tom & Jerry'}</Foo>
52+
3753
<Foo>Jupiter {">"} Venus</Foo>
3854
3955
<Foo>Jupiter {'>'} Venus</Foo>

0 commit comments

Comments
 (0)