Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(biome_js_analyze): update noUselessFragments's action logic to avoid the panic #595

Merged
merged 3 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use biome_analyze::context::RuleContext;
use biome_analyze::{declare_rule, ActionCategory, FixKind, Rule, RuleDiagnostic};
use biome_console::markup;
use biome_diagnostics::Applicability;
use biome_js_factory::make::{ident, js_expression_statement, jsx_string, jsx_tag_expression};
use biome_js_factory::make::{
ident, js_expression_statement, js_string_literal_expression, jsx_string, jsx_tag_expression,
};
use biome_js_syntax::{
AnyJsxChild, AnyJsxElementName, AnyJsxTag, JsLanguage, JsParenthesizedExpression, JsSyntaxKind,
JsxChildList, JsxElement, JsxExpressionAttributeValue, JsxFragment, JsxTagExpression,
Expand Down Expand Up @@ -241,7 +243,11 @@ impl Rule for NoUselessFragments {
),
AnyJsxChild::JsxText(text) => {
let new_value = format!("\"{}\"", text.value_token().ok()?);
Some(jsx_string(ident(&new_value)).into_syntax())
if parent.kind() == JsSyntaxKind::JSX_EXPRESSION_ATTRIBUTE_VALUE {
Some(jsx_string(ident(&new_value)).into_syntax())
} else {
Some(js_string_literal_expression(ident(&new_value)).into_syntax())
}
}
AnyJsxChild::JsxExpressionChild(child) => {
child.expression().map(|expression| {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{'' != null && <>stuff</>}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
expression: issue_459.jsx
---
# Input
```js
{'' != null && <>stuff</>}

```

# Diagnostics
```
issue_459.jsx:1:16 lint/complexity/noUselessFragments FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! Avoid using unnecessary Fragment.

> 1 │ {'' != null && <>stuff</>}
│ ^^^^^^^^^^
2 │

i Unsafe fix: Remove the Fragment

1 │ - {''·!=·null·&&·<>stuff</>}
1 │ + {''·!=·null·&&·"stuff"}
2 2 │


```