diff --git a/CHANGELOG.md b/CHANGELOG.md index 0192b341605a..a5f4b2170527 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,9 +49,11 @@ Read our [guidelines for writing a good changelog entry](https://github.com/biom const src: string; } ``` - Contributed by [@denbezrukov](https://github.com/denbezrukov) +- Fix [#258](https://github.com/biomejs/biome/issues/258), fix [noUselessFragments](https://biomejs.dev/linter/rules/no-useless-fragments/) the case where the rule removing an assignment. Contributed by [@denbezrukov](https://github.com/denbezrukov) + + ### Parser ### VSCode diff --git a/crates/biome_js_analyze/src/semantic_analyzers/complexity/no_useless_fragments.rs b/crates/biome_js_analyze/src/semantic_analyzers/complexity/no_useless_fragments.rs index 21dde4a6c296..11f9153e7e87 100644 --- a/crates/biome_js_analyze/src/semantic_analyzers/complexity/no_useless_fragments.rs +++ b/crates/biome_js_analyze/src/semantic_analyzers/complexity/no_useless_fragments.rs @@ -8,7 +8,7 @@ use biome_diagnostics::Applicability; use biome_js_factory::make::{ident, js_expression_statement, jsx_string, jsx_tag_expression}; use biome_js_syntax::{ AnyJsxChild, AnyJsxElementName, AnyJsxTag, JsLanguage, JsParenthesizedExpression, JsSyntaxKind, - JsxChildList, JsxElement, JsxFragment, JsxTagExpression, + JsxChildList, JsxElement, JsxExpressionAttributeValue, JsxFragment, JsxTagExpression, }; use biome_rowan::{declare_node_union, AstNode, AstNodeList, BatchMutation, BatchMutationExt}; @@ -220,7 +220,12 @@ impl Rule for NoUselessFragments { node.remove_node_from_list(&mut mutation); } } else if let Some(parent) = node.parent::() { - let parent = parent.syntax().parent()?; + // We need to remove {} if the fragment is inside an attribute value:
Foo} /> + let parent = match parent.parent::() { + Some(grand_parent) => grand_parent.into_syntax(), + None => parent.into_syntax(), + }; + let child = node.children().first(); if let Some(child) = child { let new_node = match child { diff --git a/crates/biome_js_analyze/tests/specs/complexity/noUselessFragments/assigments.jsx b/crates/biome_js_analyze/tests/specs/complexity/noUselessFragments/assigments.jsx new file mode 100644 index 000000000000..2d4d15078ccc --- /dev/null +++ b/crates/biome_js_analyze/tests/specs/complexity/noUselessFragments/assigments.jsx @@ -0,0 +1 @@ +arr = <>Error diff --git a/crates/biome_js_analyze/tests/specs/complexity/noUselessFragments/assigments.jsx.snap b/crates/biome_js_analyze/tests/specs/complexity/noUselessFragments/assigments.jsx.snap new file mode 100644 index 000000000000..e49acc096523 --- /dev/null +++ b/crates/biome_js_analyze/tests/specs/complexity/noUselessFragments/assigments.jsx.snap @@ -0,0 +1,30 @@ +--- +source: crates/biome_js_analyze/tests/spec_tests.rs +expression: assigments.jsx +--- +# Input +```js +arr = <>Error + +``` + +# Diagnostics +``` +assigments.jsx:1:7 lint/complexity/noUselessFragments FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + ! Avoid using unnecessary Fragment. + + > 1 │ arr = <>Error + │ ^^^^^^^^^^ + 2 │ + + i Suggested fix: Remove the Fragment + + 1 │ - arr·=·<>Error + 1 │ + arr·=·"Error" + 2 2 │ + + +``` + + diff --git a/website/src/content/docs/internals/changelog.mdx b/website/src/content/docs/internals/changelog.mdx index f9d2b09a5bfd..fa9f0fc49711 100644 --- a/website/src/content/docs/internals/changelog.mdx +++ b/website/src/content/docs/internals/changelog.mdx @@ -55,9 +55,11 @@ Read our [guidelines for writing a good changelog entry](https://github.com/biom const src: string; } ``` - Contributed by [@denbezrukov](https://github.com/denbezrukov) +- Fix [#258](https://github.com/biomejs/biome/issues/258), fix [noUselessFragments](https://biomejs.dev/linter/rules/no-useless-fragments/) the case where the rule removing an assignment. Contributed by [@denbezrukov](https://github.com/denbezrukov) + + ### Parser ### VSCode