Skip to content

Commit 13d8df6

Browse files
committed
[Fix] jsx-key: prevent false "missing array key" warning
Fixes #3215
1 parent ddcc5b3 commit 13d8df6

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
55

66
## Unreleased
77

8+
### Fixed
9+
* [`jsx-key`]: prevent false "missing array key" warning ([#3215][] @ljharb)
10+
11+
[#3215]: https://github.com/yannickcr/eslint-plugin-react/issues/3215
12+
813
## [7.29.0] - 2022.02.24
914

1015
### Added

lib/rules/jsx-key.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,11 @@ module.exports = {
119119
const keys = attrs.filter((x) => x.name && x.name.name === 'key');
120120

121121
if (keys.length === 0) {
122-
report(context, messages.missingArrayKey, 'missingArrayKey', {
123-
node: element,
124-
});
122+
if (node.type === 'ArrayExpression') {
123+
report(context, messages.missingArrayKey, 'missingArrayKey', {
124+
node: element,
125+
});
126+
}
125127
} else {
126128
keys.forEach((attr) => {
127129
const value = context.getSourceCode().getText(attr.value);

tests/lib/rules/jsx-key.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,47 @@ ruleTester.run('jsx-key', rule, {
7474
];
7575
`,
7676
},
77+
{
78+
code: `
79+
function Component(props) {
80+
return hasPayment ? (
81+
<div className="stuff">
82+
<BookingDetailSomething {...props} />
83+
{props.modal && props.calculatedPrice && (
84+
<SomeOtherThing items={props.something} discount={props.discount} />
85+
)}
86+
</div>
87+
) : null;
88+
}
89+
`,
90+
},
91+
{
92+
code: `
93+
import React, { FC, useRef, useState } from 'react';
94+
95+
import './ResourceVideo.sass';
96+
import VimeoVideoPlayInModal from '../vimeoVideoPlayInModal/VimeoVideoPlayInModal';
97+
98+
type Props = {
99+
videoUrl: string;
100+
videoTitle: string;
101+
};
102+
const ResourceVideo: FC<Props> = ({
103+
videoUrl,
104+
videoTitle,
105+
}: Props): JSX.Element => {
106+
return (
107+
<div className="resource-video">
108+
<VimeoVideoPlayInModal videoUrl={videoUrl} />
109+
<h3>{videoTitle}</h3>
110+
</div>
111+
);
112+
};
113+
114+
export default ResourceVideo;
115+
`,
116+
features: ['types'],
117+
},
77118
]),
78119
invalid: parsers.all([
79120
{

0 commit comments

Comments
 (0)