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

[jsx-key] False positive when spreading props

Open jp7837 opened this issue 3 years ago • 6 comments

Receiving false positive on line 5 below. Since the key is part of the props spread in the element creation, it should not be flagged.

const elements = source.map((s, index) => {
	const props = {
		key: index,
	};
	return <div {...props}></div>;
});

jp7837 avatar Nov 28 '22 15:11 jp7837

Most of the time an object is spread, it’s not locally contained, and thus not statically analyzeable. Why aren’t you specifying the props explicitly inline here?

ljharb avatar Nov 28 '22 16:11 ljharb

This is just a simplified repro, but I'm wondering if we should err on the false negative side here since we can't properly analyze it?

jp7837 avatar Nov 28 '22 20:11 jp7837

I think that in the very few cases where it's justified to dynamically spread props like this, an override comment would make sense.

It might help if you provided an unsimplified repro?

ljharb avatar Nov 28 '22 21:11 ljharb

Yeah agree it's an edge case and we can override. In my project, we defined a type in another file which contained all the props including key. This is probably going to be rare though, so if there's a technical challenge here with static analysis, I am okay to close this.

jp7837 avatar Dec 09 '22 01:12 jp7837

It might be possible to leverage typescript-eslint information to confirm there's a key present in the type, but if not, there'd be nothing we could do.

In general, I think key shouldn't be defined in any shared type like that, since it's specific to the usage of a given element, and not to all uses of the component.

ljharb avatar Dec 09 '22 01:12 ljharb

Just want to throw in that we're encountering the same issue in 1 or 2 places. Happy to override with a disable directive for now as it's an edge case/uncommon occurrence for us too.

jwarby avatar Jun 11 '24 14:06 jwarby