fix(linter): only report issues on top-level fragment (#3389)

Fixes https://github.com/oxc-project/oxc/issues/3388

The snapshots were conveying this bug already on the `fail` case with
https://github.com/oxc-project/oxc/blob/main/crates/oxc_linter/src/rules/react/jsx_key.rs#L410
This commit is contained in:
Jovi De Croock 2024-05-23 15:12:19 +02:00 committed by GitHub
parent 3a5f088ca3
commit fbccd1fcb2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 11 additions and 13 deletions

View file

@ -81,7 +81,6 @@ fn is_in_array_or_iter<'a, 'b>(
loop {
let parent = ctx.nodes().parent_node(node.id())?;
match parent.kind() {
AstKind::ArrowFunctionExpression(arrow_expr) => {
let is_arrow_expr_statement = matches!(
@ -133,9 +132,10 @@ fn is_in_array_or_iter<'a, 'b>(
return None;
}
AstKind::JSXElement(_) | AstKind::JSXOpeningElement(_) | AstKind::ObjectProperty(_) => {
return None
}
AstKind::JSXElement(_)
| AstKind::JSXOpeningElement(_)
| AstKind::ObjectProperty(_)
| AstKind::JSXFragment(_) => return None,
AstKind::ReturnStatement(_) => {
is_explicit_return = true;
}
@ -313,6 +313,12 @@ fn test() {
const directiveRanges = comments?.map(tryParseTSDirective)
",
r#"
const foo: (JSX.Element | string)[] = [
"text",
<Fragment key={1}>hello world<sup>superscript</sup></Fragment>,
];
"#,
r#"
import { observable } from "mobx";

View file

@ -1,5 +1,6 @@
---
source: crates/oxc_linter/src/tester.rs
assertion_line: 161
expression: jsx_key
---
⚠ eslint-plugin-react(jsx-key): Missing "key" prop for element in array.
@ -128,15 +129,6 @@ expression: jsx_key
╰────
help: Add a "key" prop to the element in the iterator (https://react.dev/learn/rendering-lists#keeping-list-items-in-order-with-key).
⚠ eslint-plugin-react(jsx-key): Missing "key" prop for element in iterator.
╭─[jsx_key.tsx:1:12]
1 │ [1, 2, 3]?.map(x => <><OxcCompilerHello /></>)
· ─┬─ ────────┬───────
· │ ╰── Element generated here
· ╰── Iterator starts here
╰────
help: Add a "key" prop to the element in the iterator (https://react.dev/learn/rendering-lists#keeping-list-items-in-order-with-key).
⚠ eslint-plugin-react(jsx-key): Missing "key" prop for element in iterator.
╭─[jsx_key.tsx:1:11]
1 │ [1, 2, 3].map(x => <>{x}</>);