Skip to content

chore: warn unnecessary-state-wrap #1272

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

hyunbinseo
Copy link
Contributor

@hyunbinseo hyunbinseo commented Jul 17, 2025

There are cases where SvelteSet has to be reassigned. Reference sveltejs/svelte#16422 (comment)

<script lang="ts">
  import { SvelteSet } from 'svelte/reactivity';

  // SvelteSet is already reactive, $state wrapping is unnecessary.
  let ids = $state(new SvelteSet([1]));
  const count = $derived(ids.size); // added
</script>

{#each ids as id (id)}{id}{/each}

<button type="button" onclick={() => (ids = new SvelteSet([2]))}>
  Update IDs
</button>

Since this rule is considered a best practice, the recommendation can be lowered from error to warn.

This will allow devs to comment out the ESLint warning when needed with confidence.

Note

#1202 is a PR for setting ["error", { "allowReassign": true }] as the default option:

https://sveltejs.github.io/eslint-plugin-svelte/rules/no-unnecessary-state-wrap/#allow-reassign

Copy link

changeset-bot bot commented Jul 17, 2025

🦋 Changeset detected

Latest commit: b6d3e90

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 Patch

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

Copy link
Contributor

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@b6d3e90

Published Instant Preview Packages:

View Commit

@marekdedic
Copy link
Contributor

Hi,
this is a little bit more complicated issue and I don't think there is a clear agreement :/ For my 2 cents, in your example, the rule pushes you towards using Set.clear(), which is IMO better that re-assignening (at least performance-wise)

@hyunbinseo
Copy link
Contributor Author

@marekdedic Thanks for the comment.

I am trying to clear and re-add multiple (100+) new items from an array.

Is set.clear() and arr.forEach((v) => set.add(v)) still the advised method?

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

Successfully merging this pull request may close these issues.

2 participants