diff --git a/crates/oxc_linter/src/rules/react/jsx_key.rs b/crates/oxc_linter/src/rules/react/jsx_key.rs index e24674750519d..a7f00f34423f1 100644 --- a/crates/oxc_linter/src/rules/react/jsx_key.rs +++ b/crates/oxc_linter/src/rules/react/jsx_key.rs @@ -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); } } } @@ -505,6 +511,8 @@ fn test() { }))} ", r"const DummyComponent: FC<{ children: ReactNode }> = ({ children }) => { const wrappedChildren = Children.map(children, (child) => { return
{child}
; }); return
{wrappedChildren}
; };", + "const arr = [];arr.push(
);", + "const arr = [];arr.push(
,
);", ]; let fail = vec![ @@ -618,6 +626,10 @@ fn test() { Children.toArray([1, 2 ,3].map(x => )); Children.toArray(Array.from([1, 2 ,3], x => )); ", + "const arr = [];arr.push(
);", + "const arr = [];arr.push(
,
);", + "const arr = [];arr.push(
,
);", + "const arr = [];arr.push(
,
);", ]; Tester::new(JsxKey::NAME, JsxKey::PLUGIN, pass, fail).test_and_snapshot(); diff --git a/crates/oxc_linter/src/snapshots/react_jsx_key.snap b/crates/oxc_linter/src/snapshots/react_jsx_key.snap index 62f40cb899dca..fe5f3eb1a6d16 100644 --- a/crates/oxc_linter/src/snapshots/react_jsx_key.snap +++ b/crates/oxc_linter/src/snapshots/react_jsx_key.snap @@ -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(
); + · ─── + ╰──── + + ⚠ eslint-plugin-react(jsx-key): Missing "key" prop for element in array. + ╭─[jsx_key.tsx:1:43] + 1 │ const arr = [];arr.push(
,
); + · ─── + ╰──── + + ⚠ eslint-plugin-react(jsx-key): Missing "key" prop for element in array. + ╭─[jsx_key.tsx:1:26] + 1 │ const arr = [];arr.push(
,
); + · ─── + ╰──── + + ⚠ eslint-plugin-react(jsx-key): Missing "key" prop for element in array. + ╭─[jsx_key.tsx:1:26] + 1 │ const arr = [];arr.push(
,
); + · ─── + ╰──── + + ⚠ eslint-plugin-react(jsx-key): Missing "key" prop for element in array. + ╭─[jsx_key.tsx:1:35] + 1 │ const arr = [];arr.push(
,
); + · ─── + ╰────