-
Notifications
You must be signed in to change notification settings - Fork 656
feat(rome_js_analyze): noAccumulatingSpread #4426
Conversation
✅ Deploy Preview for docs-rometools ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify site settings. |
@Vivalldi if you rebase your PR, the tests CI should correctly run! There was some issue with the CI |
Thanks! Rebased and hopefully it works out |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are some cases that we should check, to avoid possible false positives.
@Vivalldi if you agree and if you'd like to follow-up, we can merge this PR and you could make another PR to supplement these new cases. Or, you could do it now in this PR.
I would choose the first option, but let the decision to you!
/// var a = ['a', 'b', 'c']; | ||
/// a.reduce((acc, val) => {acc.push(val); return acc}, []); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add an example where .reduce
is not a built-in function? For example:
var a = {
reduce() {}
};
a.reduce();
"foo.reduceRight((acc, bar) => {acc[bar.key] = bar.value; return acc;}, {})", | ||
|
||
// Object - Allow spreading the item into the accumulator | ||
"foo.reduce((acc, bar) => {acc[bar.key] = { ...bar.value }; return acc;}, {})", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe, among the use cases, we should have somewhere we check that .reduce
and .reduceRight
are actually called on arrays.
For example, e case like this should be valid:
var a = { reduce() {} }
a.reduce();
Or
class A { reduce() {} }
var a = new A();
a.reduce()
@ematipico, I agree with the additional cases. Happy to get this PR in and I'll update test cases in a follow up |
Summary
From a performance standpoint, spread on an accumulator is terrible,
O(n^2)
(see discussion for details). The approach I took in this rule is to query onJsSpread
nodes. Initially we're only covering cases where the node is spreading a known accumulator (those found in.reduce
&.reduceRight
).Discussion #4320
Future Ideas
At some point I'd like to cover cases where spreading is being done on any accumulator, such as in the case of for loopsTest Plan
Since this rule queries on spread operators I've added tests that cover known invalid cases while also ensuring that valid uses of spread are maintained.
.reduce
cases.reduceRight
casesChangelog
Documentation