From e94345735599d610e7ef65d8de504092d8d4f044 Mon Sep 17 00:00:00 2001 From: Tu Shaokun <2801884530@qq.com> Date: Sat, 13 Dec 2025 23:13:50 +0800 Subject: [PATCH] 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 --- .changeset/fix-curly-braces-opening-brace.md | 5 +++++ .../lint/style/use_consistent_curly_braces.rs | 12 +++++++++++- .../style/useConsistentCurlyBraces/valid.jsx | 16 ++++++++++++++++ .../useConsistentCurlyBraces/valid.jsx.snap | 16 ++++++++++++++++ 4 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 .changeset/fix-curly-braces-opening-brace.md diff --git a/.changeset/fix-curly-braces-opening-brace.md b/.changeset/fix-curly-braces-opening-brace.md new file mode 100644 index 000000000000..cfaf2049e5f1 --- /dev/null +++ b/.changeset/fix-curly-braces-opening-brace.md @@ -0,0 +1,5 @@ +--- +"@biomejs/biome": patch +--- + +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 (`{`, `}`, `<`, `>`, `&`). diff --git a/crates/biome_js_analyze/src/lint/style/use_consistent_curly_braces.rs b/crates/biome_js_analyze/src/lint/style/use_consistent_curly_braces.rs index 3a1ecad3c5d1..a793be454e35 100644 --- a/crates/biome_js_analyze/src/lint/style/use_consistent_curly_braces.rs +++ b/crates/biome_js_analyze/src/lint/style/use_consistent_curly_braces.rs @@ -410,7 +410,17 @@ fn contains_only_spaces(literal: &JsStringLiteralExpression) -> bool { .is_ok_and(|text| text.bytes().all(|b| b == b' ')) } -const FORBIDDEN_CHARS: [char; 4] = ['>', '"', '\'', '}']; +/// Characters that would cause parsing issues or semantic changes if a JSX expression +/// child is converted to plain JSX text. When a string literal contains any of these, +/// we must NOT suggest removing the curly braces. +/// +/// - `{` `}` - would be parsed as expression delimiters +/// - `<` `>` - would be parsed as tag delimiters +/// - `&` - would be parsed as HTML entity start +/// - `"` `'` - included for consistency, though mainly relevant in attribute contexts +/// +/// See: https://github.com/biomejs/biome/issues/8011 +const FORBIDDEN_CHARS: [char; 7] = ['{', '}', '<', '>', '&', '"', '\'']; fn contains_forbidden_chars(str_literal: &JsStringLiteralExpression) -> bool { str_literal.inner_string_text().is_ok_and(|text| { diff --git a/crates/biome_js_analyze/tests/specs/style/useConsistentCurlyBraces/valid.jsx b/crates/biome_js_analyze/tests/specs/style/useConsistentCurlyBraces/valid.jsx index 4fe602bf27aa..79bdbd532d26 100644 --- a/crates/biome_js_analyze/tests/specs/style/useConsistentCurlyBraces/valid.jsx +++ b/crates/biome_js_analyze/tests/specs/style/useConsistentCurlyBraces/valid.jsx @@ -28,6 +28,22 @@ let baz = 4; {'Invalid closing tag }'} +Invalid opening tag {'{'} + +{'Invalid opening tag {'} + +{'start {{'} + +{'}} end'} + +{'