diff --git a/lib/rules/jsx-key.js b/lib/rules/jsx-key.js
index ccc91a470e..e4e9adf5fc 100644
--- a/lib/rules/jsx-key.js
+++ b/lib/rules/jsx-key.js
@@ -36,8 +36,31 @@ module.exports = {
return body.filter(item => item.type === 'ReturnStatement')[0];
}
+ const childrenToArraySelector = `:matches(
+ CallExpression
+ [callee.object.object.name=React]
+ [callee.object.property.name=Children]
+ [callee.property.name=toArray],
+ CallExpression
+ [callee.object.name=Children]
+ [callee.property.name=toArray]
+ )`.replace(/\s/g, '');
+ let isWithinChildrenToArray = false;
+
return {
+ [childrenToArraySelector]: function() {
+ isWithinChildrenToArray = true;
+ },
+
+ [`${childrenToArraySelector}:exit`]: function() {
+ isWithinChildrenToArray = false;
+ },
+
JSXElement: function(node) {
+ if (isWithinChildrenToArray) {
+ return;
+ }
+
if (hasProp(node.openingElement.attributes, 'key')) {
return;
}
@@ -52,6 +75,10 @@ module.exports = {
// Array.prototype.map
CallExpression: function (node) {
+ if (isWithinChildrenToArray) {
+ return;
+ }
+
if (node.callee && node.callee.type !== 'MemberExpression') {
return;
}
diff --git a/tests/lib/rules/jsx-key.js b/tests/lib/rules/jsx-key.js
index 4bea4bd5ee..dc9626667b 100644
--- a/tests/lib/rules/jsx-key.js
+++ b/tests/lib/rules/jsx-key.js
@@ -37,7 +37,15 @@ ruleTester.run('jsx-key', rule, {
{code: '[1, 2, 3].foo(x =>