This repository has been archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 660
fix(rome_js_analyze): false positives for noUselessFragments
#3668
#3858
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,12 +9,10 @@ use rome_js_factory::make::{ | |
ident, js_expression_statement, js_string_literal_expression, jsx_tag_expression, | ||
}; | ||
use rome_js_syntax::{ | ||
JsLanguage, JsSyntaxKind, JsxAnyChild, JsxAnyElementName, JsxAnyTag, JsxChildList, JsxElement, | ||
JsxFragment, JsxTagExpression, | ||
}; | ||
use rome_rowan::{ | ||
declare_node_union, AstNode, AstNodeList, BatchMutation, BatchMutationExt, SyntaxNodeOptionExt, | ||
JsLanguage, JsParenthesizedExpression, JsSyntaxKind, JsxAnyChild, JsxAnyElementName, JsxAnyTag, | ||
JsxChildList, JsxElement, JsxFragment, JsxTagExpression, | ||
}; | ||
use rome_rowan::{declare_node_union, AstNode, AstNodeList, BatchMutation, BatchMutationExt}; | ||
|
||
declare_rule! { | ||
/// Disallow unnecessary fragments | ||
|
@@ -108,31 +106,42 @@ impl Rule for NoUselessFragments { | |
let model = ctx.model(); | ||
match node { | ||
NoUselessFragmentsQuery::JsxFragment(fragment) => { | ||
let matches_allowed_parents = node | ||
let parents_where_fragments_must_be_preserved = node | ||
.syntax() | ||
.parent() | ||
.map(|parent| match JsxTagExpression::try_cast(parent) { | ||
Ok(parent) => { | ||
let parent_kind = parent.syntax().parent().kind(); | ||
matches!( | ||
parent_kind, | ||
Some( | ||
Ok(parent) => parent | ||
.syntax() | ||
.parent() | ||
.and_then(|parent| { | ||
if let Some(parenthesized_expression) = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would need to use an equivalent of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I am aware of it and I intentionally left it out. I consider it an edge case that we can tackle if we really know if could cause issue. There's to consider that the formatter will remove all the parentheses |
||
JsParenthesizedExpression::cast_ref(&parent) | ||
{ | ||
parenthesized_expression.syntax().parent() | ||
} else { | ||
Some(parent) | ||
} | ||
}) | ||
.map(|parent| { | ||
matches!( | ||
parent.kind(), | ||
JsSyntaxKind::JS_RETURN_STATEMENT | ||
| JsSyntaxKind::JS_INITIALIZER_CLAUSE | ||
| JsSyntaxKind::JS_CONDITIONAL_EXPRESSION | ||
| JsSyntaxKind::JS_ARROW_FUNCTION_EXPRESSION | ||
| JsSyntaxKind::JS_FUNCTION_EXPRESSION | ||
| JsSyntaxKind::JS_FUNCTION_DECLARATION | ||
| JsSyntaxKind::JS_PROPERTY_OBJECT_MEMBER | ||
) | ||
) | ||
} | ||
}) | ||
.unwrap_or(false), | ||
Err(_) => false, | ||
}) | ||
.unwrap_or(false); | ||
|
||
let child_list = fragment.children(); | ||
|
||
if !matches_allowed_parents { | ||
if !parents_where_fragments_must_be_preserved { | ||
match child_list.first() { | ||
Some(first) if child_list.len() == 1 => { | ||
Some(NoUselessFragmentsState::Child(first)) | ||
|
9 changes: 9 additions & 0 deletions
9
crates/rome_js_analyze/tests/specs/correctness/noUselessFragments/issue_3668.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// should not trigger | ||
function Component2() { | ||
const str = 'str'; | ||
return (<>{str}</>); | ||
} | ||
|
||
const obj = { | ||
element: <>test</> | ||
}; |
19 changes: 19 additions & 0 deletions
19
crates/rome_js_analyze/tests/specs/correctness/noUselessFragments/issue_3668.jsx.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--- | ||
source: crates/rome_js_analyze/tests/spec_tests.rs | ||
expression: issue_3668.jsx | ||
--- | ||
# Input | ||
```js | ||
// should not trigger | ||
function Component2() { | ||
const str = 'str'; | ||
return (<>{str}</>); | ||
} | ||
|
||
const obj = { | ||
element: <>test</> | ||
}; | ||
|
||
``` | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be safer to instead allow list the parents where the fragment must not be preserved? That way adding new AST node types won't break the rule.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it wouldn't. I personally see it way harder to maintain