New Rule: no-unnecessary-spread - a rule to prevent syntactically unnecessary object, array and JSX attribute spreads
See original GitHub issueRule details
This rule would detect provably useless object literal, array literal, and JSX attribute spreads
Related ECMAScript feature
It’s not new, quite old - but I thought there was high value in this check based on my anecdotal experience across several large codebases.
What type of rule is this?
Warns about a potential problem
Example bad code
const x = { ...{} };
const y = { ...{ foo: 1 } };
const z = {
a: 1,
b: 2,
...{
c: 3,
d: 4
},
e: 5,
};
const a = [ ...[] ];
const b = [ ...[1] ];
const c = [
1,
2,
...[
3,
4
],
5,
];
const j = <div {...{}} />;
const k = <div {...{ foo: 1 }} />;
const l = <div
a={1}
b={2}
{...{
c: 3,
d: 4
}}
e={5}
/>;
Good versions of the above "bad" code
const x = { };
const y = { foo: 1 };
const z = {
a: 1,
b: 2,
c: 3,
d: 4
e: 5,
};
const a = [ ];
const b = [ 1 ];
const c = [
1,
2,
3,
4,
5,
];
const j = <div />;
const k = <div foo={1} />;
const l = <div
a={1}
b={2}
c={3}
d={4}
e={5}
/>;
Why should this rule be in the core instead of a plugin?
I’ve worked across a few codebases and found this issue occurs at an alarmingly high rate in each codebase. I think it would be of great benefit to the community if it was part of the core rules and part of the recommended set to help cleanup obviously and syntactically unnecessary, dead code.
Participation
- I am willing to submit a pull request to implement this rule.
Additional comments
I have a prototype of the rule ready to go that I’ve tested on a large codebase (10s of thousands of files) and it includes a fixer that handles the majority of cases (in this codebase 97% of the cases were auto-fixable)
Issue Analytics
- State:
- Created 9 months ago
- Reactions:1
- Comments:6 (6 by maintainers)
Top GitHub Comments
I’m 👍 to the rule.
yes, I’m 100% supportive if the syntax is new added. imho, the purpose of our policy change is to provide only rules that are sufficiently general(and useful), and I believe this proposal is well met. maybe we could make an exception here?