Skip to content

Commit

Permalink
fix(analyzer): lint/complexity/noUselessFragments removing assignment #…
Browse files Browse the repository at this point in the history
  • Loading branch information
denbezrukov authored Sep 12, 2023
1 parent 4a024bb commit 4ec3bcd
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 4 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -220,7 +220,12 @@ impl Rule for NoUselessFragments {
node.remove_node_from_list(&mut mutation);
}
} else if let Some(parent) = node.parent::<JsxTagExpression>() {
let parent = parent.syntax().parent()?;
// We need to remove {} if the fragment is inside an attribute value: <div x-some-prop={<>Foo</>} />
let parent = match parent.parent::<JsxExpressionAttributeValue>() {
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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
arr = <>Error</>
Original file line number Diff line number Diff line change
@@ -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 │
```


4 changes: 3 additions & 1 deletion website/src/content/docs/internals/changelog.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 4ec3bcd

Please sign in to comment.