From d37b9918e20478959018e80e7d6cb45a2be1eb86 Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Wed, 9 Nov 2022 14:44:23 +0100 Subject: [PATCH] Fix `noArrayIndexKey` test --- .../specs/correctness/noArrayIndexKey.jsx | 2 + .../correctness/noArrayIndexKey.jsx.snap | 289 +++++++++++------- 2 files changed, 187 insertions(+), 104 deletions(-) diff --git a/crates/rome_js_analyze/tests/specs/correctness/noArrayIndexKey.jsx b/crates/rome_js_analyze/tests/specs/correctness/noArrayIndexKey.jsx index 81cc1c17ad6..efa70cb986b 100644 --- a/crates/rome_js_analyze/tests/specs/correctness/noArrayIndexKey.jsx +++ b/crates/rome_js_analyze/tests/specs/correctness/noArrayIndexKey.jsx @@ -1,3 +1,5 @@ +import { Children, cloneElement } from "react"; + // invalid something.forEach((Element, index) => { foo diff --git a/crates/rome_js_analyze/tests/specs/correctness/noArrayIndexKey.jsx.snap b/crates/rome_js_analyze/tests/specs/correctness/noArrayIndexKey.jsx.snap index 704239b044f..682b1d94dec 100644 --- a/crates/rome_js_analyze/tests/specs/correctness/noArrayIndexKey.jsx.snap +++ b/crates/rome_js_analyze/tests/specs/correctness/noArrayIndexKey.jsx.snap @@ -4,6 +4,8 @@ expression: noArrayIndexKey.jsx --- # Input ```js +import { Children, cloneElement } from "react"; + // invalid something.forEach((Element, index) => { foo @@ -69,24 +71,24 @@ something.forEach((element, index) => { # Diagnostics ``` -noArrayIndexKey.jsx:3:21 lint/correctness/noArrayIndexKey ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ +noArrayIndexKey.jsx:5:21 lint/correctness/noArrayIndexKey ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ! Avoid using the index of an array as key property in an element. - 1 │ // invalid - 2 │ something.forEach((Element, index) => { - > 3 │ foo + 3 │ // invalid + 4 │ something.forEach((Element, index) => { + > 5 │ foo │ ^^^^^ - 4 │ }); - 5 │ something.forEach((element, index, array) => { + 6 │ }); + 7 │ something.forEach((element, index, array) => { i This is the source of the key value. - 1 │ // invalid - > 2 │ something.forEach((Element, index) => { + 3 │ // invalid + > 4 │ something.forEach((Element, index) => { │ ^^^^^ - 3 │ foo - 4 │ }); + 5 │ foo + 6 │ }); i The order of the items may change, and this also affects performances and component state. @@ -96,25 +98,25 @@ noArrayIndexKey.jsx:3:21 lint/correctness/noArrayIndexKey ━━━━━━━ ``` ``` -noArrayIndexKey.jsx:6:21 lint/correctness/noArrayIndexKey ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ +noArrayIndexKey.jsx:8:21 lint/correctness/noArrayIndexKey ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ! Avoid using the index of an array as key property in an element. - 4 │ }); - 5 │ something.forEach((element, index, array) => { - > 6 │ foo - │ ^^^^^ - 7 │ }); - 8 │ things.filter((thing, index) => { + 6 │ }); + 7 │ something.forEach((element, index, array) => { + > 8 │ foo + │ ^^^^^ + 9 │ }); + 10 │ things.filter((thing, index) => { i This is the source of the key value. - 3 │ foo - 4 │ }); - > 5 │ something.forEach((element, index, array) => { + 5 │ foo + 6 │ }); + > 7 │ something.forEach((element, index, array) => { │ ^^^^^ - 6 │ foo - 7 │ }); + 8 │ foo + 9 │ }); i The order of the items may change, and this also affects performances and component state. @@ -124,25 +126,25 @@ noArrayIndexKey.jsx:6:21 lint/correctness/noArrayIndexKey ━━━━━━━ ``` ``` -noArrayIndexKey.jsx:9:34 lint/correctness/noArrayIndexKey ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ +noArrayIndexKey.jsx:11:34 lint/correctness/noArrayIndexKey ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ! Avoid using the index of an array as key property in an element. - 7 │ }); - 8 │ things.filter((thing, index) => { - > 9 │ otherThings.push(foo); + 9 │ }); + 10 │ things.filter((thing, index) => { + > 11 │ otherThings.push(foo); │ ^^^^^ - 10 │ }); - 11 │ + 12 │ }); + 13 │ i This is the source of the key value. - 6 │ foo - 7 │ }); - > 8 │ things.filter((thing, index) => { + 8 │ foo + 9 │ }); + > 10 │ things.filter((thing, index) => { │ ^^^^^ - 9 │ otherThings.push(foo); - 10 │ }); + 11 │ otherThings.push(foo); + 12 │ }); i The order of the items may change, and this also affects performances and component state. @@ -152,24 +154,24 @@ noArrayIndexKey.jsx:9:34 lint/correctness/noArrayIndexKey ━━━━━━━ ``` ``` -noArrayIndexKey.jsx:13:21 lint/correctness/noArrayIndexKey ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ +noArrayIndexKey.jsx:15:21 lint/correctness/noArrayIndexKey ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ! Avoid using the index of an array as key property in an element. - 12 │ something.forEach((Element, index) => { - > 13 │ + 14 │ something.forEach((Element, index) => { + > 15 │ │ ^^^^^ - 14 │ }); - 15 │ something.forEach((element, index, array) => { + 16 │ }); + 17 │ something.forEach((element, index, array) => { i This is the source of the key value. - 10 │ }); - 11 │ - > 12 │ something.forEach((Element, index) => { + 12 │ }); + 13 │ + > 14 │ something.forEach((Element, index) => { │ ^^^^^ - 13 │ - 14 │ }); + 15 │ + 16 │ }); i The order of the items may change, and this also affects performances and component state. @@ -179,25 +181,25 @@ noArrayIndexKey.jsx:13:21 lint/correctness/noArrayIndexKey ━━━━━━━ ``` ``` -noArrayIndexKey.jsx:16:21 lint/correctness/noArrayIndexKey ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ +noArrayIndexKey.jsx:18:21 lint/correctness/noArrayIndexKey ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ! Avoid using the index of an array as key property in an element. - 14 │ }); - 15 │ something.forEach((element, index, array) => { - > 16 │ + 16 │ }); + 17 │ something.forEach((element, index, array) => { + > 18 │ │ ^^^^^ - 17 │ }); - 18 │ things.filter((thing, index) => { + 19 │ }); + 20 │ things.filter((thing, index) => { i This is the source of the key value. - 13 │ - 14 │ }); - > 15 │ something.forEach((element, index, array) => { + 15 │ + 16 │ }); + > 17 │ something.forEach((element, index, array) => { │ ^^^^^ - 16 │ - 17 │ }); + 18 │ + 19 │ }); i The order of the items may change, and this also affects performances and component state. @@ -207,25 +209,25 @@ noArrayIndexKey.jsx:16:21 lint/correctness/noArrayIndexKey ━━━━━━━ ``` ``` -noArrayIndexKey.jsx:19:34 lint/correctness/noArrayIndexKey ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ +noArrayIndexKey.jsx:21:34 lint/correctness/noArrayIndexKey ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ! Avoid using the index of an array as key property in an element. - 17 │ }); - 18 │ things.filter((thing, index) => { - > 19 │ otherThings.push(); + 19 │ }); + 20 │ things.filter((thing, index) => { + > 21 │ otherThings.push(); │ ^^^^^ - 20 │ }); - 21 │ things.reduce((collection, thing, index) => ( + 22 │ }); + 23 │ things.reduce((collection, thing, index) => ( i This is the source of the key value. - 16 │ - 17 │ }); - > 18 │ things.filter((thing, index) => { + 18 │ + 19 │ }); + > 20 │ things.filter((thing, index) => { │ ^^^^^ - 19 │ otherThings.push(); - 20 │ }); + 21 │ otherThings.push(); + 22 │ }); i The order of the items may change, and this also affects performances and component state. @@ -235,25 +237,25 @@ noArrayIndexKey.jsx:19:34 lint/correctness/noArrayIndexKey ━━━━━━━ ``` ``` -noArrayIndexKey.jsx:22:35 lint/correctness/noArrayIndexKey ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ +noArrayIndexKey.jsx:24:35 lint/correctness/noArrayIndexKey ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ! Avoid using the index of an array as key property in an element. - 20 │ }); - 21 │ things.reduce((collection, thing, index) => ( - > 22 │ collection.concat() + 22 │ }); + 23 │ things.reduce((collection, thing, index) => ( + > 24 │ collection.concat() │ ^^^^^ - 23 │ ), []); - 24 │ + 25 │ ), []); + 26 │ i This is the source of the key value. - 19 │ otherThings.push(); - 20 │ }); - > 21 │ things.reduce((collection, thing, index) => ( + 21 │ otherThings.push(); + 22 │ }); + > 23 │ things.reduce((collection, thing, index) => ( │ ^^^^^ - 22 │ collection.concat() - 23 │ ), []); + 24 │ collection.concat() + 25 │ ), []); i The order of the items may change, and this also affects performances and component state. @@ -263,24 +265,24 @@ noArrayIndexKey.jsx:22:35 lint/correctness/noArrayIndexKey ━━━━━━━ ``` ``` -noArrayIndexKey.jsx:26:38 lint/correctness/noArrayIndexKey ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ +noArrayIndexKey.jsx:28:38 lint/correctness/noArrayIndexKey ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ! Avoid using the index of an array as key property in an element. - 25 │ React.Children.map(this.props.children, (child, index) => ( - > 26 │ React.cloneElement(child, { key: index }) + 27 │ React.Children.map(this.props.children, (child, index) => ( + > 28 │ React.cloneElement(child, { key: index }) │ ^^^^^ - 27 │ )) - 28 │ + 29 │ )) + 30 │ i This is the source of the key value. - 23 │ ), []); - 24 │ - > 25 │ React.Children.map(this.props.children, (child, index) => ( + 25 │ ), []); + 26 │ + > 27 │ React.Children.map(this.props.children, (child, index) => ( │ ^^^^^ - 26 │ React.cloneElement(child, { key: index }) - 27 │ )) + 28 │ React.cloneElement(child, { key: index }) + 29 │ )) i The order of the items may change, and this also affects performances and component state. @@ -290,24 +292,103 @@ noArrayIndexKey.jsx:26:38 lint/correctness/noArrayIndexKey ━━━━━━━ ``` ``` -noArrayIndexKey.jsx:30:45 lint/correctness/noArrayIndexKey ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ +noArrayIndexKey.jsx:32:45 lint/correctness/noArrayIndexKey ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ! Avoid using the index of an array as key property in an element. - 29 │ React.Children.forEach(this.props.children, function (child, index) { - > 30 │ return React.cloneElement(child, { key: index }) + 31 │ React.Children.forEach(this.props.children, function (child, index) { + > 32 │ return React.cloneElement(child, { key: index }) │ ^^^^^ - 31 │ }) - 32 │ + 33 │ }) + 34 │ i This is the source of the key value. - 27 │ )) - 28 │ - > 29 │ React.Children.forEach(this.props.children, function (child, index) { + 29 │ )) + 30 │ + > 31 │ React.Children.forEach(this.props.children, function (child, index) { │ ^^^^^ - 30 │ return React.cloneElement(child, { key: index }) - 31 │ }) + 32 │ return React.cloneElement(child, { key: index }) + 33 │ }) + + i The order of the items may change, and this also affects performances and component state. + + i Check the React documentation. + + +``` + +``` +noArrayIndexKey.jsx:37:32 lint/correctness/noArrayIndexKey ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + ! Avoid using the index of an array as key property in an element. + + 36 │ Children.map(this.props.children, (child, index) => ( + > 37 │ cloneElement(child, { key: index }) + │ ^^^^^ + 38 │ )) + 39 │ + + i This is the source of the key value. + + > 36 │ Children.map(this.props.children, (child, index) => ( + │ ^^^^^ + 37 │ cloneElement(child, { key: index }) + 38 │ )) + + i The order of the items may change, and this also affects performances and component state. + + i Check the React documentation. + + +``` + +``` +noArrayIndexKey.jsx:41:39 lint/correctness/noArrayIndexKey ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + ! Avoid using the index of an array as key property in an element. + + 40 │ Children.forEach(this.props.children, function (child, index) { + > 41 │ return cloneElement(child, { key: index }) + │ ^^^^^ + 42 │ }) + 43 │ + + i This is the source of the key value. + + 38 │ )) + 39 │ + > 40 │ Children.forEach(this.props.children, function (child, index) { + │ ^^^^^ + 41 │ return cloneElement(child, { key: index }) + 42 │ }) + + i The order of the items may change, and this also affects performances and component state. + + i Check the React documentation. + + +``` + +``` +noArrayIndexKey.jsx:45:44 lint/correctness/noArrayIndexKey ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + ! Avoid using the index of an array as key property in an element. + + 44 │ Children.forEach(this.props.children, function (child, index) { + > 45 │ const foo = cloneElement(child, { key: index }) + │ ^^^^^ + 46 │ return foo; + 47 │ }) + + i This is the source of the key value. + + 42 │ }) + 43 │ + > 44 │ Children.forEach(this.props.children, function (child, index) { + │ ^^^^^ + 45 │ const foo = cloneElement(child, { key: index }) + 46 │ return foo; i The order of the items may change, and this also affects performances and component state. @@ -317,22 +398,22 @@ noArrayIndexKey.jsx:30:45 lint/correctness/noArrayIndexKey ━━━━━━━ ``` ``` -noArrayIndexKey.jsx:49:38 lint/correctness/noArrayIndexKey ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ +noArrayIndexKey.jsx:51:38 lint/correctness/noArrayIndexKey ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ! Avoid using the index of an array as key property in an element. - 48 │ things.map((thing, index) => ( - > 49 │ React.cloneElement(thing, { key: index }) + 50 │ things.map((thing, index) => ( + > 51 │ React.cloneElement(thing, { key: index }) │ ^^^^^ - 50 │ )); - 51 │ + 52 │ )); + 53 │ i This is the source of the key value. - > 48 │ things.map((thing, index) => ( + > 50 │ things.map((thing, index) => ( │ ^^^^^ - 49 │ React.cloneElement(thing, { key: index }) - 50 │ )); + 51 │ React.cloneElement(thing, { key: index }) + 52 │ )); i The order of the items may change, and this also affects performances and component state.