Skip to content

Commit

Permalink
[Fix] jsx-key: prevent false "missing array key" warning
Browse files Browse the repository at this point in the history
Fixes #3215
  • Loading branch information
ljharb committed Feb 25, 2022
1 parent ddcc5b3 commit 13d8df6
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 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
* [`jsx-key`]: prevent false "missing array key" warning ([#3215][] @ljharb)

[#3215]: https://github.com/yannickcr/eslint-plugin-react/issues/3215

## [7.29.0] - 2022.02.24

### Added
Expand Down
8 changes: 5 additions & 3 deletions lib/rules/jsx-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,11 @@ module.exports = {
const keys = attrs.filter((x) => x.name && x.name.name === 'key');

if (keys.length === 0) {
report(context, messages.missingArrayKey, 'missingArrayKey', {
node: element,
});
if (node.type === 'ArrayExpression') {
report(context, messages.missingArrayKey, 'missingArrayKey', {
node: element,
});
}
} else {
keys.forEach((attr) => {
const value = context.getSourceCode().getText(attr.value);
Expand Down
41 changes: 41 additions & 0 deletions tests/lib/rules/jsx-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,47 @@ ruleTester.run('jsx-key', rule, {
];
`,
},
{
code: `
function Component(props) {
return hasPayment ? (
<div className="stuff">
<BookingDetailSomething {...props} />
{props.modal && props.calculatedPrice && (
<SomeOtherThing items={props.something} discount={props.discount} />
)}
</div>
) : null;
}
`,
},
{
code: `
import React, { FC, useRef, useState } from 'react';
import './ResourceVideo.sass';
import VimeoVideoPlayInModal from '../vimeoVideoPlayInModal/VimeoVideoPlayInModal';
type Props = {
videoUrl: string;
videoTitle: string;
};
const ResourceVideo: FC<Props> = ({
videoUrl,
videoTitle,
}: Props): JSX.Element => {
return (
<div className="resource-video">
<VimeoVideoPlayInModal videoUrl={videoUrl} />
<h3>{videoTitle}</h3>
</div>
);
};
export default ResourceVideo;
`,
features: ['types'],
},
]),
invalid: parsers.all([
{
Expand Down

0 comments on commit 13d8df6

Please sign in to comment.