@@ -54,6 +54,9 @@ ruleTester.run('no-array-index-key', rule, {
5454 {
5555 code : 'foo.map((baz, i) => React.cloneElement(someChild, { ...someChild.props }))' ,
5656 } ,
57+ {
58+ code : 'foo.map((baz, i) => cloneElement(someChild, { ...someChild.props }))' ,
59+ } ,
5760 {
5861 code : `
5962 foo.map((item, i) => {
@@ -63,6 +66,15 @@ ruleTester.run('no-array-index-key', rule, {
6366 })
6467 ` ,
6568 } ,
69+ {
70+ code : `
71+ foo.map((item, i) => {
72+ return cloneElement(someChild, {
73+ key: item.id
74+ })
75+ })
76+ ` ,
77+ } ,
6678 {
6779 code : 'foo.map((baz, i) => <Foo key />)' ,
6880 } ,
@@ -100,13 +112,27 @@ ruleTester.run('no-array-index-key', rule, {
100112 })
101113 ` ,
102114 } ,
115+ {
116+ code : `
117+ React.Children.map(this.props.children, (child, index, arr) => {
118+ return cloneElement(child, { key: child.id });
119+ })
120+ ` ,
121+ } ,
103122 {
104123 code : `
105124 Children.forEach(this.props.children, (child, index, arr) => {
106125 return React.cloneElement(child, { key: child.id });
107126 })
108127 ` ,
109128 } ,
129+ {
130+ code : `
131+ Children.forEach(this.props.children, (child, index, arr) => {
132+ return cloneElement(child, { key: child.id });
133+ })
134+ ` ,
135+ } ,
110136 {
111137 code : 'foo?.map(child => <Foo key={child.i} />)' ,
112138 features : [ 'optional chaining' ] ,
@@ -145,6 +171,14 @@ ruleTester.run('no-array-index-key', rule, {
145171 code : 'foo.map((baz, i) => React.cloneElement(someChild, { ...someChild.props, key: i }))' ,
146172 errors : [ { messageId : 'noArrayIndex' } ] ,
147173 } ,
174+ {
175+ code : `
176+ import { cloneElement } from 'react';
177+
178+ foo.map((baz, i) => cloneElement(someChild, { ...someChild.props, key: i }))
179+ ` ,
180+ errors : [ { messageId : 'noArrayIndex' } ] ,
181+ } ,
148182 {
149183 code : `
150184 foo.map((item, i) => {
@@ -155,6 +189,18 @@ ruleTester.run('no-array-index-key', rule, {
155189 ` ,
156190 errors : [ { messageId : 'noArrayIndex' } ] ,
157191 } ,
192+ {
193+ code : `
194+ import { cloneElement } from 'react';
195+
196+ foo.map((item, i) => {
197+ return cloneElement(someChild, {
198+ key: i
199+ })
200+ })
201+ ` ,
202+ errors : [ { messageId : 'noArrayIndex' } ] ,
203+ } ,
158204 {
159205 code : 'foo.forEach((bar, i) => { baz.push(<Foo key={i} />); })' ,
160206 errors : [ { messageId : 'noArrayIndex' } ] ,
@@ -229,33 +275,73 @@ ruleTester.run('no-array-index-key', rule, {
229275 } ,
230276 {
231277 code : `
232- Children.map(this.props.children, (child, index) => {
233- return React.cloneElement(child, { key: index });
234- })
278+ Children.map(this.props.children, (child, index) => {
279+ return React.cloneElement(child, { key: index });
280+ })
281+ ` ,
282+ errors : [ { messageId : 'noArrayIndex' } ] ,
283+ } ,
284+ {
285+ code : `
286+ import { cloneElement } from 'react';
287+
288+ Children.map(this.props.children, (child, index) => {
289+ return cloneElement(child, { key: index });
290+ })
235291 ` ,
236292 errors : [ { messageId : 'noArrayIndex' } ] ,
237293 } ,
238294 {
239295 code : `
240- React.Children.map(this.props.children, (child, index) => {
241- return React.cloneElement(child, { key: index });
242- })
296+ React.Children.map(this.props.children, (child, index) => {
297+ return React.cloneElement(child, { key: index });
298+ })
243299 ` ,
244300 errors : [ { messageId : 'noArrayIndex' } ] ,
245301 } ,
246302 {
247303 code : `
248- Children.forEach(this.props.children, (child, index) => {
249- return React.cloneElement(child, { key: index });
250- })
304+ import { cloneElement } from 'react';
305+
306+ React.Children.map(this.props.children, (child, index) => {
307+ return cloneElement(child, { key: index });
308+ })
251309 ` ,
252310 errors : [ { messageId : 'noArrayIndex' } ] ,
253311 } ,
254312 {
255313 code : `
256- React.Children.forEach(this.props.children, (child, index) => {
257- return React.cloneElement(child, { key: index });
258- })
314+ Children.forEach(this.props.children, (child, index) => {
315+ return React.cloneElement(child, { key: index });
316+ })
317+ ` ,
318+ errors : [ { messageId : 'noArrayIndex' } ] ,
319+ } ,
320+ {
321+ code : `
322+ import { cloneElement } from 'react';
323+
324+ Children.forEach(this.props.children, (child, index) => {
325+ return cloneElement(child, { key: index });
326+ })
327+ ` ,
328+ errors : [ { messageId : 'noArrayIndex' } ] ,
329+ } ,
330+ {
331+ code : `
332+ React.Children.forEach(this.props.children, (child, index) => {
333+ return React.cloneElement(child, { key: index });
334+ })
335+ ` ,
336+ errors : [ { messageId : 'noArrayIndex' } ] ,
337+ } ,
338+ {
339+ code : `
340+ import { cloneElement } from 'react';
341+
342+ React.Children.forEach(this.props.children, (child, index) => {
343+ return cloneElement(child, { key: index });
344+ })
259345 ` ,
260346 errors : [ { messageId : 'noArrayIndex' } ] ,
261347 } ,
0 commit comments