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(lint/useJsxKeyInIterable): handle fragments more comprehensively #2747

Conversation

dyc3
Copy link
Contributor

@dyc3 dyc3 commented May 6, 2024

Summary

This PR makes the useJsxKeyInIterable lint handle more cases related to JSX fragments. It can now handle cases like this:

// valid
[].map((item) => {
	return <>{item.condition ? <div key={item.id} /> : <div key={item.id}>foo</div>}</>;
});

// invalid
[].map((item) => {
	return <>{item.condition ? <div /> : <div>foo</div>}</>;
});

related to: #2590

Test Plan

Added snapshot tests

cargo test -p biome_js_analyze use_jsx_key_in_iterable

@github-actions github-actions bot added A-Linter Area: linter L-JavaScript Language: JavaScript and super languages labels May 6, 2024
@dyc3 dyc3 force-pushed the 05-06-fix_lint_usejsxkeyiniterable_handle_fragments_more_comprehensively branch from cc6d7f2 to c93fd10 Compare May 6, 2024 15:56
Copy link

codspeed-hq bot commented May 6, 2024

CodSpeed Performance Report

Merging #2747 will not alter performance

Comparing dyc3:05-06-fix_lint_usejsxkeyiniterable_handle_fragments_more_comprehensively (3abadbe) with main (49cace8)

Summary

✅ 97 untouched benchmarks

@dyc3
Copy link
Contributor Author

dyc3 commented May 6, 2024

Once this and #2749 is merged, I have another branch that should completely resolve #2590.

@vohoanglong0107
Copy link
Contributor

In Eslint, using fragments inside an iterator is encouraged by utilizing the Fragment component exported by React:

a.map(e => <Fragment key={e.id}>{e}</Fragment>)

I'm not necessarily advocating for this approach; just providing some food for thought.

@dyc3 dyc3 force-pushed the 05-06-fix_lint_usejsxkeyiniterable_handle_fragments_more_comprehensively branch from c93fd10 to 4a28330 Compare May 8, 2024 13:47
Copy link
Member

@ematipico ematipico left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recursion spotted! 😄 We should remove it

Comment on lines 287 to 301
.filter_map(|child| {
match &child {
AnyJsxChild::JsxElement(_) | AnyJsxChild::JsxSelfClosingElement(_) => {
any_tags_found = true;
}
// HACK: don't flag the entire fragment if there's a conditional expression
AnyJsxChild::JsxExpressionChild(node) => {
let expr = node.expression()?;
if expr.as_js_conditional_expression().is_some() {
any_tags_found = true;
}
}
_ => {}
}
handle_jsx_child(&child, model)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mixing a closure approach with internal filtering is not very idiomatic in the Rust world. I suggest using just a loop to collect the information that you require.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if I've fixed this the way you want, but it looks a bit cleaner.

@dyc3 dyc3 force-pushed the 05-06-fix_lint_usejsxkeyiniterable_handle_fragments_more_comprehensively branch from 4a28330 to 77df690 Compare May 8, 2024 20:42
@dyc3 dyc3 force-pushed the 05-06-fix_lint_usejsxkeyiniterable_handle_fragments_more_comprehensively branch from 77df690 to e89f0d9 Compare May 9, 2024 11:04
Copy link
Member

@ematipico ematipico left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! We should add a new changelog entry, then we can merge it

@dyc3 dyc3 force-pushed the 05-06-fix_lint_usejsxkeyiniterable_handle_fragments_more_comprehensively branch from e89f0d9 to 3abadbe Compare May 10, 2024 05:26
@github-actions github-actions bot added the A-Changelog Area: changelog label May 10, 2024
@ematipico ematipico merged commit 7c577de into biomejs:main May 10, 2024
12 checks passed
@dyc3 dyc3 deleted the 05-06-fix_lint_usejsxkeyiniterable_handle_fragments_more_comprehensively branch May 10, 2024 11:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Changelog Area: changelog A-Linter Area: linter L-JavaScript Language: JavaScript and super languages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants