eslint-plugin-svelte icon indicating copy to clipboard operation
eslint-plugin-svelte copied to clipboard

feat: add `no-unnecessary-state-wrap` rule

Open baseballyama opened this issue 10 months ago • 13 comments

close: https://github.com/sveltejs/eslint-plugin-svelte/issues/1052

baseballyama avatar Feb 09 '25 08:02 baseballyama

🦋 Changeset detected

Latest commit: fe121fd87c04d1a8ef36f34a02cdfc7b1f2726ca

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
eslint-plugin-svelte Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

changeset-bot[bot] avatar Feb 09 '25 08:02 changeset-bot[bot]

Try the Instant Preview in Online Playground

ESLint Online Playground

Install the Instant Preview to Your Local

npm i https://pkg.pr.new/eslint-plugin-svelte@1062

Published Instant Preview Packages:

  • eslint-plugin-svelte: https://pkg.pr.new/eslint-plugin-svelte@1062

View Commit

github-actions[bot] avatar Feb 09 '25 08:02 github-actions[bot]

Hi, sorry if this is unfinished and you're planning to add this, but atm, this ignores https://github.com/sveltejs/eslint-plugin-svelte/issues/1052#issuecomment-2630480779

marekdedic avatar Feb 09 '25 12:02 marekdedic

Since the clear function can be used, $state is usually unnecessary. If there is a reason to use $state(new SvelteMap()), this rule can simply be disabled. ESLint is fully configurable.

baseballyama avatar Feb 09 '25 12:02 baseballyama

I still have to disagree - the rule now has a clear false positive that can be detected by its implementation, but just isn't. And I'm sorry, but yeah, you can disable any rule for any line, but if the FP can be detected by the rule, it should be. Only when it's not possible (like what we're discussing in #910 ) should users be forced to manually disable rules...

marekdedic avatar Feb 10 '25 10:02 marekdedic

Can you provide an example of fp?

baseballyama avatar Feb 10 '25 10:02 baseballyama

Of course, see https://github.com/sveltejs/eslint-plugin-svelte/issues/1052#issuecomment-2630480779 for a simple example or here for a real-world example (this one is in $derived, but the same semantics apply)

marekdedic avatar Feb 10 '25 10:02 marekdedic

Then, let’s add an option to not report an error when a reassignment exists.

baseballyama avatar Feb 10 '25 14:02 baseballyama

Thanks :) I don't even think it needs to be an option - if a $state(SvelteMap) is reassigned, the $state is always necessary...

marekdedic avatar Feb 10 '25 16:02 marekdedic

I don’t think so. I believe it might simply be a mistake. At least in my project, I want to consistently treat it as an error. For example, in the following case, it can clearly use “clear”.

  let map = $state(new SvelteMap());
  map = new SvelteMap();

baseballyama avatar Feb 10 '25 16:02 baseballyama

Hmm, you are absolutely right that the code snippet is incorrect, thanks, I din't realize that!

I am thinking if this isn't a separate issue, though - the anti-pattern here is IMO assigning a new SvelteMap() to an object which is a SvelteMap instead of using .clear(). And that would also be an issue with the regular Map, wouldn't it? So maybe you found a more general issue here that assigning new X() to a variable of type X is incorrect and can be replaced with X.clear() for X being any of SvelteMap, SvelteSet, Map, Set (I checked MDN and that's all I could find)

Now if that were a separate rule it would nicely dovetail with this one even if this rule ignored all re-assigned variables, the other rule would replace the re-assignment with .clear() and this rule would then remove the $state.

I feel I am rambling a little bit - do I make any sense to you? :D

marekdedic avatar Feb 11 '25 11:02 marekdedic

In any case, for those who don’t want this rule enabled, they can simply disable it. In most cases, $state(new SvelteMap()) doesn’t really make sense, so I think having it enabled by default isn’t a bad idea.

baseballyama avatar Feb 23 '25 13:02 baseballyama

I added allowReassign option.

baseballyama avatar Feb 23 '25 14:02 baseballyama