Skip to content

Commit db5a068

Browse files
authored
no-array-for-each: Fix broken auto-fixed code (#1039)
1 parent ca2f54f commit db5a068

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

rules/no-array-for-each.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ function getFixFunction(callExpression, sourceCode, functionInfo) {
4949
const {returnStatements} = functionInfo.get(callback);
5050

5151
const getForOfLoopHeadText = () => {
52-
const parametersText = parameters.map(parameter => sourceCode.getText(parameter));
52+
const [elementText, indexText] = parameters.map(parameter => sourceCode.getText(parameter));
5353
const useEntries = parameters.length === 2;
5454

5555
let text = 'for (const ';
56-
text += useEntries ? `[${parametersText.join(', ')}]` : parametersText[0];
56+
text += useEntries ? `[${indexText}, ${elementText}]` : elementText;
5757

5858
text += ' of ';
5959

@@ -351,7 +351,7 @@ const create = context => {
351351
},
352352
ReturnStatement(node) {
353353
const currentFunction = functionStack[functionStack.length - 1];
354-
// `globalReturn `
354+
// `globalReturn`
355355
/* istanbul ignore next: ESLint deprecated `ecmaFeatures`, can't test */
356356
if (!currentFunction) {
357357
return;

test/snapshots/no-array-for-each.js.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ Generated by [AVA](https://avajs.dev).
397397
> Output
398398
399399
`␊
400-
1 | for (const [foo, index] of a((foo, index) => foo + index).entries()) bar();␊
400+
1 | for (const [index, foo] of a((foo, index) => foo + index).entries()) bar();␊
401401
`
402402

403403
> Error 1/1
@@ -417,7 +417,7 @@ Generated by [AVA](https://avajs.dev).
417417
`␊
418418
1 | const foo = [];␊
419419
2 | const index = 1;␊
420-
3 | for (const [foo, index] of a.entries()) foo[index];␊
420+
3 | for (const [index, foo] of a.entries()) foo[index];␊
421421
`
422422

423423
> Error 1/1
@@ -763,7 +763,7 @@ Generated by [AVA](https://avajs.dev).
763763
> Output
764764
765765
`␊
766-
1 | for (const [element, index] of foo.entries()) {␊
766+
1 | for (const [index, element] of foo.entries()) {␊
767767
2 | bar(element, index);␊
768768
3 | }␊
769769
`
@@ -785,7 +785,7 @@ Generated by [AVA](https://avajs.dev).
785785
> Output
786786
787787
`␊
788-
1 | for (const [element, index] of foo.entries()) {␊
788+
1 | for (const [index, element] of foo.entries()) {␊
789789
2 | bar(element, index);␊
790790
3 | }␊
791791
`
@@ -807,7 +807,7 @@ Generated by [AVA](https://avajs.dev).
807807
> Output
808808
809809
`␊
810-
1 | for (const [element, index] of foo.entries()) {␊
810+
1 | for (const [index, element] of foo.entries()) {␊
811811
2 | bar(element, index);␊
812812
3 | }␊
813813
`
@@ -827,7 +827,7 @@ Generated by [AVA](https://avajs.dev).
827827
> Output
828828
829829
`␊
830-
1 | for (const [element, index] of foo.entries()) bar(element, index);␊
830+
1 | for (const [index, element] of foo.entries()) bar(element, index);␊
831831
`
832832

833833
> Error 1/1
@@ -843,7 +843,7 @@ Generated by [AVA](https://avajs.dev).
843843
> Output
844844
845845
`␊
846-
1 | for (const [element, index] of (foo).entries()) bar(element, index)␊
846+
1 | for (const [index, element] of (foo).entries()) bar(element, index)␊
847847
`
848848

849849
> Error 1/1
@@ -859,7 +859,7 @@ Generated by [AVA](https://avajs.dev).
859859
> Output
860860
861861
`␊
862-
1 | for (const [element, index] of (0, foo).entries()) bar(element, index)␊
862+
1 | for (const [index, element] of (0, foo).entries()) bar(element, index)␊
863863
`
864864

865865
> Error 1/1
0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)