Skip to content

Commit

Permalink
[Fix] no-array-index-key: consider flatMap
Browse files Browse the repository at this point in the history
  • Loading branch information
k-yle authored and ljharb committed Feb 11, 2023
1 parent 66b58dd commit abb4871
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange

## Unreleased

### Fixed
* [`no-array-index-key`]: consider flatMap ([#3530][] @k-yle)

[#3530]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3530

## [7.32.2] - 2023.01.28

### Fixed
Expand Down
4 changes: 4 additions & 0 deletions docs/rules/no-array-index-key.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ things.findIndex((thing, index) => {
otherThings.push(<Hello key={index} />);
});

things.flatMap((thing, index) => (
<Hello key={index} />
));

things.reduce((collection, thing, index) => (
collection.concat(<Hello key={index} />)
), []);
Expand Down
1 change: 1 addition & 0 deletions lib/rules/no-array-index-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ module.exports = {
filter: 1,
find: 1,
findIndex: 1,
flatMap: 1,
forEach: 1,
map: 1,
reduce: 2,
Expand Down
7 changes: 7 additions & 0 deletions tests/lib/rules/no-array-index-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ ruleTester.run('no-array-index-key', rule, {
{
code: 'foo.map((bar, i) => <Foo key={String(baz)} />)',
},
{
code: 'foo.flatMap((a) => <Foo key={a} />)',
},
{
code: 'foo.reduce((a, b) => a.concat(<Foo key={b.id} />), [])',
},
Expand Down Expand Up @@ -226,6 +229,10 @@ ruleTester.run('no-array-index-key', rule, {
code: 'foo.reduce((a, b, i) => a.concat(<Foo key={i} />), [])',
errors: [{ messageId: 'noArrayIndex' }],
},
{
code: 'foo.flatMap((a, i) => <Foo key={i} />)',
errors: [{ messageId: 'noArrayIndex' }],
},
{
code: 'foo.reduceRight((a, b, i) => a.concat(<Foo key={i} />), [])',
errors: [{ messageId: 'noArrayIndex' }],
Expand Down

0 comments on commit abb4871

Please sign in to comment.