Skip to content

Commit fe4006b

Browse files
committed
fix(linter/jsx-key): false positive in react/jsx-key (#11918)
fixes #11916
1 parent f102cb1 commit fe4006b

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use cow_utils::CowUtils;
22
use oxc_ast::{
33
AstKind,
44
ast::{
5-
CallExpression, Expression, JSXAttributeItem, JSXAttributeName, JSXElement, JSXFragment,
6-
Statement,
5+
Argument, CallExpression, Expression, JSXAttributeItem, JSXAttributeName, JSXElement,
6+
JSXFragment, Statement,
77
},
88
};
99
use oxc_diagnostics::OxcDiagnostic;
@@ -150,6 +150,7 @@ enum InsideArrayOrIterator {
150150
Iterator(Span),
151151
}
152152

153+
#[expect(clippy::bool_to_int_with_if)]
153154
fn is_in_array_or_iter<'a, 'b>(
154155
node: &'b AstNode<'a>,
155156
ctx: &'b LintContext<'a>,
@@ -158,6 +159,7 @@ fn is_in_array_or_iter<'a, 'b>(
158159

159160
let mut is_outside_containing_function = false;
160161
let mut is_explicit_return = false;
162+
let mut argument = None;
161163

162164
loop {
163165
let parent = ctx.nodes().parent_node(node.id())?;
@@ -202,9 +204,15 @@ fn is_in_array_or_iter<'a, 'b>(
202204
AstKind::CallExpression(v) => {
203205
let callee = &v.callee.without_parentheses();
204206

205-
if let Some(v) = callee.as_member_expression() {
206-
if let Some((span, ident)) = v.static_property_info() {
207-
if TARGET_METHODS.contains(&ident) {
207+
if let Some(member_expr) = callee.as_member_expression() {
208+
if let Some((span, ident)) = member_expr.static_property_info() {
209+
if TARGET_METHODS.contains(&ident)
210+
&& argument.is_some_and(|argument: &Argument<'_>| {
211+
v.arguments
212+
.get(if ident == "from" { 1 } else { 0 })
213+
.is_some_and(|arg| arg.span() == argument.span())
214+
})
215+
{
208216
return Some(InsideArrayOrIterator::Iterator(span));
209217
}
210218
}
@@ -219,6 +227,9 @@ fn is_in_array_or_iter<'a, 'b>(
219227
AstKind::ReturnStatement(_) => {
220228
is_explicit_return = true;
221229
}
230+
AstKind::Argument(arg) => {
231+
argument = Some(arg);
232+
}
222233
_ => {}
223234
}
224235
node = parent;
@@ -495,6 +506,7 @@ fn test() {
495506
);
496507
}))}
497508
",
509+
r"const DummyComponent: FC<{ children: ReactNode }> = ({ children }) => { const wrappedChildren = Children.map(children, (child) => { return <div>{child}</div>; }); return <main>{wrappedChildren}</main>; };",
498510
];
499511

500512
let fail = vec![

0 commit comments

Comments
 (0)