eslint-plugin-unicorn icon indicating copy to clipboard operation
eslint-plugin-unicorn copied to clipboard

`no-array-for-each` should skip fix if second parameter is not an `Identifier`

Open fisker opened this issue 2 years ago • 2 comments

#1812 makes me realize that the following edge cases will cause runtime error by autofix.

// The second parameter default value can be a reference from first parameter
array.forEach((element, index = element) {})
array.forEach(({foo}, index = foo) {})

Even if it's not an AssignmentPattern

// The second parameter property value can be value from first parameter
array.forEach((element, {bar = element}) {})
array.forEach(({foo}, {bar = foo}) {})

If array is a real array, the second parameter should be a number, so we should only allow Identifier.

//cc @TimShilov

fisker avatar May 12 '22 06:05 fisker

@fisker I'm happy to help on this one but I'm struggling to wrap my head around how tests work in this repo so I cannot come up with a failing test.

I've added these cases

'foo.forEach((bar, index = bar) => {})',
'foo.forEach(({bar}, {index = bar}) => {});',

... to snapshot invalid cases here https://github.com/sindresorhus/eslint-plugin-unicorn/blob/166524a5613d9f0e27cb5bc505fe83d86ff9e7b2/test/no-array-for-each.mjs#L26-L28 but the tests are still passing just fine.

If you can push me in the right direction I can make a PR.

TimShilov avatar May 12 '22 15:05 TimShilov

Just check the snapshots are expected.

fisker avatar May 25 '22 01:05 fisker