Skip to content

Commit

Permalink
fix(linter): fix edge case in noUselessFragments (#2520)
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed Apr 19, 2024
1 parent 5477979 commit af3c2a8
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 11 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b
#### Bug fixes

- Fix case where `jsxRuntime` wasn't being respected by `useImportType` rule ([#2473](https://github.com/biomejs/biome/issues/2473)).Contributed by @arendjr
- Fix [#2460](https://github.com/biomejs/biome/issues/2460), where the rule `noUselessFragments` was crashing the linter in some cases. Now cases like these are correctly handled:
```jsx
callFunction(<>{bar}</>)
```
Contributed by @ematipico

### Parser

Expand Down
10 changes: 3 additions & 7 deletions crates/biome_js_analyze/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ mod tests {
use crate::react::hooks::StableHookResult;
use crate::{analyze, AnalysisFilter, ControlFlow};

// #[ignore]
#[ignore]
#[test]
fn quick_test() {
fn markup_to_string(markup: Markup) -> String {
Expand All @@ -252,11 +252,7 @@ mod tests {
String::from_utf8(buffer).unwrap()
}

const SOURCE: &str = r#"<div
class={{
"px-2·foo·p-4·bar": ["foo bar p-4"],
}}
/>"#;
const SOURCE: &str = r#"foo(<>{bar}</>);"#;

let parsed = parse(SOURCE, JsFileSource::tsx(), JsParserOptions::default());

Expand All @@ -268,7 +264,7 @@ mod tests {
dependencies_index: Some(1),
stable_result: StableHookResult::None,
};
let rule_filter = RuleFilter::Rule("nursery", "useSortedClasses");
let rule_filter = RuleFilter::Rule("complexity", "noUselessFragments");

options.configuration.rules.push_rule(
RuleKey::new("nursery", "useHookAtTopLevel"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ use biome_js_factory::make::{
jsx_tag_expression, token, JsxExpressionChildBuilder,
};
use biome_js_syntax::{
AnyJsxChild, AnyJsxElementName, AnyJsxTag, JsLanguage, JsParenthesizedExpression, JsSyntaxKind,
JsxChildList, JsxElement, JsxExpressionAttributeValue, JsxFragment, JsxTagExpression, JsxText,
T,
AnyJsExpression, AnyJsxChild, AnyJsxElementName, AnyJsxTag, JsLanguage,
JsParenthesizedExpression, JsSyntaxKind, JsxChildList, JsxElement, JsxExpressionAttributeValue,
JsxFragment, JsxTagExpression, JsxText, T,
};
use biome_rowan::{declare_node_union, AstNode, AstNodeList, BatchMutation, BatchMutationExt};

Expand Down Expand Up @@ -292,7 +292,11 @@ impl Rule for NoUselessFragments {
})
} else {
child.expression().map(|expression| {
js_expression_statement(expression).build().into_syntax()
if let AnyJsExpression::JsIdentifierExpression(node) = expression {
node.into_syntax()
} else {
js_expression_statement(expression).build().into_syntax()
}
})
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
callFunction(<>{bar}</>)
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
expression: issue_2460.jsx
---
# Input
```jsx
callFunction(<>{bar}</>)

```

# Diagnostics
```
issue_2460.jsx:1:14 lint/complexity/noUselessFragments FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! Avoid using unnecessary Fragment.
> 1 │ callFunction(<>{bar}</>)
│ ^^^^^^^^^^
2 │
i A fragment is redundant if it contains only one child, or if it is the child of a html element, and is not a keyed fragment.
i Unsafe fix: Remove the Fragment
1 │ callFunction(<>{bar}</>)
│ --- ----
```

0 comments on commit af3c2a8

Please sign in to comment.