Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

fix(rome_js_analyze): noUselessFragments use JsxString node when replacing JsxText #4662

Merged
merged 2 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,10 @@ multiple files:

The rule no longer reports false positive diagnostics when accessing properties directly from a hook and calling a hook inside function arguments.

- Fix [noUselessFragments](https://docs.rome.tools/lint/rules/nouselessfragments/)'s panics when running `rome check --apply-unsafe` ([#4637](https://github.com/rome/tools/issues/4639))

This rule's code action emits an invalid AST, so I fixed using JsxString instead of JsStringLiteral

### Parser

### VSCode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ use rome_analyze::context::RuleContext;
use rome_analyze::{declare_rule, ActionCategory, Rule, RuleDiagnostic};
use rome_console::markup;
use rome_diagnostics::Applicability;
use rome_js_factory::make::{
ident, js_expression_statement, js_string_literal_expression, jsx_tag_expression,
};
use rome_js_factory::make::{ident, js_expression_statement, jsx_string, jsx_tag_expression};
use rome_js_syntax::{
AnyJsxChild, AnyJsxElementName, AnyJsxTag, JsLanguage, JsParenthesizedExpression, JsSyntaxKind,
JsxChildList, JsxElement, JsxFragment, JsxTagExpression,
Expand Down Expand Up @@ -243,11 +241,7 @@ impl Rule for NoUselessFragments {
),
AnyJsxChild::JsxText(text) => {
let new_value = format!("\"{}\"", text.value_token().ok()?);
Some(
js_string_literal_expression(ident(&new_value))
.syntax()
.clone(),
)
Some(jsx_string(ident(&new_value)).syntax().clone())
}
AnyJsxChild::JsxExpressionChild(child) => {
child.expression().map(|expression| {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function SomeComponent() {
return <div x-some-prop={<>Foo</>} />;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
source: crates/rome_js_analyze/tests/spec_tests.rs
expression: issue_4639.jsx
---
# Input
```js
export function SomeComponent() {
return <div x-some-prop={<>Foo</>} />;
}

```

# Diagnostics
```
issue_4639.jsx:2:28 lint/complexity/noUselessFragments FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! Avoid using unnecessary Fragment.

1 │ export function SomeComponent() {
> 2 │ return <div x-some-prop={<>Foo</>} />;
│ ^^^^^^^^
3 │ }
4 │

i Suggested fix: Remove the Fragment

1 1 │ export function SomeComponent() {
2 │ - ··return·<div·x-some-prop={<>Foo</>}·/>;
2 │ + ··return·<div·x-some-prop="Foo"·/>;
3 3 │ }
4 4 │


```