Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rule proposal: Prefer Array.from(arr, mapFn) over Array.from(arr).map(mapFn) #2357

Open
yvele opened this issue May 13, 2024 · 5 comments
Open

Comments

@yvele
Copy link

yvele commented May 13, 2024

Description

Prefer Array.from(arrayLike, mapFn) over Array.from(arrayLike).map(...)

Prefer directly using Array.from mapFn argument instead of an extra map.

This is supposed to improve performances.

Fail

const foo = Array.from(bar).map(baz);
const foo = Array.from(bar).map((element, i) => baz(element, i));

Pass

const foo = Array.from(bar, baz);
const foo = Array.from(bar, (element, i) => baz(element, i));

Proposed rule name

prefer-array-from-map-fn

Additional Info

This should be automatically fixable by the --fix CLI option.

This looks 🤔 a bit related to:

@sindresorhus
Copy link
Owner

Please fill out the title.

@fisker
Copy link
Collaborator

fisker commented May 13, 2024

This rule may conflict with prefer-array-flat-map, I'm not how will ESLint work if we enable both for following code

Array.from(foo).map(bar).flat()

@yvele yvele changed the title Rule proposal: Rule proposal: Prefer Array.from(arr, mapFn) over Array.from(arr).map(...) May 13, 2024
@yvele
Copy link
Author

yvele commented May 13, 2024

Please fill out the title.

Done

This rule may conflict with prefer-array-flat-map, I'm not how will ESLint work if we enable both for following code

Array.from(foo).map(bar).flat()

We first need to figure out what we want to have

Array.from(foo, bar).flat()

vs

Array.from(foo).flatMap(bar)

In my opinion the flatMap(bar) looks more expressive, because bar is more about flatting than about mapping, does that make sense?
From performance perspective they looks the same (do they?)

@yvele yvele changed the title Rule proposal: Prefer Array.from(arr, mapFn) over Array.from(arr).map(...) Rule proposal: Prefer Array.from(arr, mapFn) over Array.from(arr).map(mapFn) May 13, 2024
@silverwind
Copy link
Contributor

Already exists in another plugin: https://github.com/freaktechnik/eslint-plugin-array-func#from-map

@yvele
Copy link
Author

yvele commented May 13, 2024

Already exists in another plugin: https://github.com/freaktechnik/eslint-plugin-array-func#from-map

Okay cool, thanks 👍

https://www.npmjs.com/package/eslint-plugin-array-func

image

So.. what now? @fisker @sindresorhus I would prefer to stick with eslint-plugin-unicorn only that is well managed and that I trust. Will this feature be added to eslint-plugin-unicorn or should I add an extra eslint-plugin-array-func to my ESLint config? 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants