Skip to content

Commit 8ad2870

Browse files
committed
fix(linter/jsx-key): check if element in push call
1 parent 6b054b6 commit 8ad2870

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

crates/oxc_linter/src/rules/react/jsx_key.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,10 @@ fn is_in_array_or_iter<'a, 'b>(
210210
})
211211
{
212212
return Some(InsideArrayOrIterator::Iterator(span));
213+
} else if ident == "push" && argument.is_some_and(|argument| {
214+
v.arguments.iter().any(|arg| argument.span() == arg.span())
215+
}) {
216+
return Some(InsideArrayOrIterator::Array);
213217
}
214218
}
215219
}
@@ -505,6 +509,8 @@ fn test() {
505509
}))}
506510
",
507511
r"const DummyComponent: FC<{ children: ReactNode }> = ({ children }) => { const wrappedChildren = Children.map(children, (child) => { return <div>{child}</div>; }); return <main>{wrappedChildren}</main>; };",
512+
"const arr = [];arr.push(<div key={0} />);",
513+
"const arr = [];arr.push(<div key={0} />, <div key={1} />);",
508514
];
509515

510516
let fail = vec![
@@ -618,6 +624,10 @@ fn test() {
618624
Children.toArray([1, 2 ,3].map(x => <App />));
619625
Children.toArray(Array.from([1, 2 ,3], x => <App />));
620626
",
627+
"const arr = [];arr.push(<div />);",
628+
"const arr = [];arr.push(<div key={0} />, <div />);",
629+
"const arr = [];arr.push(<div />, <div key={0} />);",
630+
"const arr = [];arr.push(<div />, <div />);",
621631
];
622632

623633
Tester::new(JsxKey::NAME, JsxKey::PLUGIN, pass, fail).test_and_snapshot();

crates/oxc_linter/src/snapshots/react_jsx_key.snap

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,3 +346,33 @@ source: crates/oxc_linter/src/tester.rs
346346
12 │
347347
╰────
348348
help: Add a "key" prop to the element in the iterator (https://react.dev/learn/rendering-lists#keeping-list-items-in-order-with-key).
349+
350+
⚠ eslint-plugin-react(jsx-key): Missing "key" prop for element in array.
351+
╭─[jsx_key.tsx:1:26]
352+
1 │ const arr = [];arr.push(<div />);
353+
· ───
354+
╰────
355+
356+
⚠ eslint-plugin-react(jsx-key): Missing "key" prop for element in array.
357+
╭─[jsx_key.tsx:1:43]
358+
1 │ const arr = [];arr.push(<div key={0} />, <div />);
359+
· ───
360+
╰────
361+
362+
⚠ eslint-plugin-react(jsx-key): Missing "key" prop for element in array.
363+
╭─[jsx_key.tsx:1:26]
364+
1 │ const arr = [];arr.push(<div />, <div key={0} />);
365+
· ───
366+
╰────
367+
368+
⚠ eslint-plugin-react(jsx-key): Missing "key" prop for element in array.
369+
╭─[jsx_key.tsx:1:26]
370+
1 │ const arr = [];arr.push(<div />, <div />);
371+
· ───
372+
╰────
373+
374+
⚠ eslint-plugin-react(jsx-key): Missing "key" prop for element in array.
375+
╭─[jsx_key.tsx:1:35]
376+
1 │ const arr = [];arr.push(<div />, <div />);
377+
· ───
378+
╰────

0 commit comments

Comments
 (0)