Skip to content

Commit

Permalink
[Refactor] jsx-key: use more AST selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Feb 22, 2022
1 parent 24402fb commit d000177
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
* [Refactor] [`jsx-sort-default-props`]: remove unnecessary code ([#1817][] @ljharb)
* [Docs] [`jsx-no-target-blank`]: fix syntax highlighting ([#3199][] @shamrin)
* [Docs] [`jsx-key`]: improve example ([#3202][] @chnakamura)
* [Refactor] [`jsx-key`]: use more AST selectors (@ljharb)

[#3207]: https://github.com/yannickcr/eslint-plugin-react/issues/3207
[#3202]: https://github.com/yannickcr/eslint-plugin-react/pull/3202
Expand Down
18 changes: 9 additions & 9 deletions lib/rules/jsx-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,19 +163,19 @@ module.exports = {
},

// Array.prototype.map
'CallExpression, OptionalCallExpression'(node) {
if (node.callee && node.callee.type !== 'MemberExpression' && node.callee.type !== 'OptionalMemberExpression') {
return;
}

if (node.callee && node.callee.property && node.callee.property.name !== 'map') {
return;
}

// eslint-disable-next-line no-multi-str
'CallExpression[callee.type="MemberExpression"][callee.property.name="map"],\
CallExpression[callee.type="OptionalMemberExpression"][callee.property.name="map"],\
OptionalCallExpression[callee.type="MemberExpression"][callee.property.name="map"],\
OptionalCallExpression[callee.type="OptionalMemberExpression"][callee.property.name="map"]'(node) {
const fn = node.arguments[0];
const isFn = fn && fn.type === 'FunctionExpression';
const isArrFn = fn && fn.type === 'ArrowFunctionExpression';

if (!fn && !isFn && !isArrFn) {
return;
}

if (isArrFn && (fn.body.type === 'JSXElement' || fn.body.type === 'JSXFragment')) {
checkIteratorElement(fn.body);
}
Expand Down

0 comments on commit d000177

Please sign in to comment.