Skip to content

Commit

Permalink
fix(useJsxKeyInIterables): unwrap parenthesized expression (#2016)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sec-ant authored Mar 9, 2024
1 parent 0264357 commit 0763c47
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,14 @@ fn handle_iterators(
let body = callback.body().ok()?;
match body {
AnyJsFunctionBody::AnyJsExpression(expr) => {
handle_potential_react_component(&expr, model, is_inside_jsx)
// unwrap parenthesized expression
let mut inner_expr = expr;
while let AnyJsExpression::JsParenthesizedExpression(parenthesized_expr) =
inner_expr
{
inner_expr = parenthesized_expr.expression().ok()?;
}
handle_potential_react_component(&inner_expr, model, is_inside_jsx)
.map(|state| vec![state])
}
AnyJsFunctionBody::JsFunctionBody(body) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ React.Children.map(c => React.cloneElement(c));
(<h1>{data.map(c => <h1></h1>)}</h1>)

(<h1>{data.map(c => xyz)}</h1>)

(<h1>{data.map(c => (<h1></h1>))}</h1>)
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ React.Children.map(c => React.cloneElement(c));

(<h1>{data.map(c => xyz)}</h1>)

(<h1>{data.map(c => (<h1></h1>))}</h1>)
```

# Diagnostics
Expand Down Expand Up @@ -586,10 +587,28 @@ invalid.jsx:37:21 lint/nursery/useJsxKeyInIterable ━━━━━━━━━
> 37 │ (<h1>{data.map(c => xyz)}</h1>)
│ ^^^
38 │
39 │ (<h1>{data.map(c => (<h1></h1>))}</h1>)
i Either return a JSX expression, or suppress this instance if you determine it is safe.
i Check the React documentation for why a key prop is required.
```

```
invalid.jsx:39:22 lint/nursery/useJsxKeyInIterable ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! Missing key property for this element in iterable.
37 │ (<h1>{data.map(c => xyz)}</h1>)
38 │
> 39 │ (<h1>{data.map(c => (<h1></h1>))}</h1>)
│ ^^^^
i The order of the items may change, and having a key can help React identify which item was moved.
i Check the React documentation.
```
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,5 @@ React.Children.map(c => React.cloneElement(c, {key: c}));
(<h1>{[<h1 key={1}></h1>, <h1 key={2}></h1>, <h1 key={3}></h1>]}</h1>)

(<h1>{data.map(c => <h1 key={c}></h1>)}</h1>)

(<h1>{data.map(c => (<h1 key={c}></h1>))}</h1>)
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,5 @@ React.Children.map(c => React.cloneElement(c, {key: c}));

(<h1>{data.map(c => <h1 key={c}></h1>)}</h1>)

(<h1>{data.map(c => (<h1 key={c}></h1>))}</h1>)
```

0 comments on commit 0763c47

Please sign in to comment.