Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: no-array-index-key not working in React.Children methods #2085

Merged
merged 5 commits into from Dec 22, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/rules/no-array-index-key.md
Expand Up @@ -50,6 +50,14 @@ things.reduce((collection, thing, index) => (
things.reduceRight((collection, thing, index) => (
collection.concat(<Hello key={index} />)
), []);

React.Children.map(this.props.children, (child, index) => (
React.cloneElement(child, { key: index })
))

Children.forEach(this.props.children, (child, index) => (
React.cloneElement(child, { key: index })
))
```

The following patterns are **not** considered warnings:
Expand Down
41 changes: 33 additions & 8 deletions lib/rules/no-array-index-key.js
Expand Up @@ -47,6 +47,32 @@ module.exports = {
&& indexParamNames.indexOf(node.name) !== -1;
}

function isUsingReactChildren(node) {
const callee = node.callee;
if (
!callee
|| !callee.property
|| !callee.object
) {
return null;
}

const isReactChildMethod = ['map', 'forEach'].indexOf(callee.property.name) > -1;
if (!isReactChildMethod) {
return null;
}

const obj = callee.object;
if (obj && obj.name === 'Children') {
return true;
}
if (obj && obj.object && obj.object.name === 'React') {
ljharb marked this conversation as resolved.
Show resolved Hide resolved
return true;
}

return false;
}

function getMapIndexParamName(node) {
const callee = node.callee;
if (callee.type !== 'MemberExpression') {
Expand All @@ -59,16 +85,19 @@ module.exports = {
return null;
}

const firstArg = node.arguments[0];
if (!firstArg) {
const callbackArg = isUsingReactChildren(node)
? node.arguments[1]
: node.arguments[0];

if (!callbackArg) {
return null;
}

if (!astUtil.isFunctionLikeExpression(firstArg)) {
if (!astUtil.isFunctionLikeExpression(callbackArg)) {
return null;
}

const params = firstArg.params;
const params = callbackArg.params;

const indexParamPosition = iteratorFunctionsToIndexParamPosition[callee.property.name];
if (params.length < indexParamPosition + 1) {
Expand Down Expand Up @@ -132,24 +161,20 @@ module.exports = {
&& ['createElement', 'cloneElement'].indexOf(node.callee.property.name) !== -1
&& node.arguments.length > 1
) {
// React.createElement
if (!indexParamNames.length) {
return;
}

const props = node.arguments[1];

if (props.type !== 'ObjectExpression') {
return;
}

ljharb marked this conversation as resolved.
Show resolved Hide resolved
props.properties.forEach(prop => {
if (!prop.key || prop.key.name !== 'key') {
// { ...foo }
// { foo: bar }
return;
}

checkPropValue(prop.value);
});

Expand Down
53 changes: 53 additions & 0 deletions tests/lib/rules/no-array-index-key.js
Expand Up @@ -89,6 +89,22 @@ ruleTester.run('no-array-index-key', rule, {

{
code: 'foo.reduceRight((a, b, i) => a.concat(<Foo key={b.id} />), [])'
},

{
code: `
React.Children.map(this.props.children, (child, index, arr) => {
return React.cloneElement(child, { key: child.id });
})
`
},

{
code: `
Children.forEach(this.props.children, (child, index, arr) => {
return React.cloneElement(child, { key: child.id });
})
`
}
],

Expand Down Expand Up @@ -227,6 +243,43 @@ ruleTester.run('no-array-index-key', rule, {
{
code: 'foo.findIndex((bar, i) => { baz.push(React.createElement(\'Foo\', { key: i })); })',
errors: [{message: 'Do not use Array index in keys'}]
},

{
code: `
Children.map(this.props.children, (child, index) => {
return React.cloneElement(child, { key: index });
})
`,
errors: [{message: 'Do not use Array index in keys'}]
},

{
code: `
React.Children.map(this.props.children, (child, index) => {
return React.cloneElement(child, { key: index });
})
`,
errors: [{message: 'Do not use Array index in keys'}]
},

{
code: `
Children.forEach(this.props.children, (child, index) => {
return React.cloneElement(child, { key: index });
})
`,
errors: [{message: 'Do not use Array index in keys'}]
},

{
code: `
React.Children.forEach(this.props.children, (child, index) => {
return React.cloneElement(child, { key: index });
})
`,
errors: [{message: 'Do not use Array index in keys'}]
}

]
});