Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

feat(rome_js_analyze): noAccumulatingSpread #4426

Merged
merged 9 commits into from
May 2, 2023

Conversation

Vivalldi
Copy link
Contributor

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 on JsSpread 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 loops
// invalid
let acc = [];
for (bar in foo) {
  acc = [...acc, bar];
}

// valid
const acc = [];
for (bar in foo) {
  acc.push(bar);
}

Test 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.

  • Known .reduce cases
  • Known .reduceRight cases

Changelog

  • The PR requires a changelog line

Documentation

  • The PR requires documentation
  • I will create a new PR to update the documentation

@github-actions github-actions bot added A-Diagnostic Area: errors and diagnostics A-Linter Area: linter A-Project Area: project configuration and loading labels Apr 30, 2023
@netlify
Copy link

netlify bot commented Apr 30, 2023

Deploy Preview for docs-rometools ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 9a10db3
🔍 Latest deploy log https://app.netlify.com/sites/docs-rometools/deploys/644fbdfc7860f60008aee819
😎 Deploy Preview https://deploy-preview-4426--docs-rometools.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@ematipico
Copy link
Contributor

@Vivalldi if you rebase your PR, the tests CI should correctly run! There was some issue with the CI

@Vivalldi Vivalldi force-pushed the vivalldi/no-accumulating-spread branch from be6ceb3 to 714cb6b Compare May 1, 2023 12:46
@Vivalldi
Copy link
Contributor Author

Vivalldi commented May 1, 2023

@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

Copy link
Contributor

@ematipico ematipico left a 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!

Comment on lines +44 to +45
/// var a = ['a', 'b', 'c'];
/// a.reduce((acc, val) => {acc.push(val); return acc}, []);
Copy link
Contributor

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;}, {})",
Copy link
Contributor

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()

@Vivalldi
Copy link
Contributor Author

Vivalldi commented May 2, 2023

@ematipico, I agree with the additional cases. Happy to get this PR in and I'll update test cases in a follow up

@ematipico ematipico merged commit 365fc59 into rome:main May 2, 2023
16 checks passed
@Vivalldi Vivalldi deleted the vivalldi/no-accumulating-spread branch May 2, 2023 13:39
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A-Diagnostic Area: errors and diagnostics A-Linter Area: linter A-Project Area: project configuration and loading
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants