Skip to content

Commit d000177

Browse files
committed
[Refactor] jsx-key: use more AST selectors
1 parent 24402fb commit d000177

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
4040
* [Refactor] [`jsx-sort-default-props`]: remove unnecessary code ([#1817][] @ljharb)
4141
* [Docs] [`jsx-no-target-blank`]: fix syntax highlighting ([#3199][] @shamrin)
4242
* [Docs] [`jsx-key`]: improve example ([#3202][] @chnakamura)
43+
* [Refactor] [`jsx-key`]: use more AST selectors (@ljharb)
4344

4445
[#3207]: https://github.com/yannickcr/eslint-plugin-react/issues/3207
4546
[#3202]: https://github.com/yannickcr/eslint-plugin-react/pull/3202

lib/rules/jsx-key.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -163,19 +163,19 @@ module.exports = {
163163
},
164164

165165
// Array.prototype.map
166-
'CallExpression, OptionalCallExpression'(node) {
167-
if (node.callee && node.callee.type !== 'MemberExpression' && node.callee.type !== 'OptionalMemberExpression') {
168-
return;
169-
}
170-
171-
if (node.callee && node.callee.property && node.callee.property.name !== 'map') {
172-
return;
173-
}
174-
166+
// eslint-disable-next-line no-multi-str
167+
'CallExpression[callee.type="MemberExpression"][callee.property.name="map"],\
168+
CallExpression[callee.type="OptionalMemberExpression"][callee.property.name="map"],\
169+
OptionalCallExpression[callee.type="MemberExpression"][callee.property.name="map"],\
170+
OptionalCallExpression[callee.type="OptionalMemberExpression"][callee.property.name="map"]'(node) {
175171
const fn = node.arguments[0];
176172
const isFn = fn && fn.type === 'FunctionExpression';
177173
const isArrFn = fn && fn.type === 'ArrowFunctionExpression';
178174

175+
if (!fn && !isFn && !isArrFn) {
176+
return;
177+
}
178+
179179
if (isArrFn && (fn.body.type === 'JSXElement' || fn.body.type === 'JSXFragment')) {
180180
checkIteratorElement(fn.body);
181181
}

0 commit comments

Comments
 (0)