Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(lint/useJsxKeyInIterable): fix a false positive when key is in the return statement, but not any variable declarations #3031

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,28 @@ fn handle_function_body(
model: &SemanticModel,
is_inside_jsx: bool,
) -> Vec<TextRange> {
// if the return statement definitely has a key prop, don't need to check the rest of the function
let return_statement = node
.statements()
.iter()
.find_map(|statement| statement.as_js_return_statement().cloned());
let is_return_component = return_statement
.as_ref()
.and_then(|ret| {
let returned_value = ret.argument()?;
Some(ReactComponentExpression::can_cast(
returned_value.syntax().kind(),
))
})
.unwrap_or_default();
let ranges = return_statement.and_then(|ret| {
let returned_value = ret.argument()?;
handle_potential_react_component(returned_value, model, is_inside_jsx)
});
if ranges.is_none() && is_return_component {
return vec![];
}

node.statements()
.iter()
.filter_map(|statement| {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,8 @@ React.Children.map(c => React.cloneElement(c, {key: c}));
const div = <div key={item.id}>{x}</div>;
return div;
});

[].map((item) => {
const node = <button type="button">{item.label}</button>;
return <Fragment key={item.label}>{node}</Fragment>;
})
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,9 @@ React.Children.map(c => React.cloneElement(c, {key: c}));
return div;
});

[].map((item) => {
const node = <button type="button">{item.label}</button>;
return <Fragment key={item.label}>{node}</Fragment>;
})

```