@@ -54,6 +54,9 @@ ruleTester.run('no-array-index-key', rule, {
54
54
{
55
55
code : 'foo.map((baz, i) => React.cloneElement(someChild, { ...someChild.props }))' ,
56
56
} ,
57
+ {
58
+ code : 'foo.map((baz, i) => cloneElement(someChild, { ...someChild.props }))' ,
59
+ } ,
57
60
{
58
61
code : `
59
62
foo.map((item, i) => {
@@ -63,6 +66,15 @@ ruleTester.run('no-array-index-key', rule, {
63
66
})
64
67
` ,
65
68
} ,
69
+ {
70
+ code : `
71
+ foo.map((item, i) => {
72
+ return cloneElement(someChild, {
73
+ key: item.id
74
+ })
75
+ })
76
+ ` ,
77
+ } ,
66
78
{
67
79
code : 'foo.map((baz, i) => <Foo key />)' ,
68
80
} ,
@@ -100,13 +112,27 @@ ruleTester.run('no-array-index-key', rule, {
100
112
})
101
113
` ,
102
114
} ,
115
+ {
116
+ code : `
117
+ React.Children.map(this.props.children, (child, index, arr) => {
118
+ return cloneElement(child, { key: child.id });
119
+ })
120
+ ` ,
121
+ } ,
103
122
{
104
123
code : `
105
124
Children.forEach(this.props.children, (child, index, arr) => {
106
125
return React.cloneElement(child, { key: child.id });
107
126
})
108
127
` ,
109
128
} ,
129
+ {
130
+ code : `
131
+ Children.forEach(this.props.children, (child, index, arr) => {
132
+ return cloneElement(child, { key: child.id });
133
+ })
134
+ ` ,
135
+ } ,
110
136
{
111
137
code : 'foo?.map(child => <Foo key={child.i} />)' ,
112
138
features : [ 'optional chaining' ] ,
@@ -145,6 +171,14 @@ ruleTester.run('no-array-index-key', rule, {
145
171
code : 'foo.map((baz, i) => React.cloneElement(someChild, { ...someChild.props, key: i }))' ,
146
172
errors : [ { messageId : 'noArrayIndex' } ] ,
147
173
} ,
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
+ } ,
148
182
{
149
183
code : `
150
184
foo.map((item, i) => {
@@ -155,6 +189,18 @@ ruleTester.run('no-array-index-key', rule, {
155
189
` ,
156
190
errors : [ { messageId : 'noArrayIndex' } ] ,
157
191
} ,
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
+ } ,
158
204
{
159
205
code : 'foo.forEach((bar, i) => { baz.push(<Foo key={i} />); })' ,
160
206
errors : [ { messageId : 'noArrayIndex' } ] ,
@@ -229,33 +275,73 @@ ruleTester.run('no-array-index-key', rule, {
229
275
} ,
230
276
{
231
277
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
+ })
235
291
` ,
236
292
errors : [ { messageId : 'noArrayIndex' } ] ,
237
293
} ,
238
294
{
239
295
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
+ })
243
299
` ,
244
300
errors : [ { messageId : 'noArrayIndex' } ] ,
245
301
} ,
246
302
{
247
303
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
+ })
251
309
` ,
252
310
errors : [ { messageId : 'noArrayIndex' } ] ,
253
311
} ,
254
312
{
255
313
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
+ })
259
345
` ,
260
346
errors : [ { messageId : 'noArrayIndex' } ] ,
261
347
} ,
0 commit comments