Skip to content
Draft
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
12 changes: 12 additions & 0 deletions crates/oxc_linter/src/rules/react/jsx_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,12 @@ fn is_in_array_or_iter<'a, 'b>(
})
{
return Some(InsideArrayOrIterator::Iterator(span));
} else if ident == "push"
&& argument.is_some_and(|argument| {
v.arguments.iter().any(|arg| argument.span() == arg.span())
})
{
return Some(InsideArrayOrIterator::Array);
}
}
}
Expand Down Expand Up @@ -505,6 +511,8 @@ fn test() {
}))}
",
r"const DummyComponent: FC<{ children: ReactNode }> = ({ children }) => { const wrappedChildren = Children.map(children, (child) => { return <div>{child}</div>; }); return <main>{wrappedChildren}</main>; };",
"const arr = [];arr.push(<div key={0} />);",
"const arr = [];arr.push(<div key={0} />, <div key={1} />);",
];

let fail = vec![
Expand Down Expand Up @@ -618,6 +626,10 @@ fn test() {
Children.toArray([1, 2 ,3].map(x => <App />));
Children.toArray(Array.from([1, 2 ,3], x => <App />));
",
"const arr = [];arr.push(<div />);",
"const arr = [];arr.push(<div key={0} />, <div />);",
"const arr = [];arr.push(<div />, <div key={0} />);",
"const arr = [];arr.push(<div />, <div />);",
];

Tester::new(JsxKey::NAME, JsxKey::PLUGIN, pass, fail).test_and_snapshot();
Expand Down
30 changes: 30 additions & 0 deletions crates/oxc_linter/src/snapshots/react_jsx_key.snap
Original file line number Diff line number Diff line change
Expand Up @@ -346,3 +346,33 @@ source: crates/oxc_linter/src/tester.rs
12 │
╰────
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 array.
╭─[jsx_key.tsx:1:26]
1 │ const arr = [];arr.push(<div />);
· ───
╰────

⚠ eslint-plugin-react(jsx-key): Missing "key" prop for element in array.
╭─[jsx_key.tsx:1:43]
1 │ const arr = [];arr.push(<div key={0} />, <div />);
· ───
╰────

⚠ eslint-plugin-react(jsx-key): Missing "key" prop for element in array.
╭─[jsx_key.tsx:1:26]
1 │ const arr = [];arr.push(<div />, <div key={0} />);
· ───
╰────

⚠ eslint-plugin-react(jsx-key): Missing "key" prop for element in array.
╭─[jsx_key.tsx:1:26]
1 │ const arr = [];arr.push(<div />, <div />);
· ───
╰────

⚠ eslint-plugin-react(jsx-key): Missing "key" prop for element in array.
╭─[jsx_key.tsx:1:35]
1 │ const arr = [];arr.push(<div />, <div />);
· ───
╰────
Loading