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

Bug: prefer-spread shouldn't encourage iterating over strings #2521

Open
JoshuaKGoldberg opened this issue Dec 26, 2024 · 1 comment
Open
Labels

Comments

@JoshuaKGoldberg
Copy link

JoshuaKGoldberg commented Dec 26, 2024

prefer-spread reports on String#split(''), fixing to a typical .... That matches the design of string iterability as it was originally intended. However, allowing primitives to be iterated over is widely considered a mistake: https://github.com/tc39/how-we-work/blob/main/normative-conventions.md#reject-primitives-in-iterable-taking-positions.

Proposal: can eslint-plugin-unicorn remove the preference of String#split('') from prefer-spread?

const text = 'hello 👋';

text.split(''); // "fixed" to [...text]

I'm roughly quoting @Josh-Cena from this discussion: https://github.com/typescript-eslint/typescript-eslint/pull/8509/files#r1781707713. Context: in typescript-eslint, we're implementing typescript-eslint/typescript-eslint#748 Rule proposal: prevent array, iterable and function spreads into objects. The rule is set to report on ...string by default... and then unicorn/prefer-spread reports on String#split(''). You can't win!

@github-actions github-actions bot changed the title Bug: [prefer-spread] shouldn't encourage iterating over strings Bug: prefer-spread shouldn't encourage iterating over strings Dec 26, 2024
@Josh-Cena
Copy link

I do appreciate that this is a virtually impossible task to be done correctly by default. .split("") is wrong but so is ..., just on different levels. JS should really expose iterator getters like .graphemes(), .graphemeClusters(), etc... But for now, either removing the suggestion or recommending Intl.Segmenter makes more sense. Alternatively, in our rule, we can add docs for disabling checking string spreading.

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

No branches or pull requests

2 participants